473,398 Members | 2,335 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,398 software developers and data experts.

Button Array controls

Hi Guys. This is my first question regarding programming in a long time.

See below a program that creates a form with a button array. The question
is simple. After the AddHandler MessageBox.Show is executed, how do I
refer to the button that was pressed, in order to change its color?

I've spent a lot of time looking for this. Just got back into programming after many years!

Thanks to all.

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. End Class
OS: Windows XP Home
Language : Visual Basic 2005
Oct 28 '07 #1
1 1534
cugone
20
You must first cast the sender (an Object type) to a Button type (it is in object form because all classes derive from Object)

Expand|Select|Wrap|Line Numbers
  1. dim testButton as Button = TryCast(sender, Button)
  2. MessageBox.Show("You clicked button: " & testButton.name)
  3. testButton.BackColor = Color.ColorValue
  4.  
Oct 29 '07 #2

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

Similar topics

5
by: Keith Smith | last post by:
What am I missing? I am trying to dynamically create a button... Button myButton = new Button(); myButton.Name="ButtonX";
3
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way...
1
by: Firewalker | last post by:
I am attempting to change the backColor property on the previously instantiated buttons FROM a listbox_doubleClick event. I thought it would be something like this: If...
0
by: maitrepoy | last post by:
Hello I have to create a small addin which works on Powerpoint, Word, Outlook, and Excel on Office 2000, XP, and 2003. This addin consists in adding 2 new Buttons in the "File" Menu of office....
0
by: maitrepoy | last post by:
hello I have to create a small addin which works on Powerpoint, Word, Outlook, and Excel on Office 2000, XP, and 2003. This addin consists in adding 2 new Buttons in the "File" Menu of office....
3
by: Starbuck | last post by:
Hi I have created an array of buttons thus (see below). The buttons appear on the form fine. The only thing I cannot figure is how to capture the click and do a action depending on which...
11
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
3
by: Kannan | last post by:
Hi, I am trying to created Outloook Add-in Com in outlook using C#. I have seen this URL for developing this sample http://support.microsoft.com/?kbid=302901 When I executed this program it...
5
by: Peted | last post by:
I know you can iterate through a collection of radio buttons in a panel, using a "for each in control" type iteration that c# supports, but is it possible to iterate through the radio buttons...
1
by: ganesh22 | last post by:
Hi, the below code is for to create dynamically buttons in asp.net. my problem is at the first time i visited the page its working but if i visited the second time that buttons is not clearing....
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.