Click here to Skip to main content
16,010,328 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
VB

I made a control filled with a button array. On click the selected button is colored.
I cannot figure out how to know which button was pressed in the calling form.
Hereby the used code.
Public Class ButtonsTemplate
    Private _buttons() As Windows.Forms.Button
    Private _selectedButton As Integer

    Public Sub New()
        InitializeComponent()
    End Sub

    Public ReadOnly Property SelectedButton As Integer
        Get
            Return _selectedButton
        End Get
    End Property

    Public Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = CType(sender, Button)
        Dim name As String = btn.Name
        Dim index As Integer = CInt(name.Substring(6))
        ColorButtons(index)
        _selectedButton = index
    End Sub

    Public Sub CreateNew(ByVal number As Integer)
        ReDim _buttons(number - 1)
        Me.Height = 100
        Me.Width = 100
        For i As Integer = 0 To number - 1
            _buttons(i) = New Windows.Forms.Button
            _buttons(i).Width = Me.Width
            _buttons(i).Height = Me.Height * 1 / number
            _buttons(i).Name = "Button" + CStr(i)
            Me.Controls.Add(_buttons(i))
            AddHandler _buttons(i).Click, AddressOf Buttons_Click
        Next
        For i As Integer = 1 To number - 1
            _buttons(i).Top = _buttons(i - 1).Bottom
        Next
        ColorButtons()
    End Sub

    Private Overloads Sub ColorButtons()
        For i As Integer = 0 To UBound(_buttons)
            _buttons(i).BackColor = Color.White
        Next
    End Sub

    Private Overloads Sub ColorButtons(ByVal index As Integer)
        ColorButtons()
        _buttons(index).BackColor = Color.Red
    End Sub
End Class

Public Class Form1
    Private obj As New ButtonsTemplate.ButtonsTemplate

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Controls.Add(obj)
        obj.CreateNew(4)
        AddHandler obj.Click, AddressOf Object_Click
    End Sub

    Private Sub Object_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'this event is never fired
        'except when I change line "_buttons(i).Height = Me.Height * 1 / number"
        'into "... * 0.5..." and when I click on the pane itself
	'but that is not what I want...
        Dim buttonNumber As Integer
        buttonNumber = obj.SelectedButton
        MessageBox.Show("Selected button = " + CStr(buttonNumber))
    End Sub
End Class
Posted

1 solution

Since you are converting the sender event handler parameter to a button, and since that button will be the button that initiated the event, why don't you just use it directly instead of going round the houses with substring and related rubbish?
VB
Public Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim btn As Button = CType(sender, Button)
    btn.BackColor = Color.Red
End Sub
 
Share this answer
 
Comments
wauters 29-Apr-12 6:39am    
The rubbish code is because after subsequent clicks on the buttons only one should be colored red and all the others should be white.
But that was not the question : in my calling Form1 I want to know which button was pressed.
OriginalGriff 29-Apr-12 6:49am    
No, you don't need the rubbish to do that: just loop through the array of Buttons (or the Controls array looking for Buttons) and set them all to White before you set the clicked one. Or keep a reference to which one you set red last time, and set just that white first.
Whichever way you do it, the sender parameter gives you the button that was clicked.
wauters 29-Apr-12 7:40am    
OK, setting the colors can be done easier.
But let's concentrate on my question.
After adding control ButtonsTemplate to Form1, and when pressing on any of the buttons, I want to be notified about which button was pressed.
I tried this with the event Object_Click. It's here I am stucked
Thanks anyway for the quick reply.
OriginalGriff 29-Apr-12 7:51am    
When you get the click event, the sender parameter contains the button that was clicked - you know that, because you are converting it to a Button in your original code!

Dim btn As Button = CType(sender, Button)

So where is the problem in using this?
Sorry, but I don't understand what your difficulty is!
wauters 29-Apr-12 8:21am    
ButtonsTemplate is a compiled control dll made with enclosed code.
In the calling Form1 I want to know which button was clicked.
I try this by calling event Object_Click. But this one is never triggered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900