2013-07-16 113 views
0

我使用TextChanged-EventHandler 我写在C#程序创建的每一个button1_Click事件 新TextBox现在,我想每一个新TextBox(这是创建)显示键入的文本。 我如何用EventHandler(TextChanged)做到这一点?如果文本框为空,如何填充文本框?

namespace WindowsFormsApplication5 
{ 
    public partial class Form1 : Form 
    { 
     Int32 i = 1; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     TextBox c = new TextBox(); 
     this.Controls.Add(c); 
     c.Name = "x" + i.ToString(); 
     c.Left = 3; 
     c.Top = 30 * i; 
     i++; 
     c.TextChanged += new EventHandler(c_TextChanged); 


    } 

    void c_TextChanged(object sender, EventArgs e) 
    { 
     textBox1.Text =   
    } 

} 
} 
+0

我不确定我是否理解正确。你的意思是说,每次你输入文字时,所有文本框都应该显示这些输入? – Kooki

+0

不是所有的人,只有第一个文本框 那 表格上有! – sara

+0

好吧,然后Clemens/Herms的答案应该可以帮助你;-),请接受一个答案来标记问题已解决,如果它适合你 – Kooki

回答

4
void c_TextChanged(object sender, EventArgs e) 
{ 
    textBox1.Text = ((TextBox)sender).Text; 
} 
0

你的对象的发送者应该是文本框。在那里你可以得到你想要的文字:

void c_TextChanged(object sender, EventArgs e) 
{ 
    TextBox box = sender as TextBox; 
    if (box != null) 
    { 
     textBox1.Text = box.Text; 
    } 
}