VS C# 简单的单选按钮开关
VS C# simple radio button switch
我试图在此处搜索解决方案,但 none 可用的解决方案似乎有效。两个单选按钮在一个组合框中。
代码:
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton senderControl = sender as RadioButton;
if (!senderControl.Checked)
return;
switch ((sender as RadioButton).Text)
{
case "radioButton1":
textBox4.Clear();
comboBox6.Enabled = false;
textBox4.ReadOnly = true;
textBox4.Enabled = false;
textBox4.Text = "000";
break;
case "radioButton2":
textBox4.Clear();
comboBox6.Enabled = true;
textBox4.ReadOnly = false;
textBox4.Enabled = true;
textBox4.Text = "";
break;
}
}
它不想工作,因为它什么都不做
我假设您的事件“radioChecked”已分配给单个 radioButton。您需要将两个单选按钮附加到同一个事件。您可以在代码或表单中这样做:
public Form1()
{
InitializeComponent();
radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
}
但之后您需要转到 Form.Designer.cs 文件并删除将事件分配给单选按钮的代码行:
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged); // <-- delete
另外,你的第一个条件 if (!senderControl.Checked)
是不必要的,因为如果你点击单选按钮,那么它无论如何都会被选中。
所以,这是您的代码片段:
public Form1()
{
InitializeComponent();
radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton senderControl = sender as RadioButton;
switch (senderControl.Text)
{
case "radioButton1":
textBox4.Clear();
comboBox6.Enabled = false;
textBox4.ReadOnly = true;
textBox4.Enabled = false;
textBox4.Text = "000";
break;
case "radioButton2":
textBox4.Clear();
comboBox6.Enabled = true;
textBox4.ReadOnly = false;
textBox4.Enabled = true;
textBox4.Text = "";
break;
}
}
我试图在此处搜索解决方案,但 none 可用的解决方案似乎有效。两个单选按钮在一个组合框中。
代码:
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton senderControl = sender as RadioButton;
if (!senderControl.Checked)
return;
switch ((sender as RadioButton).Text)
{
case "radioButton1":
textBox4.Clear();
comboBox6.Enabled = false;
textBox4.ReadOnly = true;
textBox4.Enabled = false;
textBox4.Text = "000";
break;
case "radioButton2":
textBox4.Clear();
comboBox6.Enabled = true;
textBox4.ReadOnly = false;
textBox4.Enabled = true;
textBox4.Text = "";
break;
}
}
它不想工作,因为它什么都不做
我假设您的事件“radioChecked”已分配给单个 radioButton。您需要将两个单选按钮附加到同一个事件。您可以在代码或表单中这样做:
public Form1()
{
InitializeComponent();
radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
}
但之后您需要转到 Form.Designer.cs 文件并删除将事件分配给单选按钮的代码行:
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged); // <-- delete
另外,你的第一个条件 if (!senderControl.Checked)
是不必要的,因为如果你点击单选按钮,那么它无论如何都会被选中。
所以,这是您的代码片段:
public Form1()
{
InitializeComponent();
radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton senderControl = sender as RadioButton;
switch (senderControl.Text)
{
case "radioButton1":
textBox4.Clear();
comboBox6.Enabled = false;
textBox4.ReadOnly = true;
textBox4.Enabled = false;
textBox4.Text = "000";
break;
case "radioButton2":
textBox4.Clear();
comboBox6.Enabled = true;
textBox4.ReadOnly = false;
textBox4.Enabled = true;
textBox4.Text = "";
break;
}
}