C#中单选按钮组如何编写单击事件

技术科 2008-10-09 10:46:39
当前有一个groupBox1,内含3个radioButton(分别为radioButton1,radioButton2,radioButton3),构成一个单选按钮组,且radioButton1默认为选中状态,要求实现效果为:
单击其中任何一个按钮,都会弹出一个MessageBox("单击了第"+X+"个按钮"),要求最少的代码量(不要给每个单选按钮都编写一个click事件)这段代码如何写?
...全文
3475 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiufeng0723 2011-06-28
  • 打赏
  • 举报
回复
为了获得选中的答案值,首先生成其中一个的单选按钮的Click事件处理方法rdoA_Click(),然后将它改名为rdoAnswer_Click()。而后在“属性”窗口中找到其他的单选按钮将他的Click事件处理指向第一个Click事件的方法,也就是改过名字的那个方法“rdoAnswer()”
也就是说一组单选按钮就写一个Click事件,然后將它改下名字,把剩下的单选按钮全都指向一个方法!



pricate void rdoAnswer_Click(object sender,EvevtArgs e)
{
//將sender转换为单选按钮,并获取选中蛋选按钮的值
string answer = ((radioButton)sender).Text;
MessageBox.Show("你选择了"+answer,"提示",MessageBoxButtons.OK,MessageBOxIcon.Information);

//answer是单选按钮的值!
}
tomyfolder 2010-09-03
  • 打赏
  • 举报
回复
哪一个是当程序刚运行时都不加载被选中呀?
mahayu1982 2008-10-16
  • 打赏
  • 举报
回复
楼主的问题用这个方法解:设立一个中间变量i,并且将radioButton2的委托事件放在其他位置加载,就能实现要求!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WFTest
{
public partial class Form1 : Form
{
int i = 0;
public Form1()
{
InitializeComponent();
this.radioButton1.Click += new System.EventHandler(this.radioButton1_Click);
this.radioButton3.Click += new System.EventHandler(this.radioButton1_Click);

}

private void radioButton1_Click(object sender, EventArgs e)
{
switch (((RadioButton)sender).Name)
{
case "radioButton1":
MessageBox.Show("按了第一个radioButton");
i++;
break;

case "radioButton2":
MessageBox.Show("按了第二个radioButton");
i = 2;
break;


case "radioButton3":
MessageBox.Show("按了第三个radioButton");
i++;
break;

default:
break;
}


if (i==1)
{
radioButton2.Click += new EventHandler(radioButton1_Click);
}
}
}
}
justindreams 2008-10-13
  • 打赏
  • 举报
回复
这样做:
在Form的load方法或者构造器里面这样做:
先选中你需要默认的选项,比如radioButton2
然后再去预定事件



private void CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show( ((RadioButton)sender).Name );
}

private void Form1_Load(object sender, EventArgs e)
{
this.radioButton2.Checked=true; //此处先选中默认选项!!
radioButton1.CheckedChanged += new EventHandler(CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(CheckedChanged);
radioButton3.CheckedChanged += new EventHandler(CheckedChanged);
}

justindreams 2008-10-13
  • 打赏
  • 举报
回复
唉,先选中,再预订事件!!!!!
你先预订事件再去选中的话,必然会在你点击之前出现MessageBox的。
止戈而立 2008-10-13
  • 打赏
  • 举报
回复
全局变量:
private bool flag=false;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
flag=true;

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}


private void rk(object sender, EventArgs e)
{
if(falg==false)return;
switch (((RadioButton)sender).Name)
{
case "radioButton1":
MessageBox.Show("单击了第1个按钮");
break;
case "radioButton2":
MessageBox.Show("单击了第2个按钮");
break;
case "radioButton3":
MessageBox.Show("单击了第3个按钮");
break;

}
}
zbking 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ZengHD 的回复:]
private void CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show( ((RadioButton)sender).Name );
}

private void Form1_Load(object sender, EventArgs e)
{
radioButton1.CheckedChanged += new EventHandler(CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(CheckedChanged);
r…
[/Quote]

正解,帮顶
止戈而立 2008-10-13
  • 打赏
  • 举报
回复
我的想法是设置一个bool变量,初始值为false,程序启动后才置为true


private void rk(object sender, EventArgs e)
{
if(falg==false)return;
switch (((RadioButton)sender).Name)
{
case "radioButton3":
MessageBox.Show("单击了第3个按钮");
break;
case "radioButton2":
MessageBox.Show("单击了第二个按钮");
break;
case "radioButton1":
MessageBox.Show("单击了第1个按钮");
break;

}
}

止戈而立 2008-10-13
  • 打赏
  • 举报
回复
LZ的意思是,因为其中一个选项默认为选中,程序一启动就会触发Click事件..
bonnibell 2008-10-13
  • 打赏
  • 举报
回复
俺明白楼主的意思,
刚测试了一下
没有问题啊

定义中

//
// panel1
//
this.panel1.Controls.Add(this.radioButton3);
this.panel1.Controls.Add(this.radioButton2);
this.panel1.Controls.Add(this.radioButton1);
this.panel1.Location = new System.Drawing.Point(35, 141);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(246, 100);
this.panel1.TabIndex = 5;
//
// radioButton3
//
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(35, 81);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(95, 16);
this.radioButton3.TabIndex = 2;
this.radioButton3.Text = "radioButton3";
this.radioButton3.UseVisualStyleBackColor = true;
this.radioButton3.Click += new System.EventHandler(this.radioButton_Click);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Checked = true;
this.radioButton2.Location = new System.Drawing.Point(35, 47);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(95, 16);
this.radioButton2.TabIndex = 1;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "radioButton2";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.Click += new System.EventHandler(this.radioButton_Click);
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(35, 13);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(95, 16);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.Click += new System.EventHandler(this.radioButton_Click);

事件中
private void radioButton_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show("aaaaa");
}
if (radioButton2.Checked == true)
{
MessageBox.Show("bbbb");
}
if (radioButton3.Checked == true)
{
MessageBox.Show("cccc");
}

}
slin60 2008-10-13
  • 打赏
  • 举报
回复
不是没人能解答,而是你根本就没看明白别人给你的解答
技术科 2008-10-13
  • 打赏
  • 举报
回复
没人能解答么?顶上去.
技术科 2008-10-11
  • 打赏
  • 举报
回复
以上所说的都只能实现一半要求,
(C#桌面程序)
1.构建一个单选按钮组(3个radioButton(分别为radioButton1,radioButton2,radioButton3))
2.将任意一个按钮的checked设为true
3.编写按钮单击事件:按下任意一个按钮时,显示"按下了第几个按钮"!
4.关键一步!启动程序,在单击任意一个按钮之前,不触发任何事件!!
以上就是这个程序的要求,目前还没有能完全实现的!加分求解!
icebear2 2008-10-10
  • 打赏
  • 举报
回复
你是应用在winform中吗?还是在web里阿?
icebear2 2008-10-10
  • 打赏
  • 举报
回复
?radioButton在一个groupBox里只能有一个true阿~
应该是全部设为false,选择时再触发事件吧?
技术科 2008-10-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 icebear2 的回复:]
private void radioButton_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show("aaaaa");
}
if (radioButton2.Checked == true)
{
MessageBox.Show("bbbb");
}
if (radioButton3.Checked == true)
{

[/Quote]

还有一个bug!!启动程序后就弹出"bbbb"(radioButton2的Checked 默认设为true),
我的要求是:任意一个按钮默认设为true,启动程序,在单击任意按钮之前,不触发任何事件!!
zhangjie_it 2008-10-10
  • 打赏
  • 举报
回复
你没有click的时候,是不会触发click事件的!
zhangjie_it 2008-10-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jsship 的回复:]
修改后的代码如下(rk函数已绑定到各按钮的click事件上):

public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void rk(object sender, EventArgs e)
{
switch (((RadioButton)sender).Text)
{
case "radioButton3":
MessageBox.Show(…
[/Quote]

使用click事件是不会出现你说的那种情况的!
ZengHD 2008-10-09
  • 打赏
  • 举报
回复
private void CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show( ((RadioButton)sender).Name );
}

private void Form1_Load(object sender, EventArgs e)
{
radioButton1.CheckedChanged += new EventHandler(CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(CheckedChanged);
radioButton3.CheckedChanged += new EventHandler(CheckedChanged);
}
zhangjie_it 2008-10-09
  • 打赏
  • 举报
回复
要使用委托!
radiobutton_click(object sender,System.Eventargs e)
{
switch((radiobutton)sender.text)
{
case "radiobutton1":
messagebox.show("单击了第一个按钮");
   break;
case "radiobutton2":
messagebox.show("单击了第二个按钮");
break;
..................
}
}
radiobutton1.click+=new SYstem.EventHandler(this.raidobutton_click);
radiobutton2.click+=new SYstem.EventHandler(this.raidobutton_click);
radiobutton3.click+=new SYstem.EventHandler(this.raidobutton_click);
加载更多回复(5)

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧