有100个按钮,是一个控件数组,如何在这100个按钮的单击事件里写入相同的程序?
button[100],控件数组
我想
private void button[0]_Click(object sender, System.Windows.Forms.PaintEventArgs e)
{
message.show “0”
}private void button[1]_Click(object sender, System.Windows.Forms.PaintEventArgs e)
{
message.show “1”
}private void button[2]_Click(object sender, System.Windows.Forms.PaintEventArgs e)
{
message.show “2”
}
.
.
.
private void button[99]_Click(object sender, System.Windows.Forms.PaintEventArgs e)
{
message.show “99”
}
上面程序如何简化?谢谢!
button[100],控件数组
我想
private void button[0]_Click(object sender, System.Windows.Forms.PaintEventArgs e)
{
message.show “0”
}private void button[1]_Click(object sender, System.Windows.Forms.PaintEventArgs e)
{
message.show “1”
}private void button[2]_Click(object sender, System.Windows.Forms.PaintEventArgs e)
{
message.show “2”
}
.
.
.
private void button[99]_Click(object sender, System.Windows.Forms.PaintEventArgs e)
{
message.show “99”
}
上面程序如何简化?谢谢!
来判断按钮
{
// button1
//
for(int i=0,count=5;i<count;i++)//count为你要添加的控件总数
{
Button[] arrayBtn=new Button[5];
Button temBtn=new Button();
arrayBtn[i]= temBtn; this.Controls.Add(arrayBtn[i]);
arrayBtn[i].Location = new System.Drawing.Point(96, 96+i*25);
arrayBtn[i].Name = "button"+Convert.ToString(i);
arrayBtn[i].TabIndex = 0;
arrayBtn[i].Text = "button"+Convert.ToString(i); arrayBtn[i].Click += new System.EventHandler(this.buttonAll_Click); }
} private void buttonAll_Click(object sender, System.EventArgs e)
{ MessageBox.Show("hello world");//此处还可以根据sender来进行复杂的判断
}
在oncommand事件中
if(e.CommandName =="1")
//处理内容
if(e.CommandName=="2")
//处理内容将所有的内容关连到同一个command处理函数
谢谢大家!
请大家不用回答了。
{
TextBox box = (TextBox)sender;
MessageBox.Show(this,"you choose " + box.Name + " and you keypress " + e.KeyChar.ToString() ,"try" ,MessageBoxButtons.OK );
} 选定textBox1,按F4显示其属性页,选择"Events"按钮(就是那个小闪电),列出了textBox1的所有事件,在keypress里写我们想让他相应的事件的名字--textBox_KeyPress,这样,当textBox1产生keypress事件时,他相应的是textBox_KeyPress这个我们自定义的事件。同样,对其它的textBox控件也做如上的属性设置。这样,5个textBox同时响应一个keypress事件,而可以通过object sender的不同,可以加以区分。这样就达到了数组的效果。大家好快啊!