Windows Forms check box changed or updated?

Is it possible in C # Windows Forms to distinguish between a checkbox that switches with a click of a user and an update to it coming from another event?

I have a listbox values ​​and a checkboxes panel containing yes / no information about the items in the list. When an item is selected in the list, I want to update checkboxes with its information. A complication arises where one of the checkboxes represents a unique status, which is allowed to have only one of the elements in the listbox . If the user clicks on this checkbox when it is in unchecked state, I want it to set the field to true for the currently selected item and false for which item was previously considered true.

As I understand it, the CheckedChanged event CheckedChanged fired both when the listbox selection is listbox (setting checkbox values ​​for this element), and when the user clicks. I need a way to separate them.

How to do it?

+4
source share
2 answers

As I understand it, the CheckedChanged event will be triggered both by changing the selection of the list (setting the flag values ​​for this element) and on the user's click. I need a way to separate them.

Yes, you're right, the CheckedChanged event will be raised when you update the state of the checkBox (what do you do when the ListBox.SelectedIndexChanged event or something like this is raised, I think).

You need to disable the handler before updating the status of the checkBox .

 private void myListBox_SelectedIndexChanged(object sender, System.EventArgs e) { //disable checkbox event mycheckbox.CheckedChanged -= mycheckbox_CheckedChanged; //you can change the checkbox state here //... //Activate event mycheckbox.CheckedChanged += myListBox_SelectedIndexChanged; } private void mycheckbox_CheckedChanged(object sender, System.EventArgs e) { //(...) } 

Another possible pattern is to use a flag to not listen to the CheckedChanged event while updating the status of the checkBox by code.

 private bool flagactivated = true; private void myListBox_SelectedIndexChanged(object sender, System.EventArgs e) { //disable checkbox event flagactivated = false; //Do stuff //(...) //Activate event flagactivated = true; } private void mycheckbox_CheckedChanged(object sender, System.EventArgs e) { if (flagactivated) { //do stuff } } 
0
source

Here is a short example showing how the click events are executed on the check box:

 Private Sub CheckBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseDown Dim a As Integer = 1 End Sub Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged Dim a As Integer = 2 End Sub Private Sub CheckBox1_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.Click Dim a As Integer = 3 End Sub Private Sub CheckBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseClick Dim a As Integer = 4 End Sub Private Sub CheckBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseUp Dim a As Integer = 5 End Sub 

If you set breakpoints in each of the ads, you will notice that only the MouseDown event is fired before CheckChanged. This means that if you want to see if the user clicked this check box, you will need to fire the event in the CheckBox.MouseDown element. Keep in mind that each time the user clicks this checkbox, he will trigger this event, even if they pull the mouse out of it while holding it, and SHOULD NOT update the event with the modified scan. It just means that you will need to fire the next event in Sub MouseUp to clear the flag.

One way to handle this would be something like this:

 Private blIsUserClick As Boolean Private Sub CheckBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseDown blIsUserClick = True End Sub Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged If blIsUserClick Then 'Is a user click event Else 'Not a user click event End If End Sub Private Sub CheckBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseUp blIsUserClick = False End Sub 
+1
source

Source: https://habr.com/ru/post/1501725/


All Articles