簡體   English   中英

C# winform 復選框仍然被選中

[英]C# winform checkbox still checked

在按鈕單擊功能中,我設置了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;
    }
}

當我再次單擊按鈕時,復選框未選中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM