473,320 Members | 1,858 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,320 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 6517
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.