我的问题是:为什么rabut1(单选按钮)得到焦点会产生rabut1.Click事件执行rabut_Click方法,但button1得到焦点却不产生button1.Click事件,不执行button1_Click()方法呢,代码如下。
using System;
using System.Windows.Forms;
using System.Drawing;class Form1:Form
{
private RadioButton rabut1;
private RadioButton rabut2;
Button button1;
public Form1()
{
FormSet();
}
public void FormSet()
{
button1=new Button();
rabut1=new RadioButton();
rabut2=new RadioButton();
//设置button1
button1.Text="OK";
//设置rabut1
rabut1.Location=new Point(10,20);
rabut1.Size=new Size(50,11);
rabut1.Text="123";
//rabut1.Checked=true;
rabut1.TabStop=false;
//设置rabut2
rabut2.Location=new Point(10,40);
rabut2.Size=new Size(50,11);
//rabut2.Checked=true;
rabut2.Text="abc";
//添加控件
Controls.Add(rabut1);
Controls.Add(rabut2);
Controls.Add(button1);
//添加button1事件
button1.Click+=new EventHandler(this.button1_Click);
button1.Focus();
//RadioButton单击事件
rabut1.Click+=new EventHandler(this.rabut_Click);
rabut2.Click+=new EventHandler(this.rabut_Click);
MessageBox.Show("小日本去死");
if (rabut1.Checked==false)
MessageBox.Show("rabut1 没被选中");
}
private void button1_Click(object sender,EventArgs e)
{
this.rabut1.Focus();// rabut1(值为123的单选按钮)得到焦点就会触发Click事件
rabut1.Checked=false;
}
private void rabut_Click(object sender,EventArgs e)
{
if (sender==rabut1)
MessageBox.Show(rabut1.Text);
else if (sender==rabut2)
MessageBox.Show(rabut2.Text);
} public static void Main()
{
Application.Run(new Form1());
}
}
using System;
using System.Windows.Forms;
using System.Drawing;class Form1:Form
{
private RadioButton rabut1;
private RadioButton rabut2;
Button button1;
public Form1()
{
FormSet();
}
public void FormSet()
{
button1=new Button();
rabut1=new RadioButton();
rabut2=new RadioButton();
//设置button1
button1.Text="OK";
//设置rabut1
rabut1.Location=new Point(10,20);
rabut1.Size=new Size(50,11);
rabut1.Text="123";
//rabut1.Checked=true;
rabut1.TabStop=false;
//设置rabut2
rabut2.Location=new Point(10,40);
rabut2.Size=new Size(50,11);
//rabut2.Checked=true;
rabut2.Text="abc";
//添加控件
Controls.Add(rabut1);
Controls.Add(rabut2);
Controls.Add(button1);
//添加button1事件
button1.Click+=new EventHandler(this.button1_Click);
button1.Focus();
//RadioButton单击事件
rabut1.Click+=new EventHandler(this.rabut_Click);
rabut2.Click+=new EventHandler(this.rabut_Click);
MessageBox.Show("小日本去死");
if (rabut1.Checked==false)
MessageBox.Show("rabut1 没被选中");
}
private void button1_Click(object sender,EventArgs e)
{
this.rabut1.Focus();// rabut1(值为123的单选按钮)得到焦点就会触发Click事件
rabut1.Checked=false;
}
private void rabut_Click(object sender,EventArgs e)
{
if (sender==rabut1)
MessageBox.Show(rabut1.Text);
else if (sender==rabut2)
MessageBox.Show(rabut2.Text);
} public static void Main()
{
Application.Run(new Form1());
}
}
双击Button会自动产生事件,或者在控件的属性中的事件(有闪电的符号)将点击事件和后台事件对应。
而button1.Click却不会触发button1.Click事件
如果你没有定义的话,是不可能触发的。
试问如果raido得到焦点不触发Click事件,那什么时候触发吗?在它被选中时吗?我是用rabut1.Focsu();得到焦点的,不是靠鼠标点击的得到的,那为什么TextBox.Focus();和Button.Focus();不能触发相应事件呢,只有RadioButton能,麻繁你给解释一下吧
当焦点转移到RADIO上时,首先触发CHECKEDCHANGE事件检查是否选中,如果选中则触发其CLICK事件,然后触发ENTER事件,这是RADIO和CHECKBOX特有的事件.