作者james732 (紫雁)
站内C_Sharp
标题Re: [问题] radiobutton的简单问题
时间Wed Jun 18 03:17:57 2008
※ 引述《ThreeDay0905 (三天)》之铭言:
: 这应该是很基础的问题
: 不过翻书或着爬文都找不到
: 就是我因为版面的关系,没有用radiobuttonlist
: 只用了radiobutton,然後将其group设为一样
: 这样我在程式中要如何使用呢
: 因为每个radiobutton的id都不一样
: 我想要知道使用者选择了哪个项目
: 没办法跟radiobuttonlist一样,直接下 radiobuttonlist.selectvalue = xxxxx
: 假如在我有多个不同名称的radiobutton,都属於avg群组的话
: 我要怎麽取得现在使用者选择了哪个radiobutton
: 或是要怎麽用程式码去指定哪个radiobutton被选取呢
我想到的方法是
1.把每个 RadioButton 的 CheckedChanged 事件都交给同一个 handle function
2.在 handle function 里,藉由参数 sender 转型来得知是哪个 RadioButton 被选择
像这样:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
if (rb.Checked == true)
{
textBox1.Text = rb.Name;
// TextBox 会显示「被选取」的 RadioButton 的 Name
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.104.62.115
※ 编辑: james732 来自: 59.104.62.115 (06/18 03:18)
1F:推 ThreeDay0905:感觉可行,真的感谢了 06/18 05:52
2F:→ ThreeDay0905:只是这样group存在的意义就只有点选不重复用吗.. 06/18 05:52
3F:→ ThreeDay0905:我还以为会有专门针对这个用的函数说.. 06/18 05:52
4F:→ james732:我所知道的仅如此 说不定有更好的写法 06/18 11:10