ContextMenuItem Visibility Change Dynamically

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

Hi Guys,

I'm completely new at this, and I've done a lot of Googling to try to find
the answer to this question, but I don't know if it can be done or whether
I'm searching for the right thing, so please bare with me and here goes...

I want to make an item on a context menu visible only if another
argument/variable exists. What command would I use to make the
contextmenuitem visible. Also is there a way to rename the contextmenuitem
based on an argument/variable? So if x = "1", text = "1" else text = "0"
(the syntax of that probably isn't right, but I'm only using it as a laymans
example).

I've tried things like me.visible = true, menuitem1.visible=true,
ContextMenu.MenuItems.Item visible=true etc but no joy. I've set the
initial visible property as false. I've looked at runtime context menus and
I think this may be the way to go if this isn't possible.

I hope this can be achieved.

Thanks.

Keith (total VB novice).
 
Keith said:
I want to make an item on a context menu visible only if another
argument/variable exists. What command would I use to make the
contextmenuitem visible. Also is there a way to rename the contextmenuitem
based on an argument/variable? So if x = "1", text = "1" else text = "0"

That's what the Opening event on the ContextMenu is for.
In there, you can do pretty much whatever you like to the menu
[sub-]items and, having done do, then decide whether to suppress the
context menu completely (so the user right-clicks, but nothing appears):

Private Sub ContextMenu_Opening( _
ByVal sender as System.Object _
, ByVal e as System.ComponentModel.CancelEventArgs _
)
e.Cancel = False

' Set up sub-items here

If nothing_to_see Then
e.Cancel = True
End If
End Sub
I've tried things like me.visible = true, menuitem1.visible=true,
ContextMenu.MenuItems.Item visible=true etc but no joy.

The .Visible property has always been plagued with "timing" issues -
whilst you're building a menu, the parent menu isn't visible so the
items themselves can't be visible, that kind of thing. This is why we
now have the .Available property instead (or as well, if you're feeling
brave).

Replace .Visible with .Available and see if it works any better.

HTH,
Phill W.
 
Phil you absolute star!!!!! Your suggestion was right on the money :)

All I had to do was drop my code in the contextmenu_opening section as
suggested and use the available property and it worked! Easy when you know
how hey. I think I'll see if I can do the name thing here aswell - in
theory it should work.

Just another question, I want my code to do IF x then x ELSE x

Is there a way to have it do more than one command in the else section? so
do ELSE x AND y AND z etc?

If String.IsNullOrEmpty(strData) Then Exit Sub Else

CustomTool1ToolStripMenuItem.Available = True

Can I add this on aswell at the end of the above line?
System.Diagnostics.Process.Start(strData, strdata2)


Thanks Phil, top marks!


Phill W. said:
Keith said:
I want to make an item on a context menu visible only if another
argument/variable exists. What command would I use to make the
contextmenuitem visible. Also is there a way to rename the
contextmenuitem based on an argument/variable? So if x = "1", text = "1"
else text = "0"

That's what the Opening event on the ContextMenu is for.
In there, you can do pretty much whatever you like to the menu [sub-]items
and, having done do, then decide whether to suppress the context menu
completely (so the user right-clicks, but nothing appears):

Private Sub ContextMenu_Opening( _
ByVal sender as System.Object _
, ByVal e as System.ComponentModel.CancelEventArgs _
)
e.Cancel = False

' Set up sub-items here

If nothing_to_see Then
e.Cancel = True
End If
End Sub
I've tried things like me.visible = true, menuitem1.visible=true,
ContextMenu.MenuItems.Item visible=true etc but no joy.

The .Visible property has always been plagued with "timing" issues -
whilst you're building a menu, the parent menu isn't visible so the items
themselves can't be visible, that kind of thing. This is why we now have
the .Available property instead (or as well, if you're feeling brave).

Replace .Visible with .Available and see if it works any better.

HTH,
Phill W.
 
I thought that it would work by just having multiple lines after the else
statement as long as there's and End If - this is what Google leads me to
believe, but it doesn't like the End If statement?! Gives me If must
precede End If...

Any ideas?

Keith said:
Phil you absolute star!!!!! Your suggestion was right on the money :)

All I had to do was drop my code in the contextmenu_opening section as
suggested and use the available property and it worked! Easy when you
know how hey. I think I'll see if I can do the name thing here aswell -
in theory it should work.

Just another question, I want my code to do IF x then x ELSE x

Is there a way to have it do more than one command in the else section?
so do ELSE x AND y AND z etc?

If String.IsNullOrEmpty(strData) Then Exit Sub Else

CustomTool1ToolStripMenuItem.Available = True

Can I add this on aswell at the end of the above line?
System.Diagnostics.Process.Start(strData, strdata2)


Thanks Phil, top marks!


Phill W. said:
Keith said:
I want to make an item on a context menu visible only if another
argument/variable exists. What command would I use to make the
contextmenuitem visible. Also is there a way to rename the
contextmenuitem based on an argument/variable? So if x = "1", text =
"1" else text = "0"

That's what the Opening event on the ContextMenu is for.
In there, you can do pretty much whatever you like to the menu
[sub-]items and, having done do, then decide whether to suppress the
context menu completely (so the user right-clicks, but nothing appears):

Private Sub ContextMenu_Opening( _
ByVal sender as System.Object _
, ByVal e as System.ComponentModel.CancelEventArgs _
)
e.Cancel = False

' Set up sub-items here

If nothing_to_see Then
e.Cancel = True
End If
End Sub
I've tried things like me.visible = true, menuitem1.visible=true,
ContextMenu.MenuItems.Item visible=true etc but no joy.

The .Visible property has always been plagued with "timing" issues -
whilst you're building a menu, the parent menu isn't visible so the items
themselves can't be visible, that kind of thing. This is why we now have
the .Available property instead (or as well, if you're feeling brave).

Replace .Visible with .Available and see if it works any better.

HTH,
Phill W.
 
Ok I figured it out.

Firstly I changed the syntax of my statements, and secondly it appears I
should have pressed enter after my first IF statement for it to then
automatically generate a THEN statement, and it linked to the END IF
statement.

This is what I now have:

If strData.Length > 0 Then

CustomTool1ToolStripMenuItem.Available = True

CustomTool1ToolStripMenuItem.Text = strdata3

End If



Thanks again.



Keith.

Keith said:
I thought that it would work by just having multiple lines after the else
statement as long as there's and End If - this is what Google leads me to
believe, but it doesn't like the End If statement?! Gives me If must
precede End If...

Any ideas?

Keith said:
Phil you absolute star!!!!! Your suggestion was right on the money :)

All I had to do was drop my code in the contextmenu_opening section as
suggested and use the available property and it worked! Easy when you
know how hey. I think I'll see if I can do the name thing here aswell -
in theory it should work.

Just another question, I want my code to do IF x then x ELSE x

Is there a way to have it do more than one command in the else section?
so do ELSE x AND y AND z etc?

If String.IsNullOrEmpty(strData) Then Exit Sub Else

CustomTool1ToolStripMenuItem.Available = True

Can I add this on aswell at the end of the above line?
System.Diagnostics.Process.Start(strData, strdata2)


Thanks Phil, top marks!


Phill W. said:
Keith wrote:

I want to make an item on a context menu visible only if another
argument/variable exists. What command would I use to make the
contextmenuitem visible. Also is there a way to rename the
contextmenuitem based on an argument/variable? So if x = "1", text =
"1" else text = "0"

That's what the Opening event on the ContextMenu is for.
In there, you can do pretty much whatever you like to the menu
[sub-]items and, having done do, then decide whether to suppress the
context menu completely (so the user right-clicks, but nothing appears):

Private Sub ContextMenu_Opening( _
ByVal sender as System.Object _
, ByVal e as System.ComponentModel.CancelEventArgs _
)
e.Cancel = False

' Set up sub-items here

If nothing_to_see Then
e.Cancel = True
End If
End Sub

I've tried things like me.visible = true, menuitem1.visible=true,
ContextMenu.MenuItems.Item visible=true etc but no joy.

The .Visible property has always been plagued with "timing" issues -
whilst you're building a menu, the parent menu isn't visible so the
items themselves can't be visible, that kind of thing. This is why we
now have the .Available property instead (or as well, if you're feeling
brave).

Replace .Visible with .Available and see if it works any better.

HTH,
Phill W.
 
Keith said:
Just another question, I want my code to do IF x then x ELSE x

Is there a way to have it do more than one command in the else section? so
do ELSE x AND y AND z etc?

*Always* stick to the multiple-line format of If..Then.
What you're using is the single-line form that, as you've discovered,
only allows for a single command. (Actually, you /can/ use ":" as a
statement delimiter to "bunch" things up, but don't!)

Break it up - it's much clearer to read in the long run:

If x Then
a()
b()
c()
Else
d()
e()
f()
Process.Start(strData, strdata2)
End If

HTH,
Phill W.
 
Back
Top