473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1469
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Rohit | last post by:
Hi, Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event" handles, by Event handles I mean Win32 Event handles which can be monitored using the ProcessExplorer from...
1
by: Darren Wooding | last post by:
Hello, can anyone offer any advice, this one is driving me up the wall! 1. I am using a webform datagrid. 2. The columns are all dynamic and defined using templates (see code below). 3. I have...
7
by: Simon Verona | last post by:
I have a problem in my application which I believe is due to open handles.. . The symptom that users report is that after they have been using the application for a while, it will randomly just...
6
by: antonyliu2002 | last post by:
Right now I put my VB code in the aspx pages. I would prefer to use codebehind, especially many of my VB functions may be shared across multiple aspx pages. I've tried a few pages with...
2
by: sck10 | last post by:
Hello, Is there an equivalent to using "Handles" in the codebehind for c#? Or do I have to declare every event in the GridView control like the following: <asp:GridView ID="gvSearchList"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.