473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to use Onclick event with an Array of Buttons?

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
4 6533
iburyak
1,017 Expert 512MB
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
isti88
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
1,017 Expert 512MB
This picture is so cute... :)
How did you do it?
Apr 11 '07 #4
Killer42
8,435 Expert 8TB
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

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

Similar topics

2
by: mike piano | last post by:
With html buttons, I can say onclick="MyFunction();" and it calls a JavaScript function called "MyFunction", but with the Web Controls buttons, OnClick="MyFunction();" produces an error: ...
8
by: Shock | last post by:
Hello everyone, I am having a problem with the program below. I have isolated the problem to the onclick event that is located throughout arrQuestions. The onclick event refers to a function...
2
by: abs | last post by:
Hi all. Is that possible to call the onclick, onchange or other event from JS ? I mean how to make the browser think that user clicked the element (onclick event) ? Eg. I have <element...
1
by: Dark Magician | last post by:
Comrades: Am trying to build a UI widget. I'm sure part of the problem is proper variable scope or object reference, and part of the problem may be the way I'm calling the function, but, here...
10
by: drawde83 | last post by:
Hi, I'm mocking up an interface in javascript for an HCI assignment. I want to be able to make the default onclick event for the buttons on my page to display an alert since the buttons won't work...
1
by: Hrvoje Vrbanc | last post by:
Hello all! My question is the following: I add buttons (server controls) programatically into a table cell. Example: Dim btObnovi As Button = New Button() celija25.Controls.Add(btObnovi) ...
1
by: tshad | last post by:
Is there a way to change the onClick event of a LinkButton? At the moment, I am using 2 buttons that I toggle the visibility on and off. I would like to use just one button, if possible and just...
3
by: wardy1975 | last post by:
Hi all....looking for a bit of help here....I'm using an asp.net server side button, and trying to add an onclick event on the button to call a javascript function. However, I am trying to enter...
7
by: rsaffy | last post by:
I am having trouble with my dynamically created button's event handling. I read that the buttons need to be recreated on every trip to the server, but how exactly do you do that when the datagrid...
1
by: sourcie | last post by:
I am changing an existing quiz found on "JavaScriptKit.com Multiple Choice Quiz" I have an image. Instead of using the radio buttons with the normal true/false question, I want to place two...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.