我正试图弄清楚如何使用我在动态控件中创建的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
eventhandler触发子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#进行大脑转换。
答案 0 :(得分: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