【问题标题】:Form Load Events not processed when Form is called 2nd time第二次调用表单时未处理表单加载事件
【发布时间】:2014-06-22 19:18:22
【问题描述】:

为什么以下方法不起作用?

两种形式;第一个调用第二个。第二种形式有一个 DataGridView - 它没有列,它们是由程序添加的,还有一个 DataGridViewButtonColumn。

第一次调用 Form2 可以正常工作。但是再次调用它,按钮没有任何文字。

' The first form - has one button, which calls Form2
Public Class Form1
    Friend fruit As New List(Of Fruit)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        fruit.Add(New Fruit("Apple", "Red"))
        fruit.Add(New Fruit("Orange", "Orange"))
        fruit.Add(New Fruit("Banana", "Yellow"))
        fruit.Add(New Fruit("Melon", "Red"))
        fruit.Add(New Fruit("Pear", "Green"))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.ShowDialog()
    End Sub
End Class


Public Class Fruit
    Public Property name As String
    Public Property colour As String
    Public Sub New(newName As String, newColour As String)
        name = newName
        colour = newColour
    End Sub
End Class

第二种形式的代码是:

' Form2 has a button which closes the form, and a DataGridView
Public Class Form2
    Dim dataGridViewButtonColumn1 As DataGridViewButtonColumn
    Dim setupAlready As Boolean = False

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dataGridViewButtonColumn1 = New DataGridViewButtonColumn
        DataGridView1.DataSource = Form1.fruit
        With dataGridViewButtonColumn1
            .Name = "ButtonCol"
            .UseColumnTextForButtonValue = False
        End With
        If Not setupAlready Then
            DataGridView1.Columns.Add(dataGridViewButtonColumn1)
        End If
        For i As Integer = 0 To 4
            DataGridView1.Rows(i).Cells("ButtonCol").Value = "Hello"
        Next
        setupAlready = True
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) _
                Handles DataGridView1.CellContentClick
        Debug.Print(String.Format("Col={0}, Row={1}, ColName={2}", e.ColumnIndex, e.RowIndex, DataGridView1.Columns(e.ColumnIndex).Name))
        If (DataGridView1.Rows.Item(e.RowIndex).Cells("ButtonCol").Value Is "Hello") Then
            DataGridView1.Rows.Item(e.RowIndex).Cells("ButtonCol").Value = "GoodBye"
            DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class

【问题讨论】:

    标签: .net vb.net winforms datagridview


    【解决方案1】:

    我认为你有几个因素导致了这种情况。首先,表单是类,应该显式地实例化。而不是Form2.ShowDialog() 这样做:

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

    Using/ .Dispose 不需要普通表单,因为当您关闭它们时,它们会被丢弃。对话并非如此,因为我们通常只是隐藏它们以便我们可以从中获取信息。

    接下来,Form_Load 事件仅在您第一次显示表单时被调用。见MSDNOccurs before a form is displayed for the first time.

    因此,通过重用未释放的Form2,不会调用 Load 事件并且不会执行 Load 事件中的代码。如果您处理并创建新的表单实例,它应该可以正常工作。顺便说一句,这适用于所有表单,而不仅仅是对话框。

    【讨论】:

    • 谢谢,很有帮助,很好地解决了问题! (抱歉回复慢,离开一周)。
    猜你喜欢
    • 1970-01-01
    • 2011-07-06
    • 2019-06-05
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多