2013-07-16 110 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; 
    } 
}