Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I need a bit of a kick-in-the-rear to get me on the right track. I have programmatically added buttons to a Panel, but I now need to handle the click event of each of those buttons.

 

I have a sub-routine that creates an instance of a Button and adds it to the Panel.Controls collection. Once the end of that sub-routine is reached, the instance of those buttons are out of scope.

 

Is there a way to programmatically create the event handlers for the buttons in the Panel.Controls collection or do I need to Declare a Button Array and add them to that in order to support event handling? Does that make sense?

 

Private Sub MakePanelControlTop()

  Dim buttonAdd As New Button
  With buttonAdd
     .FlatStyle = FlatStyle.Flat
     .Name = Me.BUTTON_NAME_ADD
     .Text = Me.BUTTON_TEXT_ADD
  End With
  PanelControlTop.Controls.Add(buttonAdd)
  Me.MakeToolTip(buttonAdd, Me.TOOLTIP_TEXT_BTNADD)

  Dim buttonRemove As New Button
  With buttonRemove
     .FlatStyle = FlatStyle.Flat
     .Name = Me.BUTTON_NAME_REMOVE
     .Text = Me.BUTTON_TEXT_REMOVE
  End With
  PanelControlTop.Controls.Add(buttonRemove)
  Me.MakeToolTip(buttonRemove, Me.TOOPTIP_TEXT_BTNREMOVE)

  Dim buttonClear As New Button
  With buttonClear
     .FlatStyle = FlatStyle.Flat
     .Name = Me.BUTTON_NAME_CLEAR
     .Text = Me.BUTTON_TEXT_CLEAR
  End With
  PanelControlTop.Controls.Add(buttonClear)
  Me.MakeToolTip(buttonClear, Me.TOOLTIP_TEXT_BTNCLEAR)

  Dim buttonApply As New Button
  With buttonApply
     .FlatStyle = FlatStyle.Flat
     .Name = Me.BUTTON_NAME_APPLY
     .Text = Me.BUTTON_TEXT_APPLY
  End With
  PanelControlTop.Controls.Add(buttonApply)
  Me.MakeToolTip(buttonApply, Me.TOOLTIP_TEXT_BTNAPPLY)

End Sub

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Posted

Why are you doing this anyway? Where are the BUTTON_NAME_ADD, BUTTON_NAME_REMOVE being kept? A resource file?

 

You should set the location and visibility of each button, otherwise they may not show up and if they do they will all be in location 0, 0.

 

Since you are creating each button seperately, you can handle each event in it's own function

 

eg.

 

Dim buttonAdd As New Button
  With buttonAdd
     .FlatStyle = FlatStyle.Flat
     .Name = Me.BUTTON_NAME_ADD
     .Text = Me.BUTTON_TEXT_ADD
     .Visible = True
     .Location = new Point(10, 10)
    End With
  AddHandler buttonAdd.Click, AddressOf Crap

  PanelControlTop.Controls.Add(buttonAdd)
  Me.MakeToolTip(buttonAdd, Me.TOOLTIP_TEXT_BTNADD)

Public Sub Crap(sender as object, e as EventArgs)
  'handle click event
End Sub

 Dim buttonRemove As New Button
  With buttonRemove
     .FlatStyle = FlatStyle.Flat
     .Name = Me.BUTTON_NAME_REMOVE
     .Text = Me.BUTTON_TEXT_REMOVE
     .Visible = True
     .Location = new Point(80, 10)
  End With

  AddHandler buttonRemove.Click, AddressOf Crap2
  PanelControlTop.Controls.Add(buttonRemove)
  Me.MakeToolTip(buttonRemove, Me.TOOPTIP_TEXT_BTNREMOVE)

Public Sub Crap2(sender as object, e as EventArgs)
'handle remove button click
End Sub

Posted
Why are you doing this anyway? Where are the BUTTON_NAME_ADD, BUTTON_NAME_REMOVE being kept? A resource file?

 

You should set the location and visibility of each button, otherwise they may not show up and if they do they will all be in location 0, 0.

 

Since you are creating each button seperately, you can handle each event in it's own function

 

The BUTTON_NAME_ADD and BUTTON_NAME_REMOVE are constants, currently declared in the same class (custom control). The location of each is set in another method based the locations of other controls (on this custom control) and the size of this custom control's Parent, etc.

 

I don't think the Crap event handler would work because buttonAdd no longer exists outside of the method that created it. When I use the AddHandler/AddressOf line as you have suggested, my Crap event handler produces an error message:

 

FilterBuilder.vb(659): 'buttonAdd' is not an event of FilterBuilder'.

 

I think these buttons are improperly scoped. I will probably need class-level availability rather than simply method level availability for these buttons specifically.

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Posted
Nevermind. I am slowly kicking my own rear into gear on this subject. I was improperly declaring the Event Handler. I'll follow-up with my code when I have it all working.
"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte

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...