[英]How do I get the text of the radio button selected item?
Here are my radio buttons 这是我的单选按钮
Runescape
Maplestory
League
So this is my current method and it runs perfectly fine. 所以这是我当前的方法,它运行得很好。
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
MessageBox.Show("You are playing Runescape.");
}
else if (radioButton2.Checked)
{
MessageBox.Show("You are playing Maplestory.");
}
else if (radioButton3.Checked)
{
MessageBox.Show("You are playing League.");
}
}
I want to know if there is a way I can print out the SelectedItem kinda like a combo box. 我想知道是否有一种方法可以像组合框一样打印出SelectedItem。 Basically, the text of the radio button. 基本上是单选按钮的文本。
Combobox version: 组合框版本:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("You are playing " + comboBox1.SelectedItem);
}
Something along the lines of this (not sure if it is possible). 与此类似的东西(不确定是否可能)。
MessageBox.Show("You are playing " + RadioButton.SelectedItem);
you can use Text
property of RadioButton
. 您可以使用RadioButton
Text
属性。
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
MessageBox.Show("You are playing "+radioButton1.Text);
}
else if (radioButton2.Checked)
{
MessageBox.Show("You are playing Maplestory "+"+radioButton2.Text);
}
else if (radioButton3.Checked)
{
MessageBox.Show("You are playing League "+"+radioButton3.Text);
}
}
Solution 2: there is no SelectedItem
Property for RadioButton
control. 解决方案2: RadioButton
控件没有SelectedItem
属性。
but you can create a function
which will return the Name
of the selected RadioButton
但是您可以创建一个function
,该function
将返回所选RadioButton
的Name
Try this: 尝试这个:
private String getSelectedRadioButtonName()
{
foreach (Control c in groupBox1.Controls)
{
if (c is RadioButton && ((RadioButton) c).Checked==true)
{
return c.Text;
}
}
return "No Game";
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("You are playing "+getSelectedRadioButtonName());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.