views:

82

answers:

4

How to raise combobox_SelectedIndexChanged(object sender, EventArgs e) programmatically?

Lets say i have method called ClearFields(). I want to call the event before i hit the end of Clearfields() method.

+8  A: 

Just like calling any other method:

Public Sub ClearFields()
    ...
    ...
    combobox_SelectedIndexChanged(Nothing, Nothing)
End Sub

It's not actually 'raising the event'... When an Event is raised, all it does is call the associated method. combobox_SelectedIndexChanged is just a method attached to an event trigger.

If you need to pass a specific comboBox into the method, replace the first Nothing with the combo box you need to pass.

Michael Rodrigues
Since this is the same as the 'actual' event raising mechanics I'd suggest there this no reason to say this is not 'raising the the event'
Preet Sangha
The method is the handler for the event and not the event itself.
aqwert
This is the correct answer, I do it all the time. Don't bother with the others. If your event cares about sender, you could pass it the combobox. Also this is NOT raising the event, just calling the handler. The 'handles' after the handler is ignored when you call it. When it is actually handling the event, VB has generated IL to add a handler between the event and that function (you can actually see the line that does this in c#... but VB's way is slicker syntactically).
FastAl
A: 

For your specific case, I think you may be able to call the OnSelectedIndexChanged method at the end of your own method. Although it seems like clearing the contents of a combo box might raise the event anyway.

Daniel Plaisted
A: 

You can raise the SelectedIndexChanged event by selecting (programmatically) a different item in your ListView control.

MusiGenesis
A: 

Just:

combobox_SelectedIndexChanged(null, null);

At the point in your code where you want to invoke the method.

code4life