vba 如何使用 Visual Basic(正确)将 Form1.TextBox1.Text 传递给 Form2.TextBox2.Text?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22020095/
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
How do I pass Form1.TextBox1.Text to Form2.TextBox2.Text using Visual Basic (correctly)?
提问by meer2kat
This is a continuation of a question asked two years ago in this thread: VB6 equivalent of string.IsNullOrEmpty(I think.) A programmer recommended I use the String.IsNullorEmpty method, which I used this thread for to convert to Visual Basic, but I still couldn't get it to work.
这是两年前在此线程中提出的一个问题的延续: VB6 等效于 string.IsNullOrEmpty(我认为。)一位程序员建议我使用 String.IsNullorEmpty 方法,我使用该线程将其转换为 Visual Basic,但是我仍然无法让它工作。
The specifics of my question are here, including all current code: http://www.daniweb.com/software-development/visual-basic-4-5-6/threads/473930/passing-data-between-forms-in-vba
我的问题的细节在这里,包括所有当前代码:http: //www.daniweb.com/software-development/visual-basic-4-5-6/threads/473930/passing-data-between-forms-in -vba
Here is the gist of it, copied directly from the second link: So I'm trying to make a link between TextBox1.Text on Form1 to TextBox2.Text on Form 2. What I currently have is a line of code underneath my TextBox2_Change code reading: TextBox2 = Form1.TextBox1.Text
这是它的要点,直接从第二个链接复制: 所以我试图在 Form1 上的 TextBox1.Text 到 Form2 上的 TextBox2.Text 之间建立一个链接。我目前拥有的是我的 TextBox2_Change 代码下面的一行代码阅读:TextBox2 = Form1.TextBox1.Text
This ALMOST does what I want it to do. The only problem is that it is requiring me to input any character in to the TextBox2 when Form2 pops up before it displays.
这几乎可以完成我想要它做的事情。唯一的问题是当 Form2 在显示之前弹出时,它要求我在 TextBox2 中输入任何字符。
I'm trying to get that problem solved and then I'm eventually going to try to get it to chop off part of the file name until just the project file name displays.......but that's a whole different game I'll be playing. One step at a time.
我正在尝试解决这个问题,然后我最终会尝试让它砍掉部分文件名,直到只显示项目文件名......但那是一个完全不同的游戏我会玩 一步一步来。
Does anyone have any suggestions?
有没有人有什么建议?
采纳答案by Kashish Arora
Explanation
解释
You should write the code under the Form2_Load
event.
您应该在Form2_Load
事件下编写代码。
If you write the code under Textbox2_TextChanged
event, the code will be executed only when you type or delete something in Textbox2
(That is the same as Text being changed).
如果在Textbox2_TextChanged
事件下编写代码,则只有在其中键入或删除某些内容时才会执行代码Textbox2
(即与更改文本相同)。
Code and Example
代码和示例
Private Sub Form2_Load () Handles Mybase.Load
Textbox2.Text = Form1.Textbox1.Text
End Sub
Hope it works perfectly!
希望它完美运行!
回答by FredP
The behaviour you observe is normal : your TextBox2 is only updated (with the value from TextBox1) ... when you update it manually (_Change).
您观察到的行为是正常的:您的 TextBox2 仅更新(使用来自 TextBox1 的值)...当您手动更新它时(_Change)。
回答by Pritam Zope
Hey friend it very simple. you need not to add any kind of other functions just use dot(.) operator to access all components of Form1.
e.g.:
Form1.TextBox1.AppendText("hello")
嘿朋友很简单。您无需添加任何类型的其他函数,只需使用 dot(.) 运算符即可访问 Form1 的所有组件。例如:
Form1.TextBox1.AppendText("hello")
or you can read value from Form2 and insert it into Form1. e.g. :
或者您可以从 Form2 读取值并将其插入到 Form1 中。例如:
Dim txt As String=TextBox1.Text
Form1.TextBox1.AppendText(txt)