使用VB.Net2010。我试图创建一个半通用的弹出表单,我成功创建了该表单并将值放在标签中并具有一个按钮(此按钮简化了,会更加复杂)。当我单击按钮时,我想从弹出表单中获取一个值并将其放在调用表单的标签中。我似乎无法“看到”标签或弹出框中的其他任何内容,它仍然是有效形式,尚未处理。我当然可以在form1(调用表单)中看到这些内容,但是在弹出窗口中却看不到这些东西,要么是Me返回“ form1”,要么是Form.Active返回“ insertPopup”,因此我认为它可以工作。我可以让它与许多按钮一起使用,并根据按钮调用sub,但是这个想法是带有复选框和on button的多个选择,
导入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`
从ControlsCollection查找方法使用控件的Name属性搜索控件。如果您不命名控件,则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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句