button array question

S

Starbuck

Hi

I have created an array of buttons thus (see below). The buttons appear on
the form fine.
The only thing I cannot figure is how to capture the click and do a action
depending on which
button in thr array was clicked.
Thanks for any advise

Regards


For cnt = 0 To 7
backButtons.AddNewButton()
backButtons(cnt).Parent = ctab1
backButtons(cnt).Left = 250
backButtons(cnt).Top = 24 * (cnt + 1)
backButtons(cnt).Height = 22
backButtons(cnt).Width = 80
backButtons(cnt).Text = "BackGround"
Next cnt

backButtons = New ButtonArray(Me)
Dim backButtons As ButtonArray

From this class

Public Class ButtonArray
Inherits System.Collections.CollectionBase
Private ReadOnly HostForm As System.Windows.Forms.Form

Public Function AddNewButton() As System.Windows.Forms.Button
Dim aButton As New System.Windows.Forms.Button
Me.List.Add(aButton)
HostForm.Controls.Add(aButton)
aButton.Top = Count * 25
aButton.Width = 50
aButton.Left = 140
aButton.Tag = Me.Count
aButton.Text = "Button " & Me.Count.ToString
Return aButton
End Function

Public Sub New(ByVal host As System.Windows.Forms.Form)
HostForm = host
Me.AddNewButton()
End Sub

Default Public ReadOnly Property Item(ByVal Index As Integer) As
System.Windows.Forms.Button
Get
Return CType(Me.List.Item(Index), System.Windows.Forms.Button)
End Get
End Property

Public Sub Remove()
If Me.Count > 0 Then
HostForm.Controls.Remove(Me(Me.Count - 1))
Me.List.RemoveAt(Me.Count - 1)
End If
End Sub

End Class
 
H

Herfried K. Wagner [MVP]

Starbuck said:
I have created an array of buttons thus (see below). The buttons appear on
the form fine.
The only thing I cannot figure is how to capture the click and do a action
depending on which
button in thr array was clicked.

Add a handler to the button's 'Click' event using 'AddHandler' and check the
'sender' parameter:

\\\
AddHandler TheButton.Click, AddressOf Me.Button_Click
..
..
..
Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim SourceControl As Button = DirectCast(sender, Button)
Select Case SourceControl.Name
Case "Bla"
...
Case "Foo"
...
Case...
...
...
End Select
End Sub
///
 
S

Starbuck

Thanks Guys



Cor Ligthert said:
Starbuck

You can place after this sentence

AddHandler aButton.Click, Address Of myButton_Click

Where you remove the button you can do almost exactly the same however
than
RemoveHandler AreferenceToThatButton.Click , Address OfmyButton_Click

And than create that click event, the parameters you need can you see by
looking at intelisence when you have your cursor in that Addhandler on
aButton.Click.

In that is the button
Directcast(sender, button).blabla

I hope this helps?

Cor
 
C

Cor Ligthert

Starbuck

You can place after this sentence
HostForm.Controls.Add(aButton)

AddHandler aButton.Click, Address Of myButton_Click

Where you remove the button you can do almost exactly the same however than
RemoveHandler AreferenceToThatButton.Click , Address OfmyButton_Click

And than create that click event, the parameters you need can you see by
looking at intelisence when you have your cursor in that Addhandler on
aButton.Click.

In that is the button
Directcast(sender, button).blabla

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top