Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I have some code under the ContextMenu's popup event that should change the text property of a menuitem at runtime... but it only does it the first time. After that it refuses to change the text property...

 

Any help would be great...:confused:

Posted

Don't have my code with me right now, but it is basically this:

 

ContextMenu_Popup(variables...) Handles ContextMenu.Popup
   If(true) Then
       mnuItem.text = "Hello"
   Elseif(true) Then
       mnuItem.text = "World"
   End if
End Sub

 

Now, lets say the IF statement is true when the contextmenu is first popped up, the text on mnuItem will change to HELLO. But, if I go popup the menu again with the ELSEIF statement true and the IF statement FALSE, the text on mnuItem will not change... it will remain HELLO. I used message boxes to check if the code is running the proper if statement and it is... but the text won't change on the menu item... It's really strange because I never had a problem doing this in VB6.

Posted

This should work fine

ContextMenu_Popup(variables...) Handles ContextMenu.Popup
   If mnuItem.Text="World" Then
       mnuItem.text = "Hello"
   Else
       mnuItem.text = "World"
   End if
End Sub

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted (edited)

Hello,

 

Thanks for the reply... I put that exact code in and when I accessed the menu the first time it changed the text to HELLO because I set it to WORLD at design time, but if I access the menu item a second time, it doesn't change the text to WORLD like it should. It keeps it as HELLO. I'm not sure why it is doing this. I've done this numerous times in VB6 so it should work in here, but it won't update it.... I have no clue.. :confused:

 

EDIT: I just noticed this also. The menu that I am working with is not on the main level of the context menu. For example, the tree of the context menu looks like this:

 

-ContextMenu
   -mnuItem1
       -mnuItem2

 

So, mnuItem2 is in a submenu of ContextMenu. I am trying to change the text on mnuItem2. Here is the strange part... if I try to change the text of mnuItem1, the above code works fine. The code doesn't work for mnuItem2 though. I can even disable mnuItem2, I just can't change its text regardless of whether it is enabled or not...

Edited by Audax321
  • Leaders
Posted

why not do something like this :

Dim x As Integer = 0
'/// make an integer thats available to all sub ( so it will keep counting )
'//////////////////////

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ContextMenu1.Show(Button1, New Drawing.Point(0, 15))
   End Sub

   Private Sub ContextMenu1_Popup(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
       If x <> 0 Then
           If MenuItem1.Text = "HELLO" Then
               MenuItem1.Text = "WORLD"
           End If
           If MenuItem2.Text = "GOODBYE" Then
               MenuItem2.Text = "CYA"
           End If
       End If
       x = 1
   End Sub

Posted
Well, I have the ContextMenu set as the contextmenu for a NotifyIcon. And I'm not sure what you mean by having an x variable. It still won't allow me to change the text property of a menuitem that is not on the main level of the context menu more than once...
Posted
Try placing a Break Point in the Popup function to see when it fires
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted

Hello,

 

I put the break point in and the code is run before the menu pops up. I have other code in there that changes the menuitem.enabled property of some of the controls and that code works fine. It just has a problem with the menuitem.text property.

 

THanks...

Posted

Okay this is odd:

 

This will not work because MenuItem2's text will not update.

 

Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
       Static x as integer
       
       x = x + 1

       If (MenuItem1.Text = "Menu1") Then
           MenuItem1.Text = "Menu2"
       Else
           MenuItem1.Text = "Menu1"
       End If

       MenuItem2.Text = x

   End Sub

 

BUT, this will work:

 

Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
       Static x as integer
       
       x = x + 1

       If (MenuItem1.Text = "Menu1") Then
           MenuItem1.Text = "Menu2"
       Else
           MenuItem1.Text = "Menu1"
       End If

       MenuItem2.enabled = False
       MenuItem2.Text = x
       MenuItem2.enabled = True

   End Sub

 

Notice how in the second one I disable the control then enable it. I am guessing this causes VB to redraw the control?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...