Home All Groups Group Topic Archive Search About
Author
27 Jun 2005 4:52 PM
johnb41
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

Author
27 Jun 2005 5:12 PM
Herfried K. Wagner [MVP]
"johnb41" <ord***@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/>
Author
27 Jun 2005 5:24 PM
Cor Ligthert
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>
Author
27 Jun 2005 5:58 PM
Herfried K. Wagner [MVP]
"Cor Ligthert" <notmyfirstn***@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/>
Author
27 Jun 2005 6:46 PM
johnb41
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
Author
27 Jun 2005 6:51 PM
Armin Zingler
"johnb41" <ord***@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
Author
27 Jun 2005 7:00 PM
Cor Ligthert
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
Author
27 Jun 2005 7:09 PM
Herfried K. Wagner [MVP]
"Cor Ligthert" <notmyfirstn***@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/>
Author
27 Jun 2005 7:07 PM
Herfried K. Wagner [MVP]
"johnb41" <ord***@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/>
Author
27 Jun 2005 9:00 PM
johnb41
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
Author
27 Jun 2005 9:40 PM
Rick Mogstad
You should probably spend some time learning how addhandler works.  Its
quite useful.


Show quoteHide quote
"johnb41" <ord***@informatik.com> wrote in message
news:1119906057.466375.179700@f14g2000cwb.googlegroups.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
>
Author
28 Jun 2005 2:39 PM
johnb41
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
Author
28 Jun 2005 2:44 PM
Cor Ligthert
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