Results 1 to 39 of 39

Thread: Changing Context Menu Item's Text Property on a Tray Icon [Resolved]

  1. #1

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    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)

  2. #2
    Member skarho's Avatar
    Join Date
    Aug 2004
    Location
    Cebu, Philippines Occupation: Official Janitor SSS Contribution: 0 Pesos Certifications: None
    Posts
    53
    dunno if this helps but this works
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.       mnuchangeable.Text = TextBox1.Text
    3.    End Sub

  3. #3

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    I thought I said that doesn't work. Well, if I didn't, that doesn't work. It changes it's property fine, but it doesn't reflect it in the UI.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  4. #4
    Member skarho's Avatar
    Join Date
    Aug 2004
    Location
    Cebu, Philippines Occupation: Official Janitor SSS Contribution: 0 Pesos Certifications: None
    Posts
    53
    it reflected on my time machine

  5. #5

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Try changing it more than once while it is running, sometimes I can change it once and then it stops.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  6. #6
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    5
    what is actually calling it to change?

    i have tried it with a few buttons changeing it and i am haveing
    no problem at all with

    miName.text = "Caption"

    later,
    Blackie

  7. #7
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Just a thought, are you refreshing the menu item to update the display?

    -Bill

    CP.Solutions

  8. #8

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    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)

  9. #9
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    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
    Hope this helps

  10. #10

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    I'm using the context menu for a tray icon, and it still doesn't work . But the main menu works now.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  11. #11
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    I'm glad at least part of the problem is gone now!

    I'm about to call it a night, but I will look into the context menu/tray icon thing tomorrow.

    If I find a fix for that I'll post it for you.

  12. #12

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Bump.

    Someone must know, I find it hard to believe I'm the only one who's tried this.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  13. #13
    Member skarho's Avatar
    Join Date
    Aug 2004
    Location
    Cebu, Philippines Occupation: Official Janitor SSS Contribution: 0 Pesos Certifications: None
    Posts
    53
    dunno about your situation but works fine on my time machine
    VB Code:
    1. Private Sub ContextMenu1_Popup(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
    2.         mnuchangeable.Text = TextBox1.Text
    3.     End Sub

  14. #14

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    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)

  15. #15
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    ideas, i'm him

    i just did it on the form

  16. #16

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Ah, ok. Can you try it for a tray icon and post back if you have any results?
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  17. #17
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    still work in my time machine mate
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         NotifyIcon1.ContextMenu = ContextMenu1
    3.     End Sub
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         MenuItem1.Text = TextBox1.Text
    7.     End Sub

  18. #18

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    OK... now try it as a submenu of the main menu of a context menu i.e.


    x
    x
    x>y
    ....y << The ys.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  19. #19
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    yep. now, i follow you. it doesn't update. me dunno the details mate.

  20. #20

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Dame, hopefully someone does...
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  21. #21
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    try this mate. this works right after i post it doesn't update. hope this works for you.
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         NotifyIcon1.ContextMenu = ContextMenu1
    3.         Me.ContextMenu = ContextMenu1
    4.     End Sub
    5.  
    6.     Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
    7.         MenuItem4.Text = TextBox1.Text
    8.     End Sub
    Code:
    contextmenu1
    menuitem1
    menuitem2-menuitem3
              menuitem4

  22. #22

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Nope that doesn't work. And I only used the form context menu thingy as an example, the menu is only attached to the tray icon in my app.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  23. #23
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Hi folks....

    What happens if you change the code to add a 'refresh' after you change the menu?

    Originally posted by brown monkey
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         NotifyIcon1.ContextMenu = ContextMenu1
    3.         Me.ContextMenu = ContextMenu1
    4.         [COLOR=red]NotifyIcon1.ContextMenu.refresh()[/COLOR]
    5.     End Sub
    6.  
    7.     Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
    8.         MenuItem4.Text = TextBox1.Text
    9.     End Sub

  24. #24

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    That would be all fine if that was an actual property.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  25. #25
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    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.

  26. #26

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Same thing w/ mine in Visual Studio .NET 2003.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  27. #27
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Sorry... I 'assumed'... I should have known what that would lead to.

    I did find an MS KB article that seems to tie into this anomaly:

    827043

    There's also a related link from that page:

    ContextMenu

    I hope there's a tidbit in these that helps.

  28. #28

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    That was a good find, but unfortunatly, it doesn't apply because...

    1. OwnerDraw is set to False
    2. The items do display on the menu
    3. It's the submenus of the context menu that don't reflect properly.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  29. #29
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    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:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.    NotifyIcon1.ContextMenu = ContextMenu1
    3.    Me.ContextMenu = ContextMenu1
    4. End Sub
    5.  
    6. Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
    7.    MenuItem4.Text = TextBox1.Text
    8.    NotifyIcon1.ContextMenu = ContextMenu1
    9. 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.

  30. #30

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    That doesn't work either. If possible, can you please test your theories before posting them?
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  31. #31
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    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.

  32. #32

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    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.
    Attached Files Attached Files
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  33. #33
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Ideasman.. thanks

    I'm wrapping up a demo I have to present in the morning tonight. I'll play around with this when I get back in the office tomorrow.

  34. #34

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    OK, thanks. Just hope you can find a solution.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  35. #35
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Ok... This works... it's not elegant but it works.

    Let me know if it'll be ok for your needs.
    Attached Files Attached Files

  36. #36

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    You're not gonna like this, but it doesn't work either .
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  37. #37

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    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:
    1. MenuItem2.Text = "Sub-Menu Caption"
    2. MenuItem1.Visible = False
    3. 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)

  38. #38
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    That's interesting...

    One of the things I tried first, was flashing the 'Visible' property as you suggested, that did not work for me.

    When I set the NotifyIcon1 object to nothing and then 're-instantiated' it as in the code I uploaded, that worked for me....

    This is indeed a very strange behaving issue...

    Thanks for updating me (and everyone) on what worked for you.

    Can you post a URL to an MS knowledgebase article that addresses this as a known bug? I'd love to read what they say.

    Thanks, and I'm glad it's all working now.

  39. #39
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    I found the KB article, for anyone who might be interested.

    It specified exactly what Ideasman did, now I have to figure out why that didn't work for me

    Notify Icon Bug KB Article

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width