same sub, multiple control click events

C

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 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
End If
End Sub

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

Chris Dunaway

cj said:
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
End If
End Sub

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

Just add additional items to the handles clause such as:

Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MonMenuItem.Click, TueMenuItem.Click,
WedMenuItem.Click

End Sub

Then inside the click event, the sender which is passed in is the menu
item that was clicked.


Hope this helps,

Chris
 
G

Guest

In VB6 you would do this with 'control arrays'. I only mention this because
if you look up 'control arrays' in the VB .Net help index, it will give you a
complete discussion of how to do this kind of thing in VB .Net. along with
examples.
 
C

cj

I'm familiar with control arrays but .net doesn't do them. And even if
it did I'm not sure that control arrays worked with menu items and these
are main menu items.
 
C

cj

Yes, this is what I was thinking but how does sender tell me which one
is clicked? I don't know how to use sender.

Private Sub DaysMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles SunMenuItem.Click, MonMenuItem.Click,
TueMenuItem.Click, WedMenuItem.Click, ThuMenuItem.Click,
FriMenuItem.Click, SatMenuItem.Click
'how do I use sender here to check or uncheck the item clicked?
End Sub
 
G

Guest

Yes, control arrays did apply to menu items, and no, you can't use them in VB
..Net whcih is why there is an article to explain what to do instead! The
reason I mentioned them is that if you look it up in the .net help, it will
explain how to do what you want - did you look it up?

But to make things easier for you - in your case you really don't need to
know whcih one was clicked - the following code in the routine will do the
trick.
dim TSMI as ToolStripMenuItem
TSMI = Ctype(Sender, ToolStripMenuItem)
TSMI.Clicked = Not TSMI.Clicked
.....
 
C

cj

yea, I did look it up. I what you are talking about now in response to
some of the other folks responses to my question. I don't reall get the
byval sender thing. It works but why? And I don't want to mimic
collections with the tag property like they also suggested.
 
G

Guest

Hi,
Remember that 'Sender' is simply a reference (pointer) to the actual
Object that the event took place on. Since your procedure only 'handles'
click events for certain ToolStripMenuItems', you can safely cast it to that
type and then toggle its Checked Property. Hope this helps.
 
G

Guest

Ok, you need to look at what it means to pass a reference to an object byval
versus byref. When you pass an object reference byval it is NOT a copy of
the Object that gets passed- its a copy of the reference! So you are getting
a new pointer to the original object! When you pass a reference byref, your
are getting a pointer to the original reference variable! In both cases, you
access the original object through this pointer, but in the second case you
can modify the passed reference. For example. setting to to 'Nothing' will
cause the argument that was passed (in the calling routine) to now have a
value of 'Nothing'.
Hope this helps,
 
C

cj

Yea, when sender is sent ByVal Sender as System.Object what is being
sent is a pointer to the system object
 
G

Guest

Yes, and in this case that Object is whatever ToolStripMenuItem that was
clicked. So you can cast the System.Object to a ToolStripMenuItem and toggle
its 'Checked' property.
 
C

Chris Dunaway

cj said:
Yes, this is what I was thinking but how does sender tell me which one
is clicked? I don't know how to use sender.

One method is to assign some data to the Tag property of the menuitem
so that you can use it with the sender:

Dim mi As MenuItem = DirectCast(sender, MenuItem)
Dim tagString As String = mi.Tag.ToString

Select Case tagString
Case "Some String"
'Do something
Case "Some other string"
'Do something else
Case "A third string"
'Do something completely different
End Select

You can also use the Is operator:

If sender Is MenuItem1 Then
'Do something
Else
If sender Is MenuItem2 Then
'Do something else...
End If
End If
 
C

cj

Thanks for the ideas. I'm not a fan of using the tag property for this
kind of thing. Some other folks have helped me create this:

Private Sub DaysMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles SunMenuItem.Click, MonMenuItem.Click,
TueMenuItem.Click, WedMenuItem.Click, ThuMenuItem.Click,
FriMenuItem.Click, SatMenuItem.Click
Dim ThisMenuItem As MenuItem = sender
ThisMenuItem.Checked = Not ThisMenuItem.Checked
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top