Through which event was ListBox.SelectedIndexChanged called?

patrick

Well-known member
Joined
Dec 5, 2021
Messages
302
Programming Experience
1-3
Hello

Through which event was ListBox.SelectedIndexChanged called?

C#:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    ListBox list1 = (ListBox)sender; <=====   Here............ Must be tracked. .... Is ListBox.SelectedIndexChanged called through ListBox.MouseClick Event ? Is ListBox.SelectedIndexChanged called through ListBox.TextChanged Event ?  Is ListBox.SelectedIndexChanged called through ListBox.Click Event ?
 
    System.EventArgs e = (System.EventArgs)e; <=====   Here............ Must be tracked. .... Is ListBox.SelectedIndexChanged called through ListBox.MouseClick Event ? Is ListBox.SelectedIndexChanged called through ListBox.TextChanged Event ?  Is ListBox.SelectedIndexChanged called through ListBox.Click Event ?
}


ListBox.MouseClick Event ====> Is ListBox.SelectedIndexChanged called through ListBox.MouseClick Event ?
ListBox.TextChanged Event ====> Is ListBox.SelectedIndexChanged called through ListBox.TextChanged Event ?
ListBox.Click Event ====> Is ListBox.SelectedIndexChanged called through ListBox.Click Event ?


How do I identify which ListBox.MouseClick Event / ListBox.TextChanged Event / ListBox.Click Event​

called ListBox.SelectedIndexChanged?​

 
Why do you need to know what triggered the selection changed event? You are likely taking the wrong approach if you need to know what triggered the event. How will you handle the listbox being initialized by the form's load or shown event?

In the past, I've seen people trying to handle a particular event and needing to know what was the event that triggered that particular event. The majority were trying to avoid recursive or re-entrant behavior. This is because they put all their business logic within the event handlers themselves. What they should have done was move the business logic into specific methods, and the event handlers just call those methods. Then the methods are then written to detect and handle re-entrancy. The minority of people were actually creating brand new Windows controls or subclassing existing Windows controls. And again, as was the case with the majority, the logic for managing the control state should have been moved into specific methods that can handle the re-entrancy.

Approaches which are not recommended:
- Inspecting the callstack. That way lies madness.
- Look an the source code from Reference Source or Source Browser . Even though it is open source, you are also effectively coupling to undocumented APIs.

Speaking of undocumented APIs. I would suggest to look at the actual documentation for each of those events in question (as opposed to the source code). In most of the WinForms documentation, they will state what sequence events will be sent out. For example:
 

Latest posts

Back
Top Bottom