2016-03-31 5 views
1

GUI上でリストビューを使用して、長い手順で何が起こっているのかをユーザーにフィードバックします。モジュールには、メインプロシージャに4つのサブプロシージャがあり、リストビューの最後の列には、保留中、実行中、完了済みなどのステータスが表示されます。したがって、サブプロシージャが完了すると、対応する行のサブアイテムは「完了」として更新される必要があります。リストビューはビジュアルベーシックのモジュールから更新(またはアクセス)できません2015

ところで、基本的にはフォームであるGUIが表示されているとき(フォームロード)に問題なく、リストビューに項目を追加できます。

以下のスクリプトで、ボタンのクリックイベントでその関数を呼び出すと、すべてうまく機能します。しかし、このモジュールでは、listviewにすでに4つの項目が追加されている間にメッセージボックスの結果はゼロになります。メッセージボックスの後の行に次のエラーが表示されます。
"System.Windows.Forms.dllで 'System.ArgumentOutOfRangeException'型の未処理の例外が発生しました 追加情報:InvalidArgument =値 '0'は 'index '

問題は何ですか?前もって感謝します!フォームが読み込まれる前に、サブメインが呼び出された場合

Module Module1 
Sub CreateReport() 
    Call SubProcedure1() 
    'Update listview 
    MessageBox.Show("There is/are: " & form6_ins.ListView1.Items.Count & " item(s)!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 
    Call ListView_ScheduleUpdate1(0, 2, "Completed", 1, 2, "Running", ListView1) 'Throws Error 
    Call SubProcedure2() 
    'Update listview as well 
    Call SubProcedure3() 
    'Update listview as well 
    Call SubProcedure4() 
    'Update listview as well 
End Sub 

'Function to update the listview items 
Public Function ListView_ScheduleUpdate1(ByVal itemNumX As Integer, ByVal subItemNumX As Integer, ByVal givenFeedbackX As String, 
           ByVal itemNumY As Integer, ByVal subItemNumY As Integer, ByVal givenFeedbackY As String, ByRef LV As ListView) 

    LV.Items(itemNumX).SubItems(subItemNumX).Text = givenFeedbackX 
    LV.Items(itemNumY).SubItems(subItemNumY).Text = givenFeedbackY 
    LV.Refresh() 
End Function 
End Module 


'Form 
Public Class Form1 

Private Sub But_Report_DSchedule_Click(sender As Object, e As EventArgs) Handles But_Report_DSchedule.Click 
    MessageBox.Show("There is/are: " & ListView1.Items.Count & " item(s)!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 

    Call ListView_ScheduleUpdate1(0, 2, "Completed", 1, 2, "Running", ListView1) 
End Sub 
End Class 

答えて

1

は、リストビューは実際にはまだ存在していません。フォームが読み込まれるまで、フォーム上のコントロールのプロパティにアクセスしようとしてはいけません。書籍「Hardcore Visual Basic」から:

When a form is loaded, the sequence of events is the following:

A form is a class, and all classes have an Initialize event. Form_Initialize is called (or fired, as control developers describe calling events) as soon as you touch any variable or property of a form. Use this event to initialize variables private to the form. Normally, you should not touch any form or control properties here because doing so fires the next event.

Form_Load fires after Form_Initialize as the visual elements of the form are being created and default values are being assigned to properties. Normally, you should avoid doing anything that will draw something on the form because that causes an automatic firing of the next event. It might be tempting to call Show in Form_Load and then do further processing with the visible form, but you’re usually better off doing this in the next event.

Form_Activate fires after you have loaded and shown the form. It’s possible to load a form without showing it; in this case, Form_Activate isn’t fired until you call the Show method. Form_Activate is also called when you switch from one modeless form to another, or when you switch between MDI forms (but not when you return focus from another application). If your application has modeless or MDI forms, don’t do anything in Form_Activate that you want to happen only once. Or use a static variable (such as fNotFirst) to protect against multiple initializations in Form_Activate. The interactions between the three initialization events can be confusing. Sometimes trial and error is the only way to figure out the right initialization sequence for your application.

+0

@ CB_Ron-ご返信ありがとうございます。 Sub Mainは、フォーム上のボタンをクリックすると呼び出されます。実際にはメインサブではなく、メインサブにフォームがロードされます。私はそれを誤ってラベルした。フォームはそのサブの前にあります。 – Yusuf

関連する問題