C# 第一个单选按钮始终处于选中状态,即使我在所有单选按钮上将选中的属性设置为false

C# 第一个单选按钮始终处于选中状态,即使我在所有单选按钮上将选中的属性设置为false,c#,.net,winforms,C#,.net,Winforms,我有一个带有两个单选按钮的简单表单。我希望在应用程序开始时取消选中单选按钮s(默认情况下未选择任何选项),以便用户自己进行选择 尽管我在表单构造函数和表单加载事件处理程序中的所有单选按钮上将Checked属性设置为false,我还在构造函数中调用了一个方法,该方法将所有RadioButton的Checked属性设置为false,当我运行应用程序时,第一个RadioButton仍然被选中 代码如下: using System; using System.Collections.Generic; u

我有一个带有两个
单选按钮的简单表单。我希望在应用程序开始时取消选中
单选按钮
s(默认情况下未选择任何选项),以便用户自己进行选择

尽管我在
表单
构造函数和
表单
加载
事件处理程序中的所有
单选按钮
上将
Checked
属性设置为
false
,我还在构造函数中调用了一个方法,该方法将所有
RadioButton
Checked
属性设置为
false
,当我运行应用程序时,第一个
RadioButton
仍然被选中

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OptionSelection
{
    public partial class Form1 : Form
    {
        InitializeComponent();

        radioButton1.Checked = false;
        radioButton2.Checked = false;

        this.Load += new System.EventHandler(this.Form1_Load);
        this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
        this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);

        UncheckAllRadioButtons();
    }
    private void UncheckAllRadioButtons()
    {
        IEnumerable<Control> allControls = GetAllControlsOfDeterminedType(this, typeof(RadioButton));
        foreach (Control currentControl in allControls)
        {
            RadioButton _currentRadioButton = (RadioButton)currentControl;
            if (_currentRadioButton.Checked)
            {
                _currentRadioButton.Checked = false;
            }
        }
    }

    public IEnumerable<Control> GetAllControlsOfDeterminedType(Control currentControl, Type type)
    {
       IEnumerable<System.Windows.Forms.Control> allControls = currentControl.Controls.Cast<Control>();

        return allControls.SelectMany(selectedControls => GetAllControlsOfDeterminedType(selectedControls, type)).Concat(allControls).Where(candidateControl => candidateControl.GetType() == type);
    }

    private void radioButton_CheckedChanged(object sender, EventArgs e)
    {
        if (sender == (RadioButton)radioButton1)
        {
            MessageBox.Show("radioButton1");
        }
        else
        {
            MessageBox.Show("radioButton2");
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        radioButton1.Checked = false;
        radioButton2.Checked = false;
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间选项选择
{
公共部分类Form1:Form
{
初始化组件();
radioButton1.选中=假;
radioButton2.选中=错误;
this.Load+=new System.EventHandler(this.Form1\u Load);
this.radioButton1.CheckedChanged+=新的System.EventHandler(this.radioButton\u CheckedChanged);
this.radioButton2.CheckedChanged+=新的System.EventHandler(this.radioButton\u CheckedChanged);
取消选中lRadioButtons();
}
私人作废取消勾选默认按钮()
{
IEnumerable allControls=GetAllControlsOfDeterminatedType(this,typeof(RadioButton));
foreach(所有控制中的控制电流控制)
{
RadioButton _currentRadioButton=(RadioButton)currentControl;
如果(_currentRadioButton.选中)
{
_currentRadioButton.Checked=false;
}
}
}
public IEnumerable GetAllControlsOfDeterminedType(控件当前控件,类型类型)
{
IEnumerable allControls=currentControl.Controls.Cast();
返回allControls.SelectedMany(selectedControls=>GetAllControlsOfDeterminedType(selectedControls,type)).Concat(allControls)。其中(candidateControl=>candidateControl.GetType()==type);
}
私有void单选按钮\u CheckedChanged(对象发送方,事件参数e)
{
如果(发送方==(无线按钮)无线按钮1)
{
MessageBox.Show(“radioButton1”);
}
其他的
{
MessageBox.Show(“radioButton2”);
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
radioButton1.选中=假;
radioButton2.选中=错误;
}
}

您不需要所有这些代码。只需将单选按钮的
AutoCheck
属性设置为
false

如果要在代码中将
Checked
属性设置为
false
,则必须在显示表单后执行此操作。因此,将代码从
加载
事件移动到所示的
事件:

private void Form1_Shown(object sender, EventArgs e) {
  radioButton1.Checked = false;
  //radioButton2.Checked = false;
}
你只需要第一行,但这是你的代码


顺便说一下,默认情况下,加载表单时不会选中单选按钮。您面临的问题是,您的单选按钮是表单上的第一个控件(或者可能是唯一的控件)。因此,当表单显示时,焦点会移动到第一个控件,并自动检查单选按钮。您可以通过简单地更改单选按钮的
TabIndex
,使其不是第一个控件(表单上必须有其他控件),或者通过将单选按钮的
TabStop
属性设置为
false
,使其不会聚焦,来阻止这种情况的发生。

为了实现这样一件小事,需要大量的代码。你不需要这些。@Racil Hilan我很抱歉,但那一点帮助都没有:1。您没有指出我的代码有什么问题。2.你没有告诉我更好的执行任务的方法。你的代码没有问题,只是多余的。默认情况下,
Checked
属性设置为
false
,反复尝试将其设置为
false
将不会产生任何效果。您必须查找是什么将其设置为
true
并停止它。查看我的答案。@Racil Hilan所有现有代码都已发布。没有别的了。所有冗余代码都存在,只是因为默认情况下始终选中了第一个RadioButton。有趣的是,在我关闭Visual Studio并再次启动它之后,它按预期工作:所有单选按钮均未选中。此外,整个
GetAllControlsOfDeterminatedType
方法可以替换为仅
控件。OfType()
如果我将AutoChek属性设置为false,则所有单选按钮均未选中,但我无法在鼠标单击时检查它们。正如我所说,现在一切正常,没有任何更改。是的,您必须在
单击
事件中手动选中/取消选中它们。有关在代码中取消选中它们的正确方法,请参见我的更新答案。同样,您不需要任何长代码。