Call a Sub

G

Guest

What variables do I have to supply when I call a sub routine that contains
(sender, e).
For instance I want to call this:
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click

When I try to call it using:

Call MenuItem1_Click()

I get:

"Argument not supplied for parameter 'e' of Private Sub
MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)"
 
W

William Ryan eMVP

Use the name of the controlyou are sending or me:

for instance, MenuItem1_Click(thisButton, nothing)
or MenuItem1_Click(me, nothing)

You can put any control you want...and if you aren't sending any event args
then just sned nothing...
 
G

Guest

Thank you for your reply but I am still not sure. You can tell I'm new at
this :)
I can call the Sub Routine when it is an event such as an object click but
what I want to do is jump the program to it from lets say, an If statement.
i.e.
If foo = 0 then
Call Sub MenuItem1_Click()
Else
Call Sub MenuItem2_Click()
end If

or even just at the end of another sub routine when it completes as a goto
to branch the program.

When I write "Call Sub MenuItem2_Click()" it needs those args in the
parenthesis that I mentioned before.

I hope that explains my question better. Thanks for your help.
 
B

Brian Henry

Hi, You do not have to write call sub when you call a sub, it is good enough
to just write the sub or function name alone with the paramater list.
 
C

chris in grimsby

William Ryan eMVP said:
Use the name of the controlyou are sending or me:

for instance, MenuItem1_Click(thisButton, nothing)
or MenuItem1_Click(me, nothing)

You can put any control you want...and if you aren't sending any event args
then just sned nothing...


Alternatively ( and in my opionion better) put the code that is in
your menuitem1_click event procedure in a subroutine that is easier to
call, and then you can call it from both places:

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click

MyMenuHandler
End Sub

Sub MyMenuHandler()
'do something
end Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

MyMenuHandler
End Sub
 
B

Bill McCarthy

hi SayersT,

You should not call those methods, as they are really meant to handle an event.
Instead, if there is a common routine you want to call, make that a different
procedure, and call that from both your menuitem_click handler and your other
code.

Bill.
 
P

Phill. W

.. . .
For instance I want to call this:
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click

In the specific case of 'click'ing, try this :

MenuItem1.PerformClick()

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top