Results 1 to 3 of 3

Thread: Dynamic Create ToolBarButtons

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2003
    Location
    Amsterdam, Holland
    Posts
    36

    Dynamic Create ToolBarButtons

    I created a dynamic toolbar like hereunder:

    private sub createbuttons()
    -----------------------------------
    Dim tbutton As ToolBarButton
    For Each menuitem In menulist
    tbutton = New ToolBarButton()
    ...
    ...
    AddHandler tlbPackages.ButtonClick, AddressOf DynButtonClick
    tbrMenu.Buttons.Add(tbutton)
    Next

    Private Sub DynButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs)
    ----------------------------------------------------------------------------------
    MessageBox.Show(e.Button.Text)

    Problem:

    I created a handler for buttonclick. It seems to work fine and so it is. BUT when I click on the button de "DynButtonClick" will be executed 6 times and I will see the message multiple times.

    What am I doing wrong ???

    PLease Help

    Thanks in advanced
    AXH

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Dim tbutton As ToolBarButton
    For Each menuitem In menulist
    tbutton = New ToolBarButton()
    ...
    ...
    AddHandler tlbPackages.ButtonClick, AddressOf DynButtonClick
    tbrMenu.Buttons.Add(tbutton)
    Next
    I assume that tlbPackages is the toolbar itself, so maybe you should remove
    the Addhandler alltogether?!?. I think the problem is that you add six handlers to
    the same event. I don't think you need to add a handler to a toolbarbutton, because
    the toolbar uses the same events for all buttons.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2003
    Location
    Amsterdam, Holland
    Posts
    36
    I know the answer. The addhandler you have to define outside the loop and then it is working.

    By the way Thanks
    AXH

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