简体   繁体   English

将参数从动态控件传递到VB.NET中的AddHandler

[英]Passing arguments from a dynamic control to AddHandler in VB.NET

I'm trying to figure out how to pass information from one form to another using an AddHandler that I create in a dynamic control. 我试图弄清楚如何使用我在动态控件中创建的AddHandler将信息从一种形式传递到另一种形式。

I have a loop something like 我有一个类似的循环

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

The eventhandler fires off a sub ShowForm: 事件处理程序会触发一个子ShowForm:

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

This in turn pops up a form, but I can't figure out how to pass a few needed bits of information from the dynamic generated control to a static control outside the loop. 反过来,这会弹出一个表单,但是我无法弄清楚如何将一些所需的信息从动态生成的控件传递到循环外的静态控件。

I was using a static control in a form: 我在以某种形式使用静态控件:

label1.text = "something"

And I opened the new form, and I could read that into the new form using something like dim info as string = form1.label.text . 然后我打开了新表单,可以使用类似dim info as string = form1.label.text将其读入新表单。 But since it is dynamic, I don't have a label1.text. 但是由于它是动态的,所以我没有label1.text。 Instead, I have a llbNew.Text which seems to be something I can't call from form2 :( 相反,我有一个llbNew.Text ,这似乎是我不能从form2调用的东西:(

How can I pass information from form1's dynamic control to form2? 如何将信息从Form1的动态控件传递到Form2?

Please keep this to VB.NET, not C#, as I barely understand VB.NET let alone trying to brain convert from C# which I have zero knowledge of. 请保留到VB.NET,而不是C#,因为我几乎不了解VB.NET,更不用说试图从我对零知识的C#进行大脑转换了。

Here's a direction you could take. 这是您可以采取的方向。 I hope it is clear: 我希望很清楚:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM