Question How to call events dynamically.

Knvn

Well-known member
Joined
Dec 30, 2009
Messages
45
Programming Experience
Beginner
I want to call an event TabPage1_Enter dynamically. To do this what are the arguments should I pass.
 
You don't call events, they call you. Events can only be "called" by raising them from the declaring class. While here I think you're mixing the event with the event handler method, the same applies, these methods should not be called by you in code. If I'm right and you're asking how to call the "Sub TabPage1_Enter" you should instead put that code in a regular Sub that you can call both from the Enter event handler and from anywhere else in your code. Feel free to elaborate on what you are trying to achieve.
 
Ya you're right. I want to call event handler, Because when I writing something like ‘Call AnyControlName’ on the editor it suggests me such as ‘ControlName_AnyDefinedEventHandler’ so I thought that its possible to call the event handler. If so what are the arguments ?
 
An event handler is just a method, so you can call them like any other methods. That said, you shouldn't. The whole point of the parameters is that they provide information about the event. If there's no event then there's no information, so you shouldn't be faking it.

If you want to call a method then call a method. Take the code out of the event handler and put it in its own method. You can then call that new method from anywhere you like, including the event handler. Now you don't have to fake arguments for the event handler because you never call it directly.
 
Resolved

Hey I found the answer myself.

The arguments for

VB.NET:
Private Sub TabPage1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetailTab.Enter '[ I found the arguments by tracing this event. ]
is
VB.NET:
Call DetailTab_Enter(TabPage1, System.EventArgs.Empty)
 
You're not listening, are you? The correct approach to this would be:
VB.NET:
Me.TabControl1.SelectedTab = TabPage2
This will cause the Enter event to be raised and your event handler to be called.

If you do not want that Tabpage to be selected then branching out the Enter code to a separate Sub would be appropriate, this is the Sub you would call instead from other places unrelated to the Tabpage selection/Enter.
 
Creating separate method is what I did before asking the question.

The reason I asked this question is because, I surprised when it suggests me to call the event handler. (I thought that it enables us to raise the event in this way, before asking the question I didn’t know that event handler is nothing more than a method.)

Anyway thank you once again for guiding me.
 
Back
Top