Results 1 to 6 of 6

Thread: runtime menu & handler

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    74

    runtime menu & handler

    im creating a runtime menu and im trying to add a click event to them, but i need help
    Code:
    Dim WithEvents mnuSubItem As Menu
    Code:
    Me.Controls.Add(myMenu)
    Code:
    Dim MainMenufile1 As ToolStripMenuItem = New ToolStripMenuItem
    MainMenufile1.Text = "File"
    now for a sub menu that i want to click on i have
    Code:
    Dim MainMenufirstsub1 As ToolStripMenuItem = New ToolStripMenuItem
            MainMenufirstsub1.Text = "Clear"
    
    myMenu.Items.Add(MainMenufile1)
    MainMenufile1.DropDownItems.Add(MainMenufirstsub1)
    
    AddHandler MainMenufirstsub1.Click, AddressOf MainMenufirstsub1.Click ?
    the menu works fine but, i cant seem to get the handler to work correcty
    thx

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: runtime menu & handler

    vb Code:
    1. AddHandler MainMenufirstsub1.Click, AddressOf MainMenufirstsub1_Click
    2.  
    3. Private Sub MainMenufirstsub1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    4.      'your menu code here
    5. End Sub

    you can use the sender argument to determine which menu was clicked

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    74

    Re: runtime menu & handler

    im a little confused by this runtime menu
    i tried what you said
    Code:
    AddHandler MainMenufirstsub1.Click, AddressOf MainMenufirstsub1_Click
    and i get name MainMenufirstsub1_Click is not declared

    no matter what i put for adress of its not declared

    so if my mainmenufirstsub is the one i want to click then this should be right?

    im lost

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: runtime menu & handler

    Quote Originally Posted by .paul.
    vb Code:
    1. AddHandler MainMenufirstsub1.Click, AddressOf MainMenufirstsub1_Click
    2.  
    3. Private Sub MainMenufirstsub1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    4.      'your menu code here
    5. End Sub

    you can use the sender argument to determine which menu was clicked
    you need to add the sub as shown above. you can use the same handler for more than 1 menuitem, + you use the sender argument to determine which menu was clicked

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    74

    Re: runtime menu & handler

    you can use the same handler for more than 1 menuitem, + you use the sender argument to determine which menu was clicked
    could ya give me an example of how i could do that, because thats what i want to be able to do

    ya i should have known about the sub. for some reason i thought i could find it in the declarations.
    thx
    Last edited by mrsirpoopsalot; Feb 3rd, 2008 at 01:24 PM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: runtime menu & handler

    this adds 10 menuitems + displays a msgbox when you click on them

    vb Code:
    1. for x as integer = 1 to 10
    2.      Dim MainMenufirstsub1 As ToolStripMenuItem = New ToolStripMenuItem
    3.      MainMenufirstsub1.Text = "Menu" & x.tostring
    4.      MainMenufirstsub1.tag = x
    5.      MainMenufile1.DropDownItems.Add(MainMenufirstsub1)
    6.      AddHandler MainMenufirstsub1.Click, AddressOf MainMenufirstsub1_Click
    7. next
    8.  
    9. Private Sub MainMenufirstsub1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)    
    10.      msgbox "you clicked menu" & directcast(sender, menuitem).tag
    11. end Sub

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