locked
vb.net popupmenu? RRS feed

  • Question

  • What ever happened to the popupmenu function?

    In vb6 it was

    Popupmenu mnuname

    how can i do this in vb.net?

    I want a menu to popup where i clicked when i right click on a listview item
    Saturday, February 4, 2006 4:12 AM

All replies

  •  

    That's now called:

    System.Windows.Forms.ContextMenu

    Check it out in the documention and object browser.

    Saturday, February 4, 2006 4:20 AM
  • First drag a Context Menu to form, then add a ListBox and put some items...then add this code:-

    ' This 2 var holds ur mouse move location inside listbox

    ' require to display menu

    Dim x1, y1 As Single

    Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove

    ' Update x and y

    Me.x1 = e.X

    Me.y1 = e.Y

    End Sub

    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown

    ' if it is right click then

    If e.Button = MouseButtons.Right Then

    ' create some menu items; or u can create it in design time

    Me.ContextMenu1.MenuItems.Clear()

    Me.ContextMenu1.MenuItems.Add("VB.NET is great")

    Me.ContextMenu1.MenuItems.Add("You selected: " & Me.ListBox1.SelectedItem)

    Me.ContextMenu1.MenuItems.Add("Thats all! Folks.")

    ' Done! now show the menu where u click

    Me.ContextMenu1.Show(Me.ListBox1, New Point(Me.x1, Me.y1))

    End If

    End Sub

    -- hope that helps

    Saturday, February 4, 2006 6:51 PM
  • Note that, while the .NET 1.x ContextMenu class still exists, you should use the ContextMenuStrip class in .NET 2.0.  Most members are either the same or equivalent.  For instance, the ContextMenuStrip.Items property is equivalent to the ContextMenu.MenuItems property.  The ContextMenuStrip is more attractive and more functional than the ContextMenu, partly because it is a control, whereas the ContextMenu is a component.

    What you would normally do is create your ContextMenuStrip in the designer and then assign it to the ContextMenuStrip property of your ListBox, also in the designer.  Then that menu will be shown automatically whenever the user right-clicks your ListBox.  All controls have a ContextMenuStrip property, so the only reason not to use the design time support is if you don't know what the menu items will be until run time.  Even then you should probably still create an empty menu at design time and simply populate it at run time.

    • Proposed as answer by Cor Ligthert Tuesday, March 8, 2011 8:50 AM
    Sunday, February 5, 2006 1:34 AM
  • @jmcilhinney Contextmenustrip is typically vb6 popupmenu suport plain and simple. Make me wonder what is the point of learning .net if we're going backwards eliminating things that worked best. The solution above was good to me, but then you said to use contextmenustrip that solution not even supported in framework 1. compatability issues there... makes me think other programming alternatives. Here is the big question, If I code my application in .net frame work 1, will it run in x86/x64 Amd64 on core duo processors on all windows including windows 7 and future? I hate to rewrite my code its too expensive to do so. picawho@hotmail.com
    • Edited by WiseFrank Monday, August 2, 2010 9:19 AM Ms Compatibility issues
    Monday, August 2, 2010 9:09 AM
  • Hi Arindam_,

    I have never used the ContextMenuStrip control so out of curiosity I tried your code in a sample app.

    When I tried to run your code I got build errors. It needed the following changes:

          Me.ContextMenuStrip1.Items.Clear()
          Me.ContextMenuStrip1.Items.Add("VB.NET is great")
          Me.ContextMenuStrip1.Items.Add("You selected: " & Me.ListBox1.SelectedItem)
          Me.ContextMenuStrip1.Items.Add("Thats all! Folks.")
          ' Done! now show the menu where u click
          Me.ContextMenuStrip1.Show(Me.ListBox1, New Point(Me.x1, Me.y1))
    

    As you can see ContextMenu1 is changed to ContextMenuStrip1 and .MenuItems is changed to .Items.

    But thanks for the lesson, I now know more that what I did before.

    Regards Ron

    Tuesday, August 3, 2010 3:18 AM
  • I agree with WideFrank and will add that now programming an object-sensitive menu demands creating several objects: main menu for the screen and several context menu, one per  control. I can't call a forced duplication of code a good programming practice. 

     

    popup menu was too good to change, no matter how it is called now. 

     

    Peace to all programmers,

    Alexander

     


    learn it from cover to cover
    Monday, November 29, 2010 7:36 PM
  • Do you realize that the post you responded to is over 4 years old?  The OP may never even see your response!
    Tuesday, November 30, 2010 6:37 PM
  • The reply still provides useful information...
    Thursday, January 13, 2011 5:53 PM
  • Say the (Name) of the contextmenustrip is ContextMenuStrip1.

    One way is if the listview has the property contextmenustrip, then you just set as "ContextMenuStrip1" like a string.

    If there is no property listview then:

    In the event of the mouserightclick put down

    ContextMenuStrip1.Show()
    ContextMenuStrip1.Left = Cursor.Position.X
    ContextMenuStrip1.Top = Cursor.Position.Y

    Hope that helps.

    Sunday, March 6, 2011 6:46 PM
  • The reply still provides useful information...

    Maybe, but could you not do it with a letter as that is the tendency of the message.

    "Why use new methods as there are still vintage methods which have ever done what was intended".

     


    Success
    Cor
    Tuesday, March 8, 2011 8:53 AM
  • I, too, agree with WiseFrank. But what do you expect from a company that goes to trouble and ENORMOUS expense of creating a grotty language like C# with unreadable semicolons?? If the C Programmers couldn't understand VB.Net then they shouldn't be programming. What a total waste of money and time by MS.

     

     

    Monday, October 17, 2011 12:56 PM