Form Load Events not processed when Form is called 2nd time

前端 未结 1 808
独厮守ぢ
独厮守ぢ 2021-01-23 04:18

Why doesn\'t the following work?

Two forms; first calls the second. Second form has a DataGridView on it - it has no columns in it, they\'re added by the program, along

相关标签:
1条回答
  • 2021-01-23 04:33

    I think you have a convergence of several things causing this. First, forms are classes and ought to be instanced explicitly. Instead of Form2.ShowDialog() do this:

    Using frm As New Form2        ' create instance
       frm.ShowDialog
       ' do something
    End Using                     ' dialogs are also a resource
    

    Using/ .Dispose is not needed with normal forms because when you Close them, they are disposed of. Not so with dialogs since we unusually just Hide them so we can get info from them.

    Next, the Form_Load event is only called the FIRST time you show the form. See MSDN: Occurs before a form is displayed for the first time.

    So by reusing a non disposed-of Form2, the Load event isnt called and the code in the Load event isnt executed. It should work fine if you dispose of and create new form instances. BTW this applies to all forms, not just dialogs.

    0 讨论(0)
提交回复
热议问题