Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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,
Ankit
Posted
Comments
Anurag Sinha V 8-Mar-13 5:56am    
Hi,

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

-Anurag
ANKIT_JHA 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 link...it 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:
http://stackoverflow.com/questions/12659844/how-do-you-figure-out-if-any-of-the-buttons-was-clicked-in-c

-anurag

Use a list of buttons (see List(T) at MSDN[^]).
At every button click event, store the reference to the button inside the list.
 
Share this answer
 
Comments
Maciej Los 8-Mar-13 6:46am    
Excelent hint, +5!
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)
VB
Public Class MyButton
    Inherits Button

    Dim iCounter As Integer = 0
    Dim co As Integer = 0

    Property ClickOrder() As Integer
        Get
            Return co
        End Get
        Set(ByVal value As Integer)
            co = value
        End Set
    End Property

    ReadOnly Property ClickedCount() As Integer
        Get
            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
Form1.vb
VB
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.
        InitializeComponent()

        ' 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
            'clickedBtns.Add(btn)
        Next

    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
 
Share this answer
 
v2

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