比如,在点击button之后动态的加一个textbox,可以这样
private void button1_Click(object sender, System.EventArgs e)
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.Controls.Add(this.textBox1);
this.textBox1.Location = new System.Drawing.Point(40, 128);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
}
这样可以响应textchanged事件
private void button1_Click(object sender, System.EventArgs e)
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.Controls.Add(this.textBox1);
this.textBox1.Location = new System.Drawing.Point(40, 128);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
}
这样可以响应textchanged事件
private System.Windows.Forms.TextBox myTextbox;2 放一个button到form上,然后在button的click事件中写上
private void button1_Click(object sender, System.EventArgs e)
{
//创建一个新的textbox control
this.myTextbox=new TextBox();
//position the textbox location
//left=30
//right=20
myTextbox.Location=new Point(30,20);
//add the textbox to this form's control collection
this.Controls.Add(myTextbox);
//assign the textchange event
this.myTextbox.KeyDown+=new KeyEventHandler(this.myTextbox_KeyDown);
}
3 编写一个私有的函数,名字为myTextbox_KeyDown
private void myTextbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyValue==13)
{
ShowExclamation(myTextbox.Text);
}
}
这样就可以了。
textchanged也一样的做法。