1

我试图弄清楚如何使用我在动态控件中创建的 AddHandler 将信息从一个表单传递到另一个表单。

我有一个类似的循环

Dim I As Integer

For I = 0 To 10
    Dim gbNew As New GroupBox()
    Dim pbNew As New PictureBox()
    Dim llbNew As New Label()
    Dim tlbNew As New Label()
    Dim olbNew As New Label()
    Dim slbNew As New Label()
    Dim wlbNew As New Label()

    UserName = dt.Rows(I)("UserName").ToString()
    Status = dt.Rows(I)("LastJobType").ToString()
    JobType = dt.Rows(I)("LastJobType").ToString()
    LLocation = dt.Rows(I)("LastLocation").ToString()
    TimeIn = dt.Rows(I)("LogInTime")
    TimeOut = dt.Rows(I)("LogOutTime")
    FlowLayoutPanel1.Controls.Add(gbNew)

    gbNew.Controls.Add(llbNew)
    llbNew.Visible = True
    llbNew.Text = LLocation
    llbNew.Font = New Font(llbNew.Font.FontFamily, 6.5)
    llbNew.Location = New System.Drawing.Point(3, 25)
    llbNew.BorderStyle = BorderStyle.None
    llbNew.TextAlign = ContentAlignment.MiddleLeft
    llbNew.Size = New Size(80, 15)

    gbNew.Size = New System.Drawing.Size(270, 80)
    'gbNew.BackColor = System.Drawing.Color.Silver
    gbNew.Visible = True
    gbNew.Text = UserName & " " & I + 1

    AddHandler gbNew.Click, AddressOf ShowForm
Next

事件处理程序触发一个子 ShowForm:

Private Sub ShowForm()
    Details.Show()
End Sub

这反过来会弹出一个表单,但我不知道如何将一些需要的信息从动态生成的控件传递到循环外的静态控件。

我在一个表单中使用了一个静态控件:

label1.text = "something"

我打开了新表格,我可以使用类似 dim info as string = form1.label.text. 但由于它是动态的,所以我没有 label1.text。相反,我有一个llbNew.Text似乎无法从 form2 调用的东西 :(

如何将信息从 form1 的动态控件传递到 form2?

请将此保留到 VB.NET,而不是 C#,因为我几乎不了解 VB.NET,更不用说尝试从我零知识的 C# 进行大脑转换。

4

1 回答 1

1

这是你可以采取的方向。我希望很清楚:

For I = 0 To 10
    (...)
    gbNew.Text = UserName & " " & I + 1
    gbNew.Tag = dt.Rows(I) ' Any information that you need here 

    AddHandler gbNew.Click, AddressOf ShowForm  '(No changes here)
Next 

' Use the appropriate Event Handler signature @ the handler Sub
Private Sub ShowForm(sender as Object, e as EventArgs)
    Dim groupBoxClicked as GroupBox = TryCast(sender, GroupBox)
    If groupBoxClicked IsNot Nothing
        Dim detailsForm as New Details()
        detailsForm.ParentInformation = groupBoxClicked.Tag
        detailsForm.ShowDialog()
    End If
End Sub

(...)

Public Class Details ' Your Details Form
    Public Property ParentInformation as DataRow

End Class
于 2012-10-03T00:55:46.923 回答