473,763 Members | 9,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

same sub, multiple control click events

cj
I would like to have menu items a main menu bar that represent the days
of the week. When you click on them they alternate from checked to
unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

I would like to use the same sub to handle all 7 menu items. How would
I write this?
Jun 27 '06 #1
12 2212
Hello cj,

private sub Form_Load(...)

AddHandler MenuItem1.Click , AddressOf MenuItemClickHa ndler
AddHandler MenuItem2.Click , AddressOf MenuItemClickHa ndler
...
AddHandler MenuItemN.Click , AddressOf MenuItemClickHa ndler

End Sub

Private Sub MenuItemClickHa ndler(byval sender as object, byval e as EventArgs)

Dim tMenuItem as MenuItem = sender
tMenuItem.Check ed = Not tMenuItem.Check ed

End Sub
I would like to have menu items a main menu bar that represent the
days of the week. When you click on them they alternate from checked
to unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e
As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub
I would like to use the same sub to handle all 7 menu items. How
would I write this?

Jun 28 '06 #2
Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, SunMenuItem2.Cl ick,
SunMenuItem3.Cl ick, SunMenuItem4.Cl ick well you get the point i guess :-)
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

regards

Michel Posseth [MCP]

"cj" wrote:
I would like to have menu items a main menu bar that represent the days
of the week. When you click on them they alternate from checked to
unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

I would like to use the same sub to handle all 7 menu items. How would
I write this?

Jun 28 '06 #3
cj
yes but my goal is not to have all of them change SunMenuItem. It needs
to change only the one that was clicked.

Private Sub DaysMenuItem_Cl ick(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, MonMenuItem.Cli ck,
TueMenuItem.Cli ck, WedMenuItem.Cli ck, ThuMenuItem.Cli ck,
FriMenuItem.Cli ck, SatMenuItem.Cli ck
'how do I use sender here to check or uncheck the item clicked?
End Sub
M. Posseth wrote:
Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, SunMenuItem2.Cl ick,
SunMenuItem3.Cl ick, SunMenuItem4.Cl ick well you get the point i guess :-)
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

regards

Michel Posseth [MCP]

"cj" wrote:
I would like to have menu items a main menu bar that represent the days
of the week. When you click on them they alternate from checked to
unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

I would like to use the same sub to handle all 7 menu items. How would
I write this?

Jun 28 '06 #4
cj
Ok, I tried in debug to see what sender is
? sender
{System.Windows .Forms.MenuItem }
[System.Windows. Forms.MenuItem]: {System.Windows .Forms.MenuItem }

but it doesn't explain much to me. After running the first line of your
code I tried to see what tmenuitem is
? tmenuitem {System.Windows .Forms.MenuItem }
BarBreak: False
Break: False
Checked: False
Container: Nothing
DefaultItem: False
Enabled: True
FindHandle: 0
FindShortcut: 1
Handle: 2009465263
Index: 3
IsParent: False
MdiList: False
MdiListItem: Nothing
MenuItems: {System.Windows .Forms.Menu.Men uItemCollection }
MergeOrder: 0
MergeType: Add
Mnemonic: Nothing
OwnerDraw: False
Parent: {System.Windows .Forms.MenuItem }
RadioCheck: False
Shortcut: None
ShowShortcut: True
Site: Nothing
Text: "Wednesday"
Visible: True

That's a lot more info. Don't know why I couldn't say ? sender but...
If Sub MenuItem is getting the sender by value and then it would seem we
are making a (well is it new) instance of a menu item and giving it the
same properties of the sender. Well I just don't get how it sets the
original clicked menuitem on and off.

GhostInAK wrote: Hello cj,

private sub Form_Load(...)

AddHandler MenuItem1.Click , AddressOf MenuItemClickHa ndler
AddHandler MenuItem2.Click , AddressOf MenuItemClickHa ndler
...
AddHandler MenuItemN.Click , AddressOf MenuItemClickHa ndler

End Sub

Private Sub MenuItemClickHa ndler(byval sender as object, byval e as
EventArgs)

Dim tMenuItem as MenuItem = sender
tMenuItem.Check ed = Not tMenuItem.Check ed

End Sub
I would like to have menu items a main menu bar that represent the
days of the week. When you click on them they alternate from checked
to unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e
As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub
I would like to use the same sub to handle all 7 menu items. How
would I write this?


Jun 28 '06 #5
cj
I love the way you change the menu item checked on an off
(tMenuItem.Chec ked = Not tMenuItem.Check ed) but I don't get the add
handler lines and I'm not sure how the sender is working in your
example. Perhaps you can help me understand from this starting point.

Private Sub DaysMenuItem_Cl ick(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, MonMenuItem.Cli ck,
TueMenuItem.Cli ck, WedMenuItem.Cli ck, ThuMenuItem.Cli ck,
FriMenuItem.Cli ck, SatMenuItem.Cli ck
'how do I use sender here to check or uncheck the item clicked?
End Sub

GhostInAK wrote:
Hello cj,

private sub Form_Load(...)

AddHandler MenuItem1.Click , AddressOf MenuItemClickHa ndler
AddHandler MenuItem2.Click , AddressOf MenuItemClickHa ndler
...
AddHandler MenuItemN.Click , AddressOf MenuItemClickHa ndler

End Sub

Private Sub MenuItemClickHa ndler(byval sender as object, byval e as
EventArgs)

Dim tMenuItem as MenuItem = sender
tMenuItem.Check ed = Not tMenuItem.Check ed

End Sub
I would like to have menu items a main menu bar that represent the
days of the week. When you click on them they alternate from checked
to unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e
As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub
I would like to use the same sub to handle all 7 menu items. How
would I write this?


Jun 28 '06 #6
Hi Again,
First just a thought - you should probably only have one thread for this
discussion going at one time, IMHO.
The AddHandler is a way of 'connecting' an event to a routine to handle
it at runtime. Note that in Ghost's example that the MenuItemClickHa ndler
has no 'handles' clause at the end of it (unlike the other solutions you have
seen). He connects the event to the routine at run time through the use of
AddHandler.
Hope this helps.
--
Terry
"cj" wrote:
I love the way you change the menu item checked on an off
(tMenuItem.Chec ked = Not tMenuItem.Check ed) but I don't get the add
handler lines and I'm not sure how the sender is working in your
example. Perhaps you can help me understand from this starting point.

Private Sub DaysMenuItem_Cl ick(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, MonMenuItem.Cli ck,
TueMenuItem.Cli ck, WedMenuItem.Cli ck, ThuMenuItem.Cli ck,
FriMenuItem.Cli ck, SatMenuItem.Cli ck
'how do I use sender here to check or uncheck the item clicked?
End Sub

GhostInAK wrote:
Hello cj,

private sub Form_Load(...)

AddHandler MenuItem1.Click , AddressOf MenuItemClickHa ndler
AddHandler MenuItem2.Click , AddressOf MenuItemClickHa ndler
...
AddHandler MenuItemN.Click , AddressOf MenuItemClickHa ndler

End Sub

Private Sub MenuItemClickHa ndler(byval sender as object, byval e as
EventArgs)

Dim tMenuItem as MenuItem = sender
tMenuItem.Check ed = Not tMenuItem.Check ed

End Sub
I would like to have menu items a main menu bar that represent the
days of the week. When you click on them they alternate from checked
to unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e
As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub
I would like to use the same sub to handle all 7 menu items. How
would I write this?


Jun 28 '06 #7

cast sender to the sending object type and read the name property , this way
you can handle every control individual
regards

Michel Posseth [MCP]
"cj" <cj@nospam.nosp am> schreef in bericht
news:uW******** ******@TK2MSFTN GP02.phx.gbl...
yes but my goal is not to have all of them change SunMenuItem. It needs
to change only the one that was clicked.

Private Sub DaysMenuItem_Cl ick(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, MonMenuItem.Cli ck,
TueMenuItem.Cli ck, WedMenuItem.Cli ck, ThuMenuItem.Cli ck,
FriMenuItem.Cli ck, SatMenuItem.Cli ck
'how do I use sender here to check or uncheck the item clicked?
End Sub
M. Posseth wrote:
Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, SunMenuItem2.Cl ick,
SunMenuItem3.Cl ick, SunMenuItem4.Cl ick well you get the point i guess :-)
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

regards Michel Posseth [MCP]

"cj" wrote:
I would like to have menu items a main menu bar that represent the days
of the week. When you click on them they alternate from checked to
unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

I would like to use the same sub to handle all 7 menu items. How would
I write this?

Jun 28 '06 #8
cj
Agree on the thread but multiple people answer my question and I'm just
writing back to each of them instead of picking only one persons response.

what would be the advantage of using addhandler vs handles clause?
Terry wrote:
Hi Again,
First just a thought - you should probably only have one thread for this
discussion going at one time, IMHO.
The AddHandler is a way of 'connecting' an event to a routine to handle
it at runtime. Note that in Ghost's example that the MenuItemClickHa ndler
has no 'handles' clause at the end of it (unlike the other solutions you have
seen). He connects the event to the routine at run time through the use of
AddHandler.
Hope this helps.

Jun 28 '06 #9
cj
It doesn't seem to have a name property. I guess it's going by the
handle property or something.

{System.Windows .Forms.MenuItem }
BarBreak: False
Break: False
Checked: False
Container: Nothing
DefaultItem: False
Enabled: True
FindHandle: 0
FindShortcut: 1
Handle: 2009465263
Index: 3
IsParent: False
MdiList: False
MdiListItem: Nothing
MenuItems: {System.Windows .Forms.Menu.Men uItemCollection }
MergeOrder: 0
MergeType: Add
Mnemonic: Nothing
OwnerDraw: False
Parent: {System.Windows .Forms.MenuItem }
RadioCheck: False
Shortcut: None
ShowShortcut: True
Site: Nothing
Text: "Wednesday"
Visible: True
Michel Posseth [MCP] wrote:
cast sender to the sending object type and read the name property , this way
you can handle every control individual
regards

Michel Posseth [MCP]
"cj" <cj@nospam.nosp am> schreef in bericht
news:uW******** ******@TK2MSFTN GP02.phx.gbl...
yes but my goal is not to have all of them change SunMenuItem. It needs
to change only the one that was clicked.

Private Sub DaysMenuItem_Cl ick(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, MonMenuItem.Cli ck,
TueMenuItem.Cli ck, WedMenuItem.Cli ck, ThuMenuItem.Cli ck,
FriMenuItem.Cli ck, SatMenuItem.Cli ck
'how do I use sender here to check or uncheck the item clicked?
End Sub
M. Posseth wrote:
Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck, SunMenuItem2.Cl ick,
SunMenuItem3.Cl ick, SunMenuItem4.Cl ick well you get the point i guess :-)
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

regards Michel Posseth [MCP]

"cj" wrote:

I would like to have menu items a main menu bar that represent the days
of the week. When you click on them they alternate from checked to
unchecked. Right now I have 7 subs that look like this one:

Private Sub SunMenuItem_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles SunMenuItem.Cli ck
If SunMenuItem.Che cked Then
SunMenuItem.Che cked = False
Else
SunMenuItem.Che cked = True
End If
End Sub

I would like to use the same sub to handle all 7 menu items. How would
I write this?


Jun 28 '06 #10

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

Similar topics

0
1835
by: Anina | last post by:
It looks like the button control captures all of the clicks on the button no matter if the program is busy accessing code. For example: if the click event of a button has code that takes 10 seconds to process, and I click it, and I continue to click on the button 100 times *while the code is being processed* windows will queue all of those clicks, and the button click event will run 100 click events after it's done processing the original...
1
8167
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the first time the control is dynamically loaded, everything works fine. After that, if the control is loaded again from the page button event handler, the user controls events fail to fire on the first click NOTE: I (believe I) am rebuilding all...
5
9751
by: George Durzi | last post by:
I have a simple user control with a text box and a submit button. When the user enters some text into the textbox and clicks the submit button, a record is created in the database. I'm using this user control inside another webform. The webform has a "Next" button which is initially disabled. When the user control successfully adds a record I want the Next button on the webform to get enabled. Checking if the record was added...
1
2629
by: Martin | last post by:
Hi, I have produced a custom server control that simple outputs a row of 26 buttons, one button for each letter of the english alphabet. now what I would like to do is catch the button click inside the server control when it is clicked on. however when I click on a button at present I get an error saying that the An error has occurred because a control with auto-generated id
3
6759
by: Brian | last post by:
Hi, All, I want to create a single user control (component) with multi-controls. for example, I want to use one button control and 2 listBox controls to build one single user control. so, user can directely use the user control instead of 3 controls. They can click button to move the selected item from one listbox to another. but how can I inherit these 3 controls? And also, once I create the single user control, I can not implement the...
2
2670
by: John Dann | last post by:
This question has arisen from an earlier thread but is really a separate issue: I have a VB.Net listbox control on a form. I want to be able to do 2 things with items displayed within the one listbox. 1. Reorder items using drag and drop with the mouse. 2. Delete items eg by double-clicking or some other mouse-related action.
15
369
by: cj | last post by:
I would like to have menu items a main menu bar that represent the days of the week. When you click on them they alternate from checked to unchecked. Right now I have 7 subs that look like this one: Private Sub MonMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonMenuItem.Click If SunMenuItem.Checked Then SunMenuItem.Checked = False Else SunMenuItem.Checked = True
9
3191
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will load with different data (locations, departments, etc.).
9
5135
by: Rob | last post by:
When a custom control is used within a form, that control does not get events directly. I've run into this a couple times with both mouse and keyboard events. Ex: A custom control inherits from UserControl (or Panel, etc). If that particular control is being edited, it IS possible to assign handlers and get events. However, when the control is used on the main form, assigning a handler to the particular control does nothing--the...
0
10148
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
10002
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...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
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
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.