从动态创建的VB表单中获取控件的值

时间:2015-01-29 20:16:16

标签: vb.net

使用VB.Net 2010.我正在尝试创建一个半通用弹出窗体,我成功创建窗体并将值放在标签中并有一个按钮(这是简化的,它会更复杂)。当我单击按钮时,我想从弹出窗体中获取一个值,并将其放在调用表单的标签中。我似乎无法“看到”弹出窗口中的标签或其他任何内容,它仍然是一个有效的形式,尚未处理。我当然可以看到form1(调用表单)中的东西,但不是弹出窗口,要么返回“form1”,要么返回“insertPopup”的Form.Active,所以我认为它会起作用。我可以使用许多按钮并根据按钮调用sub,但是想法是多个选项,带有复选框和按钮,我可以创建复选框但不能引用它们或标签它不会工作。

` 导入System.Windows.Forms 公共类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`

1 个答案:

答案 0 :(得分:1)

方法从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)