Click here to Skip to main content
13,147,037 members (61,549 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello experts,

I am doing a simple project on windows form where I need to check the order in which the buttons got selected from a number of buttons. Please provide some ideas.

Thanks in advance,
Posted 7-Mar-13 23:43pm
Anurag Sinha V 8-Mar-13 5:56am

Aren't you selecting the buttons from the list of buttons?
If you are thn i guess you would know which button has been selected initially and else.
Put breakpoints on every single button's click event handlers, then when as you click the buttons, the corresponding event handlers will be invoked and you might get a clue as to which button is getting clicked first. Try it...

ankitjha23 8-Mar-13 6:01am
I have a form on which i have 15 buttons. Now here i want to check the order of their invocation. My real scenario is: When i click on a button X it should check if another button Y has been clicked or not. How to check this?
Anurag Sinha V 8-Mar-13 6:20am
hey, u might want to check this has something which might interest you. Basically you will have to create event handlers for each button and on the main button click you will have to have a nested loop to see which button has been clicked...Check out this link:

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use a list of buttons (see List(T) at MSDN[^]).
At every button click event, store the reference to the button inside the list.
Maciej Los 8-Mar-13 6:46am
Excelent hint, +5!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Another solution to achieve described functionality (without List(of T)) is to create custom class MyButton with ClickOrder and ClickedCount properties.

Hot to do it in VB.NET?
1) Add new class (set the name of file to MyButton.vb)
Public Class MyButton
    Inherits Button
    Dim iCounter As Integer = 0
    Dim co As Integer = 0
    Property ClickOrder() As Integer
            Return co
        End Get
        Set(ByVal value As Integer)
            co = value
        End Set
    End Property
    ReadOnly Property ClickedCount() As Integer
            Return iCounter
        End Get
    End Property
    Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        iCounter += 1
    End Sub
End Class

2) catch click event and view some information
Public Class Form1
    Private WithEvents btn As MyButton
    Dim i As Integer = 0
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        ' Add any initialization after the InitializeComponent() call.
        Dim i As Integer = 0
        'add 10 MyButtons 
        For i = 0 To 9
            btn = New MyButton
            With btn
                .Name = "button" & Int(i + 1).ToString
                .Text = .Name
                .Parent = Me
                .Left = 8
                .Top = 24 * i + 4
                .Width = 75
                .Height = 24
                'add event handler
                AddHandler btn.Click, AddressOf btn_Click
            End With
    End Sub
    'event handler for MyButton
    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        Dim b As MyButton = sender
        i += 1
        b.ClickOrder = i
        MsgBox(b.Name & " has been clicked: " & b.ClickedCount.ToString & " time(s)." & vbCr & _
                "Click order: " & b.ClickOrder.ToString, MsgBoxStyle.Information, "Message...")
    End Sub
End Class

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100