ng80092b
ng80092b

Reputation: 667

Prevent a checkbox from being checked (not simply uncheck)

Under certain conditions a checkbox I have will display an error. I'm trying do disable the checkbox unless certain conditions are met.

I've tried the following

    private void checkBox66_CheckedChanged(object sender, EventArgs e)
    {
        if (dataGridView3.RowCount == 0)
        {
            checkBox66.Checked = false;
        }
    }

But I realized this actively checks the box, before realizing any kind of uncheck. I need to make sure the checkbox ignores any clicks on it while the rowcount is 0

Upvotes: 1

Views: 2245

Answers (2)

Rick
Rick

Reputation: 841

If I am understanding your question correctly, you are trying to prevent the checkbox from ever being checked if the rowcount = 0?

You could add code to the datagridview's RowsAdded and RowsRemoved events, enabling/disabling+unchecking the checkbox depending on the number of rows.

If the user requires an explanation for why the checkbox is disabled, you could put a label next to the checkbox.

Upvotes: 0

Sandy Gettings
Sandy Gettings

Reputation: 713

Set the Enabled property to false. Whatever value the CheckBox has, checked or unchecked, will be unchangeable by the user. You can do this in the CheckBox's properties in the designer, or in code:

CheckBox1.Enabled = false;

Upvotes: 4

Related Questions