Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows form
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 8-Mar-13 0:43am
Comments
Anurag Sinha V at 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
ankitjha23 at 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 at 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
Rate this: bad
good
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.
  Permalink  
Comments
Maciej Los at 8-Mar-13 6:46am
   
Excelent hint, +5!
Rate this: bad
good
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
        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
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
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2014
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