简体   繁体   English

如何获得单选按钮所选项目的文本?

[英]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将返回所选RadioButtonName

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.

相关问题 如何在数据列表中获取选定的单选按钮文本? - How to get selected radio button text in datalist ? 如何从附加到按钮的菜单中获取所选项目 - How do I get the selected item from Menu attached to a button 如何不使用if子句而获得所选单选按钮的文本? - How can I get the text of the selected Radio Button without to use the if clause? 如何使用带浮点值的MVC RadioButtonFor选择正确的单选按钮? - How do I get the correct radio button selected using MVC RadioButtonFor with float values? 如果未选择单选按钮,如何停止计算程序? - How do I stop a program from calculating if no radio button is selected? 如何从WPF列表框中获取选定列表项中的选定单选按钮 - How to get selected radio button in selected list item from wpf listbox 如何使用jquery获取选定的asp单选按钮? - how can i get the selected asp radio button using jquery? 如何获取要在现有文本文件中列出的单选按钮的名称/文本? - How do I get the name/text of a radio button to be listed in an existing text file? 如何获得类似单选按钮组的行为,以便只能选择一项 - How to get behavior like a radio button group so only one item can be selected 选择单选按钮列表项后如何从gridview获取单元格值? - How to get cell value from gridview when radio button list item is selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM