Vb.Net - 访问另一种形式的控件中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16502177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vb.Net - Accessing text in controls on another form
提问by Chris Mays
I am fairly new to vb.net and would like to be able to access the value (such as .text on a textbox) from another an open form. In my application I open a form from my main form, and when I try to access the text in the controls on the main form I am unable to see the .text value on the control.
我对 vb.net 相当陌生,希望能够从另一个打开的表单访问值(例如文本框上的 .text)。在我的应用程序中,我从主窗体打开一个窗体,当我尝试访问主窗体上控件中的文本时,我无法看到控件上的 .text 值。
I can loop through all controls on the main form just fine, but when I want to see the actual values, all controls are empty. My controls such as text boxes and combo boxes are inside of a tab control and group boxes.
我可以很好地遍历主窗体上的所有控件,但是当我想查看实际值时,所有控件都是空的。我的控件(如文本框和组合框)位于选项卡控件和组框内。
Is there a way to make all .text or values on the open form available from the other open form?
有没有办法让打开的表单上的所有 .text 或值从另一个打开的表单可用?
Here is how I am looping through the controls on the main form.
这是我如何循环浏览主窗体上的控件。
Try
For Each Tp As TabPage In UserData.UserTabControl.TabPages
'Name of Tabcontrol is UserTabcontrol
For Each gbx As GroupBox In Tp.Controls
For Each ctrl As Control In gbx.Controls
If ctrl.Name = "UserName" Then
MsgBox(UserData.UserName.Text) 'Messagebox here is empty
End If
Next ctrl
Next gbx
Next Tp
Me.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Thanks in advance. Chris
提前致谢。克里斯
回答by Adrian
From the example you've given you already have access to a reference to your Control. Instead of going back to the Formand trying to access that control as a property of the Formyou could just cast your reference and call its Textproperty directly.
从您提供的示例中,您已经可以访问对Control. 而不是回到Form并试图访问该控件作为 的属性,Form您可以只转换引用并Text直接调用其属性。
If ctrl.Name = "UserName" Then
MsgBox(DirectCast(ctrl, TextBox).Text) 'Assuming your UserName control is a TextBox
End If
回答by ron tornambe
If you would like to reference controls on an open form, call it Form1: First add a Form1 property or variable to the calling form:
如果你想在一个打开的窗体上引用控件,称之为 Form1:首先向调用窗体添加一个 Form1 属性或变量:
Public Class Form2
Public Property f1 As Form1
...
Private Sub DoSomething()
MsgBox("Here's some text from Form1: " & f1.Textbox1.Text)
End Sub
End Class
In the callee form, set the Form2 property to the form object:
在被调用者表单中,将 Form2 属性设置为表单对象:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.f1 = Me
Form2.ShowDialog() ' or Form2.Show()
End Sub
End Class
You can then reference all Form1 objects from Form2 using the f1 property.
然后,您可以使用 f1 属性从 Form2 中引用所有 Form1 对象。
回答by zokaee hamid
By this command, I could change the amount of control in another form.
通过这个命令,我可以改变另一种形式的控制量。
My.Forms.myForm.labelControl.Text = "bela"
Please try this :
请试试这个:
MsgBox(My.Forms.UserData.UserName.Text)

