简体   繁体   中英

Winform Checkbox MouseDown event handler not allowing CheckedChanged event to fire

I have a WinForm which contains a multitude of controls interdependent on each other for their visibility and content. I have a pair of radio buttons, controlling a combobox's (ComboBoxA) enable/disable flag and content. The selection on this combobox controls the visibility of a checkbox. The checking of this checkbox controls another combobox's (ComboBoxB) visibility and content. Business requirements are quite complicated around these controls. As a result, I require the ability to fire of the events programmatically and through user action, doing different things in each case.

In the checkbox's case, I check it programmatically while loading data (if needed), which fires the CheckedChanged event which in turn does additional action controlling ComboBoxB. The code for this is pretty vanilla, nothing special, but my question is more theoretical than practical. Please keep reading.

Due to this requirement, I need a way to distinguish between programmatic checking and user action. I tried using the Click event and CheckedChanged event, setting a flag in the click event, signifying user action. Unfortunately, the CheckedChanged event fires before the Click event, dead-ending this trick.

Now, I tried using the MouseDown event to capture user action. But funnily enough, once the event fires, checkbox remains unchecked and the CheckedChanged event doesnt fire.

Now, I have managed to use a flag in the code to determine programmatic checking and use that to distinguish between the two, but I was curious as to why the MouseDown event didnt allow the checkbox to be checked. Any ideas? I searched online but either I didnt do a thorough job of it, or google is not returning the right results for me. I apologize if anybody is actually able to find a google result for this problem.

It's something else in your code, not the MouseDown event that's preventing the CheckChanged to be fired.
Here is how I know this:
I've added a checkbox and a button to an empty form, and added event handlers to Click on the button, and on the checkbox CheckedChanged , KeyDown and MouseDown events. I've also added to the form a string variable called LastEventRaised , and in the CheckedChanged I've simply shown a MessageBox :

string LastEventRaised = string.Empty;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    MessageBox.Show("Checked changed " + LastEventRaised);
    LastEventRaised = string.Empty;
}

private void checkBox1_KeyDown(object sender, KeyEventArgs e)
{
    LastEventRaised = "KeyDown";
}

private void checkBox1_MouseDown(object sender, MouseEventArgs e)
{
    LastEventRaised = "MouseDown";
}

private void button1_Click(object sender, EventArgs e)
{
    LastEventRaised = "programmatically";
    checkBox1.Checked = !checkBox1.Checked;
}

Each time the message box popped up I've got the correct message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM