471,066 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

"Handles" Question

This question is regarding the "Handles" part of a routine, for
example:

Handles Button1.Click

On Form2 (a User Control) I have a button. Here's it's code:

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

Ok, now on Form1, I have a Menu. One of the menu items needs to act
like Button1 was clicked.

I would like to add to the Handles section by adding:
Handles Button1.Click, Menuitem1.Click

This would work if the button and menuitem were on the same form. But
they are on different forms. I get an error saying "Handles clause
requires a WithEvents variable". How do I go about this?

Thanks for your help!

John

Nov 21 '05 #1
12 1359
"johnb41" <or****@informatik.com> schrieb:
I would like to add to the Handles section by adding:
Handles Button1.Click, Menuitem1.Click

This would work if the button and menuitem were on the same form. But
they are on different forms. I get an error saying "Handles clause
requires a WithEvents variable". How do I go about this?


Take a look at the 'AddHandler' statement in the documentation. You can use
this statement to add a handler to an object's event at runtime.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
John,

There are plenty of methods to force a click event of a button.
Some of them

button1.performclick
ButtonClick(nothing, nothing)

Or to create an extra sub and start that from your both events
(probably for the documentation the best way)

I hope this helps,

Cor>
Nov 21 '05 #3
"Cor Ligthert" <no************@planet.nl> schrieb:
ButtonClick(nothing, nothing)


As always: /Never/ do that!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
Hmmm... I was just going to thank Cor for that easy fix. It worked for
me with no problems.

Why /Never/ do that?

If you convince me, i'll look into the Addhandler suggestion which
looks complicated at first glance! :)

John

Nov 21 '05 #5
"johnb41" <or****@informatik.com> schrieb
Hmmm... I was just going to thank Cor for that easy fix. It worked
for me with no problems.

Why /Never/ do that?
No button has been clicked. There is no sender. There are no event args.
If you convince me, i'll look into the Addhandler suggestion which
looks complicated at first glance! :)


Use Addhandler to tell which procedure is to be called when the event is
raised. As Button1_Click is to be called, use

addhandler yourmenuitem.click, addressof Form2.Button_Click.

I'd then name it differently. Even better, use Cor's 3rd suggestion.

Armin

Nov 21 '05 #6
John,
Hmmm... I was just going to thank Cor for that easy fix. It worked for
me with no problems.

Why /Never/ do that?

If you convince me, i'll look into the Addhandler suggestion which
looks complicated at first glance! :)

He never could convince me, for me are his arguments about this synthytical
sugar.

However maybe has Herfried now new arguments.

:-)

Cor
Nov 21 '05 #7
"johnb41" <or****@informatik.com> schrieb:
Hmmm... I was just going to thank Cor for that easy fix. It worked for
me with no problems.

Why /Never/ do that?

If you convince me, i'll look into the Addhandler suggestion which
looks complicated at first glance! :)


Maybe you'll extend your code to use the values passed to the handler's
'sender' and 'e' parameter, and calling the handler directly without passing
the right values to it will lead to 'NullReferenceException's when
attempting to access the parameter values.

I suggest to either call the button's 'PerformClick' method (which will only
work if the button is enabled and visible), call the handler using
'Bla(Me.Button1, EventArgs.Empty)' or put the code from the handler to a
separate procedure and call this procedure from within the handler and any
other place.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
"Cor Ligthert" <no************@planet.nl> schrieb:
Hmmm... I was just going to thank Cor for that easy fix. It worked for
me with no problems.

Why /Never/ do that?

If you convince me, i'll look into the Addhandler suggestion which
looks complicated at first glance! :)

He never could convince me, for me are his arguments about this
synthytical sugar.


My concern is with passing 'Nothing' to the event handler's parameters,
which may lead to problems over time.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9
Thanks everyone for all your help!

I considered the PerformClick method, but it is not available for some
of my controls (for example, not available for LinkLabel).

I just couldn't figure out the AddHandler method (i.e. where in my code
to put it)

I think i'll go with making a regular sub routine, and having all my
code point to it. Easy for my brain to comprehend! :)

Thanks everyone!!

John

Nov 21 '05 #10
You should probably spend some time learning how addhandler works. Its
quite useful.
"johnb41" <or****@informatik.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks everyone for all your help!

I considered the PerformClick method, but it is not available for some
of my controls (for example, not available for LinkLabel).

I just couldn't figure out the AddHandler method (i.e. where in my code
to put it)

I think i'll go with making a regular sub routine, and having all my
code point to it. Easy for my brain to comprehend! :)

Thanks everyone!!

John

Nov 21 '05 #11
I understand how it works in one way: when you need to add a handler to
a dynamically created control. But i'm sure there's alot more to it
than that. Because of this thread, i'm realizing that i'm missing alot
of functionality from AddHandler. I'll learn it soon! :)

Thanks for everyone's help with this...

John

Nov 21 '05 #12
John,

If you don't need it, don't use it, it adds nothing. You can normally use
the build in Visual Basic functionality for this.

(Although I use it often).

Cor
Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Darren Wooding | last post: by
6 posts views Thread by antonyliu2002 | last post: by
2 posts views Thread by sck10 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.