Manually calling an event from code - VB.Net 2003

P

Paul Hadfield

All,

I'm trying to understand how to call an event from within my own code
(VB.Net 2003). For example, when creating a new form with a new button - the
following method is generated when clicking the button:

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

End Sub


But if I wanted to manually run the Button1_Click method from within one of
my own methods - how do I do it? I've tried Button1_Click() but I don't
quite understand the variables that I need to pass to make it work exactly
as though the user had clicked on the button..

Cheers in advance,
Paul
 
A

amdrit

sender is typically going to be the button you clicked. This provision is
in there because you can assign a single handler for all your buttons, this
allows you to determine which one is was. Notice "Handles Button1.Click" at
the end of you declaration? You can easily make it also respond to Button2
as well by chaning it to "Handles Button1.Click, Button2.Click"

e are the informative bits about the click, such as which button you clicked
or if you had a shift key pressed at the same time.

Consider this, you have some code that generally occurs during a button
click, and so you place the code in the button click. Later you find out
that you also need to execute this code for another reason in another
method. I see how you would think that calling the button_click method
solves your problems, but actually it simply masks it. If you think about
it, event handlers give you an opportunity to tie events to logic. That
logic should be isolated from the event itself.

As an example

private Sub MyMethodCall1()
'logic here
end Sub

private Sub MyMethodCall2()
'logic here
end Sub

Private Sub Button1_Click(sender, e)

if typof(sender) is button then
if sender is me.button1 then
MyMethodCall1
else
MyMethodCall2
endif
endif

end sub

private sub Text1_Validate(sender, e)
dim txt as textbox = ctype(sender, textbox)
if txt.text.length > 0 then
MyMethodcall2
end if
e.validate = true
end sub
 
T

Trammel

Paul Hadfield said:
All,

I'm trying to understand how to call an event from within my own code
(VB.Net 2003). For example, when creating a new form with a new button -
the following method is generated when clicking the button:

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

End Sub


But if I wanted to manually run the Button1_Click method from within one
of my own methods - how do I do it? I've tried Button1_Click() but I don't
quite understand the variables that I need to pass to make it work exactly
as though the user had clicked on the button..

Cheers in advance,
Paul

Calling the "button clicked" code from other places (like form_load, or
whatever):
<hint>lateral thinking</hint>

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

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
b1Clicky()
End Sub

Private Sub b1Clicky()
Beep()
End Sub
 
P

Phill W.

Paul said:
I'm trying to understand how to call an event from within my own code
(VB.Net 2003).

You can't /call/ an Event.
You can /handle/ one or cause one to be /raised/ but not /call/ one.
But if I wanted to manually run the Button1_Click method from within one of
my own methods - how do I do it?

Short answer - use Button1.PerformClick().

Bear in mind that this has all the same restrictions as trying to
"click" a button interactively; the Button has to be visible and enabled
and (IIRC) within Container control(s) that are themselves visible and
enabled.


/Proper/ answer - don't.

Extract the code from the button's click event handler into a.n.other
routine - preferably without needing to know the source of the call -
and call /that/ routine from anywhere that you might need it, as in:

Sub Button1_Click( ... )
DoSomethingImportant()
End Sub

Sub DoSomethingImportant()
' whatever
End Sub

Sub Form_Load( ... )
DoSomethingImportant()
End Sub

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