dyn buttons

  • Thread starter Thread starter S c o t t K r a m e r
  • Start date Start date
S

S c o t t K r a m e r

I'd like to add buttons dyn to a form, i can create new buttons, new .name &
..text properties, but i can't figure out the eventhandler

example I want to create a grid of buttons based on the itemno from a
database, then clik on the button & have it add that itemno to a list box,
or database...


Thanks!!
 
Scott, I gather you are using 'WithEvents' when you are defining the
buttons?
Show the code you are using.
 
S c o t t K r a m e r said:
I'd like to add buttons dyn to a form, i can create new buttons, new .name
&
.text properties, but i can't figure out the eventhandler

Here is a simple example on how to do that with a button control:

\\\
Dim btn As New Button()
AddHandler btn.Click, AddressOf Me.btn_Click
Me.Controls.Add(btn)
///

Add the handler code:

\\\
Private Sub btn_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
)
Dim SourceControl As Button = DirectCast(sender, Button)
MsgBox(SourceControl.Text)
End Sub
///
 
perfect thanks!!

Herfried K. Wagner said:
Here is a simple example on how to do that with a button control:

\\\
Dim btn As New Button()
AddHandler btn.Click, AddressOf Me.btn_Click
Me.Controls.Add(btn)
///

Add the handler code:

\\\
Private Sub btn_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
)
Dim SourceControl As Button = DirectCast(sender, Button)
MsgBox(SourceControl.Text)
End Sub
///
 
If I use this to build 20 buttons on the screen, how do I also remove these
controls?
 
S c o t t K r a m e r said:
If I use this to build 20 buttons on the screen, how do I also remove
these controls?

You can add the controls to an arraylist and later loop through the list and
use 'Me.Controls.Remove(...)'. to remove the control from the form.
 
ok, i did that & it works, is there a way to group these instead of removing
them by the indexes? so that i can have multiple sets of dynamic buttons?

'cause as you remove buttons the indexes actually shift on the still active
buttons...
 
Back
Top