|
-
Jan 4th, 2006, 07:02 AM
#1
Thread Starter
Giants World Champs!!!!
[RESOLVED] Button Arrays - How
I know in VB.Net there isn't an Index property on the Button control like it does in VB6. But I am trying to replicate a control array in VB.Net. I have created an array of buttons:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim but(10) As Button
Dim i As Integer
Dim lngTop As Long = 10
For i = 1 To 6
but(i) = New Button()
With but(i)
Me.Controls.Add(but(i))
.BackColor = Color.AliceBlue
.FlatStyle = FlatStyle.Standard
.Text = "But(" & i & ")"
.Name = "Button"
.Left = 10
.Top = lngTop
.Tag = "UP"
lngTop = (lngTop + but(i).Height) - 1.5
.Visible = True
AddHandler but(i).Click, AddressOf ButtonClick
End With
Next i
End Sub
But my question how do I pass the Button Array index to the following Sub:
VB Code:
Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
'Do something with Button
End Sub
Thanks,
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 4th, 2006, 07:26 AM
#2
Re: Button Arrays - How
Since I see you are already using the Tag property for something else, you may want to make your own Button class that inherits from Button, and just add an Index property.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jan 4th, 2006, 07:27 AM
#3
Fanatic Member
Re: Button Arrays - How
VB Code:
Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
'sender is a reference to the control that fires the event, it must be cast however.
Dim b As Button = DirectCast(sender, Button)
'b is the button that was clicked
Debug.WriteLine(b.Text)
End Sub
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jan 4th, 2006, 07:47 AM
#4
Thread Starter
Giants World Champs!!!!
Re: Button Arrays - How
Thank you for the posts but I guess what I meant to say is that how would I access another button's properties other than the Sender Object (ie. User Clicks Button(2) and then I want to move Button(4)'s position)
 Originally Posted by crptcblade
Since I see you are already using the Tag property for something else, you may want to make your own Button class that inherits from Button, and just add an Index property.
Can you point me to any good examples of this?
Thanks
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 4th, 2006, 07:49 AM
#5
Fanatic Member
Re: Button Arrays - How
make a class level var of Dim but(10) As Button so it can be referenced from your eventhandler.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jan 4th, 2006, 07:58 AM
#6
Thread Starter
Giants World Champs!!!!
Re: Button Arrays - How
 Originally Posted by grilkip
make a class level var of Dim but(10) As Button so it can be referenced from your eventhandler.
Thanks, I can't believe I couldn't figure that out myself. Now I am getting a little worried, VB.Net is starting to make sense to me now.
Thanks for the posts!!
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 4th, 2006, 07:58 AM
#7
Re: Button Arrays - How
make a custom class for your buttons
VB Code:
Public Class ButtonI
Inherits Forms.Button
Public Index as Integer
End Class
or, there is actually a buttonarray object you can add to your project, never used it though
EDIT NOTE: Oh? you didnt need the index to do + 1 or whatever
-
Jan 4th, 2006, 07:59 AM
#8
Fanatic Member
Re: Button Arrays - How
As a side note: you should use the keyword Private rather than Dim with Class level vars.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jan 4th, 2006, 08:22 AM
#9
Thread Starter
Giants World Champs!!!!
Re: Button Arrays - How
 Originally Posted by Phill64
make a custom class for your buttons
VB Code:
Public Class ButtonI
Inherits Forms.Button
Public Index as Integer
End Class
. . .
Thanks for you code, I added the class to the Form Class:
VB Code:
Public Class Form1
Public Class ButtonI
Inherits System.Windows.Forms.Button
Public Index As Integer
End Class
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
But I am not able to access the index variable.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 4th, 2006, 08:25 AM
#10
Re: Button Arrays - How
you will have to declare your buttons as "ButtonI" instead of "Button", as thats the type which has the Index value
-
Jan 4th, 2006, 08:50 AM
#11
Thread Starter
Giants World Champs!!!!
Re: Button Arrays - How
 Originally Posted by Phill64
you will have to declare your buttons as "ButtonI" instead of "Button", as thats the type which has the Index value
Thanks
Here is a copy of the Final code if anyone was interested:
VB Code:
Public Class Form1
Public Class ButtonI
Inherits System.Windows.Forms.Button
Public Index As Integer
End Class
Private but(10) As ButtonI
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim lngTop As Long = 10
For i = 1 To 6
but(i) = New ButtonI
With but(i)
Me.Controls.Add(but(i))
.FlatStyle = FlatStyle.Standard
.Index = i
.Text = "Button " & i
.Name = "Button"
.Left = 10
.Top = lngTop
lngTop = (lngTop + but(i).Height) - 1.5
.Visible = True
AddHandler but(i).Click, AddressOf ButtonClick
End With
Next i
End Sub
Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim intIndex As Integer
Debug.Print(CType(sender, ButtonI).Text)
Debug.Print(CType(sender, Button).Name)
Debug.Print(CType(sender, ButtonI).Index)
End Sub
End Class
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
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
|