Click here to Skip to main content
15,881,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to find the order that the buttons are clicked and add them in an Array where they are sorted by their click order. Then i want to know the name of the last button clicked (4 buttons).

What I have tried:

I have tried to check when a button is clicked so i can add it to an array but i cant do it
VB.NET
Public Class Form1
    For Each c As Control In Panel1.Controls
        If c.GetType() = GetType(Button) Then
            AddHandler c.Click, AddressOf btn_Click
        End If
    Next
End Sub

Dim Xor0 As Integer = 0
  
Public Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As Button = sender
    If btn.Text.Equals("") Then
        If Xor0 Mod 2 = 0 Then
            btn.BackColor = Color.Blue
            Label1.Text = ply2 + "'s Turn"
        Else
            btn.BackColor = Color.Red
            Label1.Text = ply1 + "'s Turn"
        End If
        Xor0 = Xor0 + 1
    End If
End Sub
End Class
Posted
Updated 4-Apr-21 6:16am
v3
Comments
Richard MacCutchan 4-Apr-21 9:15am    
You have tagged this question VisualC++ and Basic, so the answer will be different for each language. Please use the Improve question link above and add proper details of the problem, and show the code you already have.
Daniel Peqini 4-Apr-21 9:19am    
the program i use is visual basic 2010 express
Patrice T 4-Apr-21 11:46am    
Try to show your code.
Daniel Peqini 4-Apr-21 12:11pm    
Code moved to the "What I have done" section of the question
Daniel Peqini 4-Apr-21 12:16pm    
now what i need to do is to make a function that finds which of the buttons is clicked the last one out of 4 of them and then finds out which player clicked that button by checking its color. The player that clicked the last button takes 1 point

1 solution

So create a class level List of Buttons, and handle their Click events.
In the handler, check if the button has been added already, and if so, remove it.
Then add the button.

Since a List is ordered by "last entry at the end" you just need to fetch the item with the highest index, and that is the last pressed.

Hint: you can use a single event handler for any number of buttons - the Button that was clicked is passed to it as the sender parameter.
 
Share this answer
 
Comments
Daniel Peqini 4-Apr-21 10:06am    
Can you give an example please ?!
OriginalGriff 4-Apr-21 10:42am    
Which bit? You should know how to do all of those things ...

You know how to create a List, I assume?
Daniel Peqini 4-Apr-21 11:27am    
i"m a little bit new here, its a university project and i'm not very good at it, plus im running out of time.
Would u help me !
OriginalGriff 4-Apr-21 11:54am    
Does that mean you do or you don't know?

I only get exactly what you type to work with, I can't see your screen, access your HDD, or read your mind...
Daniel Peqini 4-Apr-21 12:08pm    
I do not

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