473,804 Members | 3,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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 1506
"johnb41" <or****@informa tik.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.perform click
ButtonClick(not hing, 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(not hing, 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****@informa tik.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.cl ick, addressof Form2.Button_Cl ick.

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****@informa tik.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 'NullReferenceE xception'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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3644
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 www.sysinternals.com, or even simply look at the Handle count in the good old windows TaskManager. To demonstrate the problem, all I did was created a basic Win Forms application and with Main implemented as:
1
1723
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 a nice set of buttons on the grid for edit, update, cancel and delete. 4. I handle the events with the "Handles dgGrid.EditCommand", "Handles dgGrid.UpdateCommand",
7
2636
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 crash with an exception report (I've not got the details of the error, but I'm working on that now!). I'm trying to reproduce the circumstances by simulating a typical batch of user tasks on my pc and monitoring whats happening. I'm guessing it's...
6
1563
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 codebehind by follwing some examples on the Web and they work just fine. However, I am scared away from codebehind by such extenstions as "Handles MyBase.Load", "Handles Button1.Click" and "Handles MyBase.Init" etc. which we seem to have to add to the...
2
4401
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" runat="server" DataSourceID="dsSearchList" DataKeyNames="ServiceIdea_ID" OnSelectedIndexChanged="gvSearchList_SelectedIndexChanged">
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.