Changing Context Menu Item's Text Property on a Tray Icon [Resolved]
How do I change the the text for a menu item. I'm sure it should be as simple as menuitem.Text = "Caption". The problem is that it sometimes changes once or not at all. I have the OwnerDraw property set to False.
If I check to caption of the menu item in code, it displays the correct caption but it doesn't display it in the menu, as though it needs to be refreshed or something.
Any Ideas?
Last edited by Ideas Man; Sep 17th, 2004 at 08:07 AM.
I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)
Originally posted by billcoupe Just a thought, are you refreshing the menu item to update the display?
-Bill
And how would i do that?
And yes, I think I already said that I set the caption the usual way, but it changes once and not anymore, but if you query the text property, it says that it holds the correct caption.
I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)
I looked into it a bit further... Seems you can change the 'drop down portion, Issue a "me.refresh()" and the change always shows up the next time the main menu item is clicked on.
The exception I found was on the main menu bar... seems if you try to change the main bar item in the click event of a mainbar item it doesn't work.
It does work if you're attempting to make the item morph from another method.
Code:
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
MenuItem3.Text = "Works"
Me.Refresh()
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
MenuItem3.Text = "Hello"
Me.Refresh()
End Sub
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
MenuItem4.Text = "Clicked #4"
Me.Refresh()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'will change the item on the main menu bar
MenuItem1.Text = "Clicked Button"
Me.Refresh()
End Sub
OK, I did a little more digging and found out that it does indeed update the context menu's text if you attach it to a form, however, if you attach it to a tray icon, it does not. If you change the text and bring up the context menu on a form however, when you then pull up the context menu on the tray icon, it also shows the changes.
No they way I see it, it appears to be loading a 'cached' copy of the menu or something. Does anyone know how to force it to get the new menu?
skarho, did you ever try changing the text for the menus for a tray icon on your "time machine" or just for a form?
I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)
yep. refresh is not a public function on contextmenu object. but i'm not sure though. i use vs.net 2k2. i found also that my code doesn't work with notifyicon but if i set it to form, it doesn't update automatically. it waits for the pop up on form then after, it updates to the notifyicon. i don't know why this damn thing happens.
I'm assuming again... that 'MenuItem4' is part of 'ContextMenu1', what happens if, after you change the value, you reassign the menu contents to the 'NotifyIcon1.ContextMenu' ?
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
NotifyIcon1.ContextMenu = ContextMenu1
Me.ContextMenu = ContextMenu1
End Sub
Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
MenuItem4.Text = TextBox1.Text
NotifyIcon1.ContextMenu = ContextMenu1
End Sub
I'm thinking that because the two objects are technically separate as they're not on the same 'form' they don't automatically both 'see' changes to 'Contextmenu1'... just a thought.
If possible, why don't you post your app code... I'll be happy to load it up and see if I can get this to work. Sorry about posting my ideas without testing them, but I've pressed for time the past week or so.. I won't do it again.
If you upload the code, I'll post a fix if I can make it work.
Well, I can't post my code, but I made this little app that does the same thing, if you can fix it in this, it'll fix it in my app.
Simply change the text value of the textbox and click the button, preview the menu on the tray icon, you'll see it doesn't change, preview it on the form and then on the tray icon and it does. Note: I do not want the thing bounded to the form, it is there for demonstration purposes only.
I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)
Well, I managed to find a working solution. For all those who have the same problem (I'm sure I'm not alone), to fix it, you need to set the visibility of the parent menu to False and then True like this:
VB Code:
MenuItem2.Text = "Sub-Menu Caption"
MenuItem1.Visible = False
MenuItem1.Visible = True
Where the menu structure looks like this:
MenuItem1 -> MenuItem2
^Parent.............^Sub menu
Thanks to all those who helped w/ their suggestions and for contributing their time to assist me w/ this problem.
BTW, this is a known bug.
I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)