您可以使用 Enumerable.OfType
在GroupBox中查找和投射您的RadioButtons:
var radioButtons = groupBox1.Controls.OfType<RadioButton>();
foreach (RadioButton rb in radioButtons)
{
bool state = rb.Checked;
string name = rb.Name;
}
模拟PLC读取:
private void btnAgree_Click(object sender, EventArgs e)
{
int readValue = int.Parse(txtValue.Text);
foreach (Button item in groupBox1.Controls.OfType<Button>())
{
bool status;
if (item.Tag==null)
{
continue;
}
int index = int.Parse(item.Tag.ToString());
status = (readValue & (1 << index)) != 0;
item.BackColor = status ? Color.Green : Color.Gray;
}
}