Menu Question.

  • Thread starter Thread starter I_AM_DON_AND_YOU?
  • Start date Start date
I

I_AM_DON_AND_YOU?

I have a Menu with 5 Menus as:

Menu Name Menu Text
M1 File
M2 Edit
M3 View
M4 Insert

I want to execute the code depending upon which menu is clicked. Since most
of the code is same therefore, I want to write the one procedure. Therefore
I tried to write something like...

Private Sub Menu_Click (ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles _
M1.Click, M2.Click, M3.Click, M4.Click

******************
*****************
*************

x = sender.text
msgbox (x)

********
End Sub

When I click the message box shows the Text property of menu i.e. if I click
M1 it shows "File", if I click M2 it shows "Edit" etc. However, I want to
show Name i.e. when I click M1 it should show M1, when I click M2 it should
show M2, and so on.

The second thing I want to ask is that why is that when we type SENDER and
the type dot (.) it doesn't show properties or methods associated with the
sender. However, still we can type TEXT. How would we know what other
methods or properties are associated with SENDER OBJECT.

Thanks in advance!
 
I can handle the second half of oyur question.

the SENDER object is late bound - it is defined purely as an "object", it
doesn't actually take the form of a menu item until runtime. Therefore the
intellisense in the IDE doesn't know the methods that could be applicable as
it doesn't know the type of object it is .

HTH

Simon
 
Hi Don,

I still get a chuckle out of that name! And you? ;-))

Sender is, as Simon pointed out, a 'pure object' which Intellisense can't
fathom beyond its 'objectness'.

You, on the other hand, know that it is always going to be a MenuItem so
you can cast it as such.
Dim oMenuItem As MenuItem = DirectCast (sender, MenuItem)

Now, with oMenuItem, you can use '.' to your heart's content. Except, that
is, if you want the Name property of the MenuItem. Why? Because while you can
set the Name when you Design your menu on the Form, the MenuItem 'control'
doesn't <have> a Name. Why? I don't know. But the Control class, which most
controls inherit from, is the one that provides the Name property. And
MenuItem doesn't inherit from Control.

How important is it that you have the actual name of the MenuItem ? Ie.
what do you want to do with it?

Regards,
Fergus
 
How important is it that you have the actual name of the MenuItem ? Ie.
what do you want to do with it?

Actually I just wrote the example. In my actual program the menu is very
(very very very ) big comprising of more than 300 options in different
menus, sub-menus. So when I was designing the Menus I designed by giving
name like M1, M2, M3, M4......... N1, N2, N3....................... These
are Menu Name. The Menu Names (what we call caption in VB 6.0) are very big
like "PREVIOUS ENTRY", "FILE PRINT".... so I thought I should write coding
based upon the menu names.

That's why I want to know how to access the menu name in the code.

In VB 6.0 we have two properties
(1) menuname.name
(2) menuname.caption
 
Hi Don,

Very ^ 4 = 300. Oh yes, that's big!

You can still use the MenuItem name in code, you just can't use its name
as text.

Private Sub Menu_Click (...) Handles _
M1.Click, M2.Click, M3.Click, M4.Click
If sender Is M1 Then _
MsgBox ("It's M1")
If sender Is M2 Then _
MsgBox ("It's M2")
etc

But I'm thinking that that may not do the trick. What is you that you
actually want to achieve?

Regards,
Fergus
 
The second thing I want to ask is that why is that when we type SENDER
and the type dot (.) it doesn't show properties or methods associated
with the sender. However, still we can type TEXT. How would we

Make sure you have Option Strict On. Then when you cast sender to a
menuitem (or other object depending on the event), you should see all the
properties of the correct type when you type the dot.

Chris
 
Hi Don, try this:

Private Sub OnMenuClick(...) Handles ...

Dim miMenu As MenuItem = DirectCast(sender, MenuItem)

Select Case True
Case Is M1
' M1
Case Is M2
' M2
End Select

End Sub

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Magic. You need to pretend that I've casted "True" to sender. ;-) (Nice one
Fergus)

Correction:

' ///
Select Case True
Case sender Is M1
' M1
Case sender Is M2
' M2
End Select
' ///

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Back
Top