未加工的形式加载事件时,形式被称为第二次形式、被称为、加载、事件

由网友(荒岛孤人笑)分享简介:为什么不下面的工作?两种形式;首先调用第二。第二种形式上有一个DataGridView - 有没有在它的列,它们是由该程序添加,再加上DataGridViewButtonColumn 调用Form2的第一次工作正常。但称这是第二次,按键没有任何文字。第一种形式 - 有一个按钮,它调用窗体2公共类Form1中朋友水果...

为什么不下面的工作?

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

调用Form2的第一次工作正常。但称这是第二次,按键没有任何文字。

 第一种形式 - 有一个按钮,它调用窗体2
公共类Form1中
    朋友水果作为新的列表(水果)

    私人小组Form1_Load的(发送者为对象,E作为EventArgs的)把手MyBase.Load
        fruit.Add(新成果(苹果,红))
        fruit.Add(新成果(橙色,橙色))
        fruit.Add(新成果(香蕉,黄))
        fruit.Add(新成果(甜瓜,红))
        fruit.Add(新成果(梨花,绿))
    结束小组

    私人小组的button1_Click(发送者为对象,E作为EventArgs的)把手Button1.Click
        Form2.ShowDialog()
    结束小组
末级


公共类水果
    公共属性名称作为字符串
    公共属性颜色作为字符串
    公共子新(了newName作为字符串,newColour作为字符串)
        NAME =了newName
        颜色= newColour
    结束小组
末级
 

code对于第二种形式是:

 窗体2有一个按钮,关闭窗体,和一个DataGridView
公共类窗体2
    昏暗dataGridViewButtonColumn1作为DataGridViewButtonColumn
    昏暗setupAlready由于布尔=假

    私人小组Form2_Load(发送者为对象,E作为EventArgs的)把手MyBase.Load
        dataGridViewButtonColumn1 =新DataGridViewButtonColumn
        DataGridView1.DataSource = Form1.fruit
        随着dataGridViewButtonColumn1
            。名称=ButtonCol
            .UseColumnTextForButtonValue = FALSE
        结束与
        如果不setupAlready然后
            DataGridView1.Columns.Add(dataGridViewButtonColumn1)
        结束如果
        对于我作为整数= 0-4
            DataGridView1.Rows(我).Cells(ButtonCol)。值=你好
        下一个
        setupAlready = TRUE
    结束小组

    私人小组DataGridView1_CellContentClick(发送者为对象,E作为DataGridViewCellEventArgs)_
                手柄DataGridView1.CellContentClick
        Debug.Print(的String.Format(列= {0},行= {1},COLNAME = {2},e.ColumnIndex,e.RowIndex,DataGridView1.Columns(e.ColumnIndex).Name点))
        如果(DataGridView1.Rows.Item(e.RowIndex).Cells(ButtonCol)。值是你好),然后
            DataGridView1.Rows.Item(e.RowIndex).Cells(ButtonCol)。值=再见
            DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen
        结束如果
    结束小组

    私人小组的button1_Click(发送者为对象,E作为EventArgs的)把手Button1.Click
        Me.Close()
    结束小组
末级
 

解决方案

我觉得你有导致这几件事情收敛。首先,表格类和应该被明确实例化。而不是 Form2.ShowDialog()做到这一点:

 使用FRM作为新窗体2创建实例
   frm.ShowDialog
   ' 做一点事
使用完对话也是一种资源
 
07版的excel无法加载加载项,加载时提示未安装,但是无法安装,该怎么办

使用 / .Dispose 不需要正常的形式,因为当你关闭它们,它们被处理掉。不是这样的对话,因为我们只是不寻常隐藏他们,所以我们可以从他们那里获取信息。

接下来,在Form_Load事件只叫你展示的形式第一次。请参见 MSDN :显示前一个形式首次出现

因此​​,通过再利用处理-的窗体2 非,心不是执行Load事件心不是调用,code。在Load事件。它应该工作正常,如果你处置,创造新形式的实例。顺便说一句这适用于所有形式的,不只是对话框。

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 with a DataGridViewButtonColumn.

Calling Form2 the first time works fine. But calling it a second time, the buttons don't have any text.

' 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

Code for the second form is:

' 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

解决方案

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.

阅读全文

相关推荐

最新文章