Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between a user changing the Checkbox.Checked value, or it programmatically changing

I see that Checkboxes have a CheckedChanged event. is it possible to tell whether it was changed programmatically, or by the user actually checking the checkbox?

I've got a large grid where the user can type in a filter, or use checkboxes for a sort of "Quick filter" that offers common filtering parameters. Then say they go and modify the filter through the textbox, I was checking whether or not I should programmatically (un)check the CheckBox controls so that it reflects the filter in the textbox.

    private void genericCheckbox_CheckedChanged(object sender, EventArgs e)
    {
        UpdateFilter();
    }

    private void UpdateFilter()
    {
        if (gdcSVNDefaultView.RowCount == 0)
            return;

        gdcSVNDefaultView.ActiveFilterString = BuildTableFilter();
        gdcSVNDefaultView.BestFitColumns();
    }

    private void gdcSVNDefaultView_ColumnFilterChanged(object sender, EventArgs e)
    {
        lblTotalFileCount.Text = gdcSVNDefaultView.RowCount.ToString();

        if (gdcSVNDefaultView.ActiveFilterString.Contains("Normal"))
            cheNormalFiles.Checked = true;
        else
            cheNormalFiles.Checked = false;

        if (gdcSVNDefaultView.ActiveFilterString.Contains("bin") || 
            gdcSVNDefaultView.ActiveFilterString.Contains("obj"))
            cheBinObjFolders.Checked = true;
        else
            cheBinObjFolders.Checked = false;
    }

With some very light testing, this seems to work just as I want it to. But I'm afraid that there's some sort of 'infinite loop' case where the ColumnFilterChanged event will fire because of the UpdateFilter method being called when the CheckedChanged event happens, which could in turn cause CheckedChange to happen again since the ColumnFilterChanged manipulates the Checkboxes.

like image 255
sab669 Avatar asked Oct 21 '13 17:10

sab669


1 Answers

Using flag for this purpose is OK:

bool suppressCheckedChanged;
private void gdcSVNDefaultView_ColumnFilterChanged(object sender, EventArgs e)
{
   suppressCheckedChanged = true;
   //.... your own code
   //....
   suppressCheckedChanged = false;
}
private void genericCheckbox_CheckedChanged(object sender, EventArgs e)
{
    if(suppressCheckedChanged) return;
    UpdateFilter();
}

UPDATE

Using flag is the best way (the most concise and convenient) I think. However, after some searching on the internal implementation of CheckBox.cs, I've found that the internal field checkState is used. the Checked property is designed just for convenience. The CheckedChanged event is raised in the setter of the CheckState property. So by modifying the checkState field, we bypass the CheckedChanged event raiser. Because the field checkState is not public, we have to use Reflection to change its value. That's why this code is a little lengthy compared to using flag. This is the code for you, note that this is just a reference to open wide the knowledge on this problem, as I said using flag is much more concise and the code is also coherent:

//Use this extension method for convenience
public static class CheckBoxExtension {
  public static void SetChecked(this CheckBox chBox, bool check){
    typeof(CheckBox).GetField("checkState", BindingFlags.NonPublic |
                                            BindingFlags.Instance)
                    .SetValue(chBox, check ? CheckState.Checked : 
                                             CheckState.Unchecked);
    chBox.Invalidate();
  }
}
//then you can use the SetChecked method like this:
checkBox1.SetChecked(true);//instead of checkBox1.Checked = true;
checkBox1.SetChecked(false);//instead of checkBox1.Checked = false;
like image 103
King King Avatar answered Sep 24 '22 04:09

King King