Results 1 to 10 of 10

Thread: Context Menu

  1. #1

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    41

    Context Menu

    Hey all,
    Can anyone tell me how to retrieve the index of the selected context menu item once it is chosen? Note I have multiple items in the context menu. If so, would you be so kind as to tell me how?

    As always, thanks for your help.
    Tomson

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Granting that all your menuitems are handled with one sub (which would be wise)....using addHandlers in your Form_Load or menu creation...

    VB Code:
    1. AddHandler MyMenuItem.Click, AddressOf MenuClick


    You then code a function below that all clicks on the menu are directed to:
    VB Code:
    1. Protected Sub MenuClick(ByVal sender As Object, ByVal e As EventArgs)
    2. Select Case CType(sender, MenuItem).Text
    3.  
    4.  Case "Copy"
    5.     'do something or call some function
    6.    
    7.  
    8.  Case "Paste"
    9.    'do something else or call some other function
    10.  
    11. End Select
    12. End Sub

  3. #3
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    another way of doing it and it only is worth while if you have the same context menu to appear on many in different screens, is to add the context menu

    Type in the menu options.

    Then double click the menu item and write the code you want to execute against that item.

    From then on it is cut and paste the same code into every screen.



    Not elegant, not great, but it works, if you need an example post again and I will supply.

    Richard

  4. #4

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    41

    Wink Hey thanks

    Both worked good. Now I have an additional question. How can I make the context menu act like a regular windows menu. That is when the item in the context menu is highlighted, I can fire an event to creat another context menu based on what has been highlighted? I am presuming this is possible.

    Thanks again for all your help.

    Tomson

  5. #5
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    why not have sub menu items so that when you click a context menu item, it simply reveals a set of sub menu options. Always remember that menu items can be built dynamically or in your program as well as through the designer.

    Can't thinkl of a way of having 2 different context menus on the same form, but without knowing what you want to do, hard to advise further.

    Actualy think about you are only allowed one main menu, so why should it be different for context menus.

  6. #6

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    41

    Question I'm not finding it.

    I don't see that context menus allows for submenu items. If it does could you direct me to how to do this?

    Thanks for your help.
    Tomson

  7. #7
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    at the bottom of your form there is a context menu control, click on it and context menu appears in the top left of the screen.

    Click on the option, exposes a sub menu item, click on that and type in the menu option name

    Double click that and enter your code

  8. #8

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    41

    Smile Okay here is the deal.

    I am trying to progrmatically add subitems to a context menu. I don't fully understand how the contextmenuMenuItems.AddRange() actually works. What I need is the Menu to display Jack and the subitem to be Rabbit. The second item needs to be Santa and its subitem to be Claus.
    I have to do this in code and not on the contextmenu itself. The reason, I am hitting a database and populating the menu with items in it. Then based upon what is populated and chosen in the menu it will then have to populate the subitems from another table. It is ever changing. Names can be added and taken away at any given time.

    If this can be done, I would appreciate you showing/directing me how.

    Thanks for your help
    Tomson.

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    What you are asking is possible and actually easy. I was able to populate a menu with its submenu's using the code below.

    Instead of tring to figure out the code though, you should read up on MSDN and here's an article to help you:

    http://www.syncfusion.com/FAQ/WinForms/FAQ_c49c.asp

    VB Code:
    1. Private Sub ContactFormMainInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.  
    4.         Me.btn_AddContactType.ContextMenu = New ContextMenu
    5.   'add the menuitems
    6.         For i As Integer = 0 To Me._formDictionaryDataSet.Tables(2).Rows.Count - 1
    7.             Me.btn_AddContactType.ContextMenu.MenuItems.Add _ (New MenuItem(CType(_formDictionaryDataSet.Tables(2).Rows(i).Item(1), String), New System.EventHandler(AddressOf addContactTypeHandler)))
    8.         Next
    9.  
    10.    End Sub
    11. Private Sub btn_AddContactType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _  Handles btn_AddContactType.Click
    12.  
    13.   'show the menu
    14.         Me.btn_AddContactType.ContextMenu.Show(btn_AddContactType, New Point(0, btn_AddContactType.Height))
    15.     End Sub
    16.  
    17.     Private Sub addContactTypeHandler(ByVal sender As Object, ByVal e As System.EventArgs)
    18.         'change form in panel based on form selected in menu
    19.  
    20.         Me.Panel1.Controls.Clear()
    21.  
    22.         Dim s As String = sender.text
    23.  
    24.         Select Case s
    25.             Case "Add Person"
    26.  
    27.                 _PanelForm = New PersonInfoForm
    28.  
    29.             Case "Add Business"
    30.                 _PanelForm = New BusinessInfoForm
    31.            
    32.         End Select
    33.        
    34.     End Sub
    Last edited by nemaroller; Dec 15th, 2003 at 03:08 PM.

  10. #10
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    Nemaroller gave you the perfect answer, except here is a tip, create a new project just to experiment with context menus and adding sub menu items, that way you can learn the technique and then apply it to your application.

    You know also explore the Treeview control, it might also suit you needs

    Just a thought

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