从动态创建的 VB 表单控件中获取值
get value from control on VB form that was created on the fly
使用 VB.Net 2010。我正在尝试创建一个半通用的弹出表单,我成功地创建了表单并将值放入标签中并有一个按钮(这是简化的,它会更复杂) .当我单击按钮时,我想从弹出窗体中获取一个值并将其放在调用窗体的标签中。我似乎无法 "see" 标签或弹出窗口中的任何其他内容,它仍然是有效的形式,尚未处理。我当然可以看到 form1(调用表单)中的内容,但看不到弹出窗口,无论是 Me which returns "form1" 还是 Form.Active which returns "insertPopup" 所以我认为它会起作用。我可以让它与许多按钮一起工作,并根据按钮调用子程序,但我的想法是使用复选框和按钮进行多项选择,我可以创建复选框,但无法引用它们或标签,它将无法工作。
`
进口 System.Windows.Forms
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
createPopup(Me, "next", "prior") 'this will place 2 values on the popup so we can make a choice
End Sub
Private Sub createPopup(callingFrmName As Form, Optional lblN As String = Nothing, Optional lblP As String = Nothing)
Dim insertPopup As New Form
'create the popup, set the size, center the screen
insertPopup.Size = New System.Drawing.Size(300, 400)
insertPopup.StartPosition = FormStartPosition.CenterScreen
insertPopup.Name = "insertpopup"
insertPopup.Show()
'create buttons, labels, textboxes, etc.
Dim acceptNextButton As New Button
Dim acceptPriorButton As New Button
Dim cancelButton As New Button
Dim lblCallingForm As New Label
Dim lblnext As New Label
Dim lblprior As New Label
'set the sizes, text and other parts of the controls
lblCallingForm.Location = New System.Drawing.Point(10, 25)
lblCallingForm.Size = New System.Drawing.Size(185, 24)
lblCallingForm.Text = "Calling Form Name : " & callingFrmName.Name.ToString
lblnext.Location = New System.Drawing.Point(10, 100)
lblnext.Size = New System.Drawing.Size(185, 24)
lblnext.Text = lblN
lblprior.Location = New System.Drawing.Point(50, 205)
lblprior.Size = New System.Drawing.Size(185, 24)
lblprior.Text = lblP
acceptNextButton.Location = New System.Drawing.Point(200, 100)
acceptNextButton.Text = "Insert"
acceptNextButton.Size = New System.Drawing.Size(85, 24)
acceptNextButton.TabIndex = 1
acceptPriorButton.Location = New System.Drawing.Point(100, 325)
acceptPriorButton.Text = "Insert"
acceptPriorButton.Size = New System.Drawing.Size(85, 24)
acceptPriorButton.TabIndex = 1
cancelButton.Location = New System.Drawing.Point(190, 325)
cancelButton.Text = "Cancel"
cancelButton.Size = New System.Drawing.Size(85, 24)
cancelButton.TabIndex = 2
'now really create them, show them
insertPopup.Controls.Add(lblnext)
insertPopup.Controls.Add(lblprior)
insertPopup.Controls.Add(lblCallingForm)
insertPopup.Controls.Add(acceptNextButton)
insertPopup.Controls.Add(cancelButton)
'add Handlers so users can click on buttons.
AddHandler acceptNextButton.Click, AddressOf acceptNextButton_Click
AddHandler cancelButton.Click, AddressOf cancelButton_Click
End Sub
Private Sub acceptNextButton_Click(sender As System.Object, e As System.EventArgs)
Dim callingForm As Form = CType(CType(lblCall, Label).Parent, Form) 'get the name of the calling form so we can put values back in.
Dim frmInsertPopup As Form = Form.ActiveForm
'Dim lblP As Object = Form.ActiveForm.lblprior.text 'NOPE, also tried insertpopup.lblprior.text
Dim c As Control() = callingForm.Controls.Find("lblCall", True) 'find the control on the calling form set it.
If c.Count > 0 Then 'Check to see if we got a match
CType(c(0), Label).Text = callingForm.Name.ToString
End If 'this works to put FIXED values into calling form, fixed like "HELP" or the callingform variable BUT NOT something from popup form
Dim x As Control() = Form.ActiveForm.Controls.Find("lblprior", True) 'NEVER FOUND
If x.Count > 0 Then
callingForm.Controls("Label2").Text = CType(x(0), Label).Text 'NEVER GETS HERE and I don't understand why not.
End If
'callingForm.Controls("Label2").Text = "This worked also"
'callingForm.Controls("Label2").Text = CType(Form.ActiveForm.Controls("lblprior"), Label).Text 'this does NOT work
CType(CType(sender, Button).Parent, Form).Close()
End Sub`
从 ControlsCollection 中查找的方法使用控件名称 属性 搜索控件。如果您不命名您的控件,Find 方法将失败
lblprior.Name = "lblprior"
lblprior.Location = New System.Drawing.Point(50, 205)
lblprior.Size = New System.Drawing.Size(185, 24)
lblprior.Text = lblP
' Now this should work
Dim x As Control() = Form.ActiveForm.Controls.Find("lblprior", True)
使用 VB.Net 2010。我正在尝试创建一个半通用的弹出表单,我成功地创建了表单并将值放入标签中并有一个按钮(这是简化的,它会更复杂) .当我单击按钮时,我想从弹出窗体中获取一个值并将其放在调用窗体的标签中。我似乎无法 "see" 标签或弹出窗口中的任何其他内容,它仍然是有效的形式,尚未处理。我当然可以看到 form1(调用表单)中的内容,但看不到弹出窗口,无论是 Me which returns "form1" 还是 Form.Active which returns "insertPopup" 所以我认为它会起作用。我可以让它与许多按钮一起工作,并根据按钮调用子程序,但我的想法是使用复选框和按钮进行多项选择,我可以创建复选框,但无法引用它们或标签,它将无法工作。
` 进口 System.Windows.Forms Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
createPopup(Me, "next", "prior") 'this will place 2 values on the popup so we can make a choice
End Sub
Private Sub createPopup(callingFrmName As Form, Optional lblN As String = Nothing, Optional lblP As String = Nothing)
Dim insertPopup As New Form
'create the popup, set the size, center the screen
insertPopup.Size = New System.Drawing.Size(300, 400)
insertPopup.StartPosition = FormStartPosition.CenterScreen
insertPopup.Name = "insertpopup"
insertPopup.Show()
'create buttons, labels, textboxes, etc.
Dim acceptNextButton As New Button
Dim acceptPriorButton As New Button
Dim cancelButton As New Button
Dim lblCallingForm As New Label
Dim lblnext As New Label
Dim lblprior As New Label
'set the sizes, text and other parts of the controls
lblCallingForm.Location = New System.Drawing.Point(10, 25)
lblCallingForm.Size = New System.Drawing.Size(185, 24)
lblCallingForm.Text = "Calling Form Name : " & callingFrmName.Name.ToString
lblnext.Location = New System.Drawing.Point(10, 100)
lblnext.Size = New System.Drawing.Size(185, 24)
lblnext.Text = lblN
lblprior.Location = New System.Drawing.Point(50, 205)
lblprior.Size = New System.Drawing.Size(185, 24)
lblprior.Text = lblP
acceptNextButton.Location = New System.Drawing.Point(200, 100)
acceptNextButton.Text = "Insert"
acceptNextButton.Size = New System.Drawing.Size(85, 24)
acceptNextButton.TabIndex = 1
acceptPriorButton.Location = New System.Drawing.Point(100, 325)
acceptPriorButton.Text = "Insert"
acceptPriorButton.Size = New System.Drawing.Size(85, 24)
acceptPriorButton.TabIndex = 1
cancelButton.Location = New System.Drawing.Point(190, 325)
cancelButton.Text = "Cancel"
cancelButton.Size = New System.Drawing.Size(85, 24)
cancelButton.TabIndex = 2
'now really create them, show them
insertPopup.Controls.Add(lblnext)
insertPopup.Controls.Add(lblprior)
insertPopup.Controls.Add(lblCallingForm)
insertPopup.Controls.Add(acceptNextButton)
insertPopup.Controls.Add(cancelButton)
'add Handlers so users can click on buttons.
AddHandler acceptNextButton.Click, AddressOf acceptNextButton_Click
AddHandler cancelButton.Click, AddressOf cancelButton_Click
End Sub
Private Sub acceptNextButton_Click(sender As System.Object, e As System.EventArgs)
Dim callingForm As Form = CType(CType(lblCall, Label).Parent, Form) 'get the name of the calling form so we can put values back in.
Dim frmInsertPopup As Form = Form.ActiveForm
'Dim lblP As Object = Form.ActiveForm.lblprior.text 'NOPE, also tried insertpopup.lblprior.text
Dim c As Control() = callingForm.Controls.Find("lblCall", True) 'find the control on the calling form set it.
If c.Count > 0 Then 'Check to see if we got a match
CType(c(0), Label).Text = callingForm.Name.ToString
End If 'this works to put FIXED values into calling form, fixed like "HELP" or the callingform variable BUT NOT something from popup form
Dim x As Control() = Form.ActiveForm.Controls.Find("lblprior", True) 'NEVER FOUND
If x.Count > 0 Then
callingForm.Controls("Label2").Text = CType(x(0), Label).Text 'NEVER GETS HERE and I don't understand why not.
End If
'callingForm.Controls("Label2").Text = "This worked also"
'callingForm.Controls("Label2").Text = CType(Form.ActiveForm.Controls("lblprior"), Label).Text 'this does NOT work
CType(CType(sender, Button).Parent, Form).Close()
End Sub`
从 ControlsCollection 中查找的方法使用控件名称 属性 搜索控件。如果您不命名您的控件,Find 方法将失败
lblprior.Name = "lblprior"
lblprior.Location = New System.Drawing.Point(50, 205)
lblprior.Size = New System.Drawing.Size(185, 24)
lblprior.Text = lblP
' Now this should work
Dim x As Control() = Form.ActiveForm.Controls.Find("lblprior", True)