[注意]走过路过不要错过啊,焦点与事件问题,请朋友们进来看看,帮忙解决
我的问题是:为什么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());
}
}