[英]How do I get the text of the radio button selected item?
這是我的單選按鈕
Runescape
Maplestory
League
所以這是我當前的方法,它運行得很好。
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.");
}
}
我想知道是否有一種方法可以像組合框一樣打印出SelectedItem。 基本上是單選按鈕的文本。
組合框版本:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("You are playing " + comboBox1.SelectedItem);
}
與此類似的東西(不確定是否可能)。
MessageBox.Show("You are playing " + RadioButton.SelectedItem);
您可以使用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);
}
}
解決方案2: RadioButton
控件沒有SelectedItem
屬性。
但是您可以創建一個function
,該function
將返回所選RadioButton
的Name
嘗試這個:
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.