-
September 3rd, 2003, 11:20 PM
#1
Creating 100 buttons on a form
Hello all,
I need to create one hundred buttons on a form, which all behave with simaliar attributes. When each button is pressed it will have to remember that it was pressed and change color (eventually changing back to the original color when pressed again).
At any rate, rather than create one hundred buttons manually, can I do this using a class? Any code examples would be very helpful!
Thanks in advance,
Bob Delamater
-
September 4th, 2003, 08:23 AM
#2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim x As Integer
For i = 0 To 99
Dim btn As New Button()
btn.Text = "Button " & i.ToString
btn.Top = x
AddHandler btn.Click, New EventHandler(AddressOf Btn_click)
x = btn.Top + btn.Height
Me.Controls.Add(btn)
Next
End Sub
Private Sub Btn_click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button
btn = DirectCast(sender, Button)
If btn.BackColor.Equals(Color.Red) Then
btn.BackColor = Color.Gray
Else
btn.BackColor = Color.Red
End If
End Sub
-
September 5th, 2003, 11:37 PM
#3
DSJ, thanks for getting me started! That was a big help for sure. Maybe I can run this one by you.
I'd like to add the controls in the following format:
1 2 3 4 5 6 7 8 9 10
11121314151617181920
21222324252627282930
31323334353637383940
etc...
I have this code so far:
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer
For i = 0 To 99
Dim btn As New Button()
btn.Text = (i + 1).ToString
btn.Left = x
Select Case i
Case 10
btn.Top = btn.Top + 25
btn.Left = 0
Case 20
btn.Top = btn.Top + 50
btn.Left = 0
Case 30
btn.Top = btn.Top + 75
btn.Left = 0
Case 40
btn.Top = btn.Top + 100
btn.Left = 0
Case 50
btn.Top = btn.Top + 125
btn.Left = 0
Case 60
btn.Top = btn.Top + 150
btn.Left = 0
Case 70
btn.Top = btn.Top + 175
btn.Left = 0
Case 80
btn.Top = btn.Top + 200
btn.Left = 0
Case 90
btn.Top = btn.Top + 225
btn.Left = 0
End Select
btn.Height = 25
btn.Width = 25
x = btn.Left + btn.Width
AddHandler btn.Click, New EventHandler(AddressOf Btn_click)
'x = btn.Left + btn.Height
btn.BackColor = Color.CornflowerBlue
Me.Controls.Add(btn)
Next
End Sub
but, the code generates the buttons like this:
12345678910
11
21
31
41
51
61
71
81
91
Seems kind of odd... Any suggestions would be helpful.
Thanks again,
Bob
Last edited by Delamater; September 6th, 2003 at 01:15 AM.
-
September 6th, 2003, 01:53 AM
#4
Wait! I got it.... I used a case 10 to 19, 20 to 29 statement and all is good!
Thanks again.
Bob
-
September 8th, 2003, 08:34 PM
#5
Thanks again DSJ for your great post! I took it a bit further and now I am running into another problem. 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 Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.White) Then
Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.CornflowerBlue)
'End If
iSeatNumber would be an integer and me.controls.item would essentially be one of the buttons created.
I also tried this:
btn(iSeatNumber).backcolor.equals(Color.White)
but intellisense says that the button cannot be accessed because it has no default property. Could it be that I need to pass the buttons object to the doubleClick event?
Any ideas on this would help a lot!
Bob
-
September 10th, 2003, 09:32 AM
#6
In one example you have btn.Top and the other btn(index).SomeProperty... are you saving them in an array when you create them? If not, btn(index) isn't valid... you should Dim btn(99) as Button at the class level and save them there as you create them so you can reference them later in your change event, otherwise you'll need to loop thru me.controls to find the button you need to change.
-
September 11th, 2003, 12:28 PM
#7
Thanks for the reply DSJ. I am not saving them in an array because quite simply, I haven't figured that out yet .
So, I thought originally like you did, I should iterate through the controls to check the backcolor property and switch it from blue to white or vice versa based on the text of the list box...
for example
if listbox.items.item(i) = "Booked" then
If Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.White) Then
Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.CornflowerBlue)
'End If
doesn't seem to work though.
Bob
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|