Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#winform复选框仍处于选中状态_C#_Winforms_Checkbox - Fatal编程技术网

C#winform复选框仍处于选中状态

C#winform复选框仍处于选中状态,c#,winforms,checkbox,C#,Winforms,Checkbox,在按钮点击功能中,我设置了checkbox.check=false 但当我再次单击按钮时,复选框仍处于选中状态 这是我的密码: // Header全選加上CheckBox System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true); rect.X = rect.Location.X + rect.Width / 4 - 9; rect.Y = rect.Location.Y + (rect.

在按钮点击功能中,我设置了
checkbox.check=false
但当我再次单击按钮时,复选框仍处于选中状态

这是我的密码:

// Header全選加上CheckBox
System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
rect.X = rect.Location.X + rect.Width / 4 - 9;
rect.Y = rect.Location.Y + (rect.Height / 2 - 9);
SolidBrush solidBrush = new SolidBrush(Color.White);
System.Windows.Forms.CheckBox checkBox = new System.Windows.Forms.CheckBox();
checkBox.Name = "checkBoxHeader";
checkBox.Size = new Size(18, 18);
checkBox.Location = rect.Location;
//checkBox.AutoCheck = false;
//MessageBox.Show(checkBox.Checked.ToString());
checkBox.Checked = false;
checkBox.Invalidate();
checkBox.Update();
dataGridView1.RefreshEdit();
checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
dataGridView1.Controls.Add(checkBox);
我将MessageBox设置为显示复选框值,但该值为“False”

当我单击botton时,如何设置复选框未选中


非常感谢

在按钮单击方法中,您实例化了一个新复选框,并且它的状态总是设置为false。您必须实例化复选框并在窗体的构造函数中设置其属性,或者在使用设计器时可以将复选框拖到窗体上

范例

private Checkbox _checkbox;

public Form1() 
{
    InitializeComponents();
    _checkbox = new CheckBox();
    //set properties
    _checkbox.Checked = true;
    //other properties you want to set
    //add checkbox to your form for example
    datagridview1.Controls.Add(_checkbox);
}
然后,在按钮单击方法中,只需编写:

_checkbox.Checked = false;

谢谢你的回答,我解决了这个问题

我将代码设置为

foreach (Control ckb in dataGridView1.Controls)
{
    if (ckb is System.Windows.Forms.CheckBox)
    {
        ((System.Windows.Forms.CheckBox)ckb).Checked = false;
    }
}

当我再次单击按钮时,复选框被取消选中

请确保您没有运行两次
InitializeComponent
。这将是控制行为中的一个问题。或者确保您没有在其他地方设置
Checked
属性