简体   繁体   English

如果文本框为空,如何用文本填充文本框?

[英]How do I fill a textbox with text if it is empty?

I use a TextChanged-EventHandler I wrote a program in c# that creates a new TextBox on every button1_Click event Now,I want each new TextBox (which was created) to show typed text. 我使用TextChanged-EventHandler在C#中编写了一个程序,该程序在每个button1_Click事件上创建一个新的TextBox 。现在,我希望每个新的TextBox (已创建)都显示键入的文本。 How can I do this with EventHandler(TextChanged)? 如何使用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 =           
    }

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

the sender of your object should be the textbox. 对象的发送者应该是文本框。 There you can get the text you want: 在那里,您可以获得所需的文本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM