"Handles" Question

  • Thread starter Thread starter johnb41
  • Start date Start date
J

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
 
johnb41 said:
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.
 
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>
 
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
 
johnb41 said:
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
 
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
 
johnb41 said:
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.
 
Cor Ligthert said:
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.
 
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
 
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
 
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
 
Back
Top