简体   繁体   English

如何在消息框上显示,选择了哪个单选按钮

[英]how to display on a messagebox, which radio button was selected

I have 2 radio button for gender. 我有2个性别单选按钮。 one male (named radMale ) one female (name radFemale ). 一名男性(名为radMale )一名女性(姓名为radFemale )。 lets say a user clicks the "male" button. 让我们说用户点击“男性”按钮。 how do i display what he chose on a MessageBox ? 我如何在MessageBox上显示他选择的内容?

MessageBox.Show(radMale = "Male");

I know this doesn't work, does anyone know which code will? 我知道这不起作用,有谁知道哪些代码会?

I tried executing the following code which might solve your problem. 我尝试执行以下代码可能会解决您的问题。

public partial class Form1: Form
{
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
        }

        void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if(rb.Checked)
                MessageBox.Show(rb.Text); //Shows whatever Text your radiobutton has
        }

        void Form1_Load(object sender, EventArgs e)
        {

        }
}

在WinForms中,使用此:

MessageBox.Show(radMale.Checked?"Male":"Female");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM