Building context menu on the fly

S

Sameh Ahmed

Hello there
Need to create a context menu in runtime, everything is ok except that I
need to check if a menuitem called "whatever" already exists in the context
menu.
Below is the code I use that gives an "Object reference not set to an
instance of an object" error
Any ideas how I can do that.
====================================================
For Each lvitem In lv1.SelectedItems '''''Depending on the item in the
selected items i want to add a MenuItem
objecttypemulti = lvitem.Tag.ToString.Substring(3, 3) 'to determine to
object type so know which MenuItems are needed
Dim menuitemtocheck As MenuItem
If objecttypemulti = 3 Then
menuitemtocheck.Index = 0 '''''Trying to give a value for the menuitem
If CM_LV1.MenuItems.Contains(menuitemtocheck) = False Then '''''here the
error is raised, i need to do that so i don't add a MenuItem more then one
time
tt = CM_LV1.MenuItems.Add("first actions")
tt.MenuItems.Add("machine hopa", New System.EventHandler(AddressOf
Me.LV_MenuItem_restartmachine))
End If
ElseIf objecttypemulti = 0 Then
tt = CM_LV1.MenuItems.Add("other Actions")
tt.MenuItems.Add("action whatever")
End If
Next
====================================================
the purpose is to add "first actions" and "other actions" only once even if
the object type in repeated more then once in the listview selected items.
Thanks in advance
Regards
Sameh
 
D

David

Hello there
Need to create a context menu in runtime, everything is ok except that I
need to check if a menuitem called "whatever" already exists in the context
menu.
Below is the code I use that gives an "Object reference not set to an
instance of an object" error

A couple of initial points. Code is a lot easier to read when it's
indented.
Any ideas how I can do that.
====================================================
For Each lvitem In lv1.SelectedItems '''''
objecttypemulti = lvitem.Tag.ToString.Substring(3, 3) 'to determine to
object type so know which MenuItems are needed
Dim menuitemtocheck As MenuItem

At this point, you've declared menuitemtocheck to be a MenuItem, but
haven't set it to an actual variable. So right now it's Nothing.
If objecttypemulti = 3 Then
menuitemtocheck.Index = 0 '''''Trying to give a value for the menuitem

And now you're trying to access the .Index property of Nothing, which is
going to throw an exception.

I'm not sure exactly what you're trying to do here, but in general you
can just loop over a menu's item collection to see if what you want is
there

For Each itemToCheck As MenuItem In CM_LV1.MenuItems
' check to see if itemToCheck matches what you're looking for
 
S

Sameh Ahmed

thanks you
I know that I was point to noting that's why I posted here:)
need to know how to assign menuitemtocheck a value?
is there a way to use the method "contains" instead of looping in the
MenuItems?
Regards
Sameh
 
D

David

thanks you
I know that I was point to noting that's why I posted here:)
need to know how to assign menuitemtocheck a value?
is there a way to use the method "contains" instead of looping in the
MenuItems?

In this case, it doesn't look like there is, although I'm admittedly not
sure exactly what you're trying to compare.
 

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