以不同的GUI形式连接两个文本框 [英] Connect two text box in different GUI form
问题描述
亲爱的朋友们
我想做这样的事情请帮帮我...
Form1包含TextBox
Form2包含TextBox
我想要Form1 TextBox .text = Form2 TextBox.tex;
请帮助
Dear Friends
I want to do somthing like this please help me...
Form1 Contain TextBox
Form2 Contain TextBox
I want Form1 TextBox.text= Form2 TextBox.tex;
Please help
推荐答案
最简单的方法是在Form2处理Form2中创建一个事件,Form1访问Form2中的一个Property(这假设Form2是在Form1中创建的,所以它的实例是可用的。
在Form2 TextBox.TextCanged事件中,发出新事件的信号。该属性将准备好TextBox的值并将其作为字符串返回。
Form1新事件处理程序,从Form2实例属性中读取值并将Form1文本框设置为新值。
完成!
事件很简单:
The easiest way to do this is to create an event in Form2 which Form1 handles, and a Property in Form2 which Form1 accesses (thisa assumes that Form2 was created in Form1, so it''s instance is available.
In the Form2 TextBox.TextCanged event, signal the new event. The Property will ready the value of the TextBox and return it as a string.
In the Form1 new event handler, read the value from the Form2 instance property and set the Form1 Textbox to the new value.
Done!
The event is easy:
/// <summary>
/// Event to indicate Text changed
/// </summary>
public event EventHandler NewTextAvailable;
/// <summary>
/// Called to signal to subscribers that Text changed
/// </summary>
/// <param name="e"></param>
protected virtual void OnNewTextAvailable(EventArgs e)
{
EventHandler eh = NewTextAvailable;
if (eh != null)
{
eh(this, e);
}
}
该属性也很简单:
The Property is also simple:
public string GetNewText
{
get { return myTextBox.Text; }
}
Form1事件处理程序也很简单:
The Form1 event handler is also simple:
void Form1_NewTextAvailable(object sender, EventArgs e)
{
Form2 instance = sender as Form2;
if (instance != null)
{
from1TextBox.Text = instance.NewText;
}
}
所以你要做的就是在创建Form2实例时添加处理程序,并在Form2 TextBox中调用OnNewTextAvailable。 TextChanged事件。
添加Form2事件信号 - OriginalGriff [/ edit]
So all you have to do is add the handler when you create the Form2 instance, and call OnNewTextAvailable in the Form2 TextBox.TextChanged event.
[edit]Added Form2 event signalling - OriginalGriff[/edit]
这篇关于以不同的GUI形式连接两个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!