By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

How to use Onclick event with an Array of Buttons?

P: 3
Hi!

I have a 2 dimensiononal Button array, that i created with this code
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Public btn_array(5, 5) As Button
  4.  
  5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  6.  
  7.  
  8.  
  9.         For i As Integer = 0 To 5
  10.             For j As Integer = 0 To 5
  11.                 btn_array(i, j) = New Button
  12.                 Me.Controls.Add(btn_array(i, j))
  13.                 btn_array(i, j).Location = New Point(10 + i * 75, 10 + j * 30)
  14.                 btn_array(i, j).Text = "Button(" & i & "," & j & ")"
  15.             Next
  16.         Next
  17.  
  18.     End Sub
  19.  
  20.  
  21. End Class
  22.  
I would like to run a sub, when the user clicks on one of the buttons. Also i should be able to tell, which button has been clicked.

Lets say i want to run the following sub, that messages me the i and the j indexes of the clicked button.

Expand|Select|Wrap|Line Numbers
  1.     Private Sub One_of_the_buttons_has_been_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
  2.         MsgBox(" You have clicked Button " & i & j )
  3.     End Sub
  4.  
I have been searching this forum and the msdn for the solution to my problem for ages but i still have not found the answer.

I would be grateful if anyone could show me a way to archive my goal.


And some information about my developer enviroment

Microsoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

Installed Edition: Professional

Microsoft Visual Basic 2005 77626-009-0000007-41551
Microsoft Visual Basic 2005

Thanks for reading 'till this :)
Apr 10 '07 #1
Share this Question
Share on Google+
4 Replies


iburyak
Expert 100+
P: 1,017
You owe me something.... :)
I am not even .NET programmer .... :)

This code does work I tested:

Expand|Select|Wrap|Line Numbers
  1. Public btn_array(5, 5) As Button
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.  
  5.         For i As Integer = 0 To 5
  6.             For j As Integer = 0 To 5
  7.                 btn_array(i, j) = New Button
  8.                 Me.Controls.Add(btn_array(i, j))
  9.                 btn_array(i, j).Location = New Point(10 + i * 75, 10 + j * 30)
  10.                 btn_array(i, j).Text = "Button(" & i & "," & j & ")"
  11.                 btn_array(i, j).Name = "Button(" & i & "," & j & ")"
  12.                 AddHandler btn_array(i, j).Click, AddressOf MyControl_Click
  13.             Next
  14.         Next
  15.  
  16.     End Sub
  17.  
  18.     Private Sub MyControl_Click(ByVal sender As Object, ByVal e As EventArgs)
  19.         MessageBox.Show("You have clicked control name " + sender.name)
  20.     End Sub
  21.  
  22.  
  23. End Class
Good Luck.
Apr 10 '07 #2

P: 3
You Deserve A Million Thank Yous!
Amazing solution, exact what i needed!!
I really owe you iburyak!



You owe me something.... :)
I am not even .NET programmer .... :)

This code does work I tested:

Expand|Select|Wrap|Line Numbers
  1. Public btn_array(5, 5) As Button
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.  
  5.         For i As Integer = 0 To 5
  6.             For j As Integer = 0 To 5
  7.                 btn_array(i, j) = New Button
  8.                 Me.Controls.Add(btn_array(i, j))
  9.                 btn_array(i, j).Location = New Point(10 + i * 75, 10 + j * 30)
  10.                 btn_array(i, j).Text = "Button(" & i & "," & j & ")"
  11.                 btn_array(i, j).Name = "Button(" & i & "," & j & ")"
  12.                 AddHandler btn_array(i, j).Click, AddressOf MyControl_Click
  13.             Next
  14.         Next
  15.  
  16.     End Sub
  17.  
  18.     Private Sub MyControl_Click(ByVal sender As Object, ByVal e As EventArgs)
  19.         MessageBox.Show("You have clicked control name " + sender.name)
  20.     End Sub
  21.  
  22.  
  23. End Class
Good Luck.
Apr 10 '07 #3

iburyak
Expert 100+
P: 1,017
This picture is so cute... :)
How did you do it?
Apr 11 '07 #4

Expert 5K+
P: 8,434
This picture is so cute... :)
How did you do it?
If you mean how it was posted, it’s just an IMG link - see the FAQ. If you mean how it was created… good question.
Apr 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.