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. 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
.
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.
but you can create a function
which will return the Name
of the selected RadioButton
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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.