-
Apr 14th, 2014, 07:27 AM
#1
Thread Starter
Member
Selecting Random Button
Hello, I have a control with 16 buttons on it, each called Button1 through Button16. I have a random number that selects one of those buttons, how do I make it so I can change the properties of that selected button?
I'm looking for something like
Code:
Dim rand As New Random
Dim start_1 = rand.Next(1,16)
Button+start_1.Text = "This"
Any help appreciated. Thanks
~ TheMeq ~
If I helped in anyway, please hit the star and send me some rep! Thanks
-
Apr 14th, 2014, 08:29 AM
#2
Addicted Member
Re: Selecting Random Button
Here's one way to do it...
Code:
Private Sub RandomButton()
Dim rand As New Random
Dim start_1 = rand.Next(1, 16)
'iterate through the forms controls
For Each ctl As Control In Me.Controls
'If the control is a button then do stuff
If ctl.GetType() Is GetType(Button) Then
'Check if it's the button you're looking for
If CType(ctl, Button).Name = "Button" & start_1 Then
'If it is the button then change the text of the button to This
CType(ctl, Button).Text = "This"
Else
'If not the button change text of button to Not This
CType(ctl, Button).Text = "Not This"
End If
End If
Next
End Sub
There may be a more direct path to the button using I think reflection maybe but it's early and I can't think of it yet.
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Apr 14th, 2014, 08:34 AM
#3
Re: Selecting Random Button
If you put them in an array it becomes a thing of just a couple of lines
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 14th, 2014, 11:05 AM
#4
Re: Selecting Random Button
Me.Controls(yourButtonNameHere)
is the shorter way to get to it, but I prefer what Kaliman suggested even more. After all, the Me.Controls route only works if the buttons are directly on the form rather than being on a panel, in a groupbox, on a tab, or in any other container.
My usual boring signature: Nothing
-
Apr 14th, 2014, 11:40 AM
#5
Addicted Member
Re: Selecting Random Button
Originally Posted by kaliman79912
If you put them in an array it becomes a thing of just a couple of lines
so maybe like this? makes sense.
Code:
Private _aButtons(15) As Button
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
_aButtons(0) = Me.Button1
_aButtons(1) = Me.Button2
_aButtons(2) = Me.Button3
_aButtons(3) = Me.Button4
_aButtons(4) = Me.Button5
_aButtons(5) = Me.Button6
_aButtons(6) = Me.Button7
_aButtons(7) = Me.Button8
_aButtons(8) = Me.Button9
_aButtons(9) = Me.Button10
_aButtons(10) = Me.Button11
_aButtons(11) = Me.Button12
_aButtons(12) = Me.Button13
_aButtons(13) = Me.Button14
_aButtons(14) = Me.Button15
_aButtons(15) = Me.Button16
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim rand As New Random
Dim start_1 = rand.Next(1, 16)
CType(_aButtons(start_1 - 1), Button).Text = "this"
End Sub
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Apr 14th, 2014, 12:14 PM
#6
Re: Selecting Random Button
Why are you doing the CType in that? The array is already declared as Button.
My usual boring signature: Nothing
-
Apr 14th, 2014, 12:15 PM
#7
Re: Selecting Random Button
more like:
Code:
Public arrButton() As Button
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
arrButton = {Button1, Button2, Button3}
End Sub
Private Sub ChangeCaption(sender As System.Object, e As System.EventArgs)
Dim intRnd As New Random
Dim start_1 As Integer = intRnd.Next(1, 16)
arrButton(start_1).Text = "this"
End Sub
Last edited by kaliman79912; Apr 14th, 2014 at 12:20 PM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 14th, 2014, 12:30 PM
#8
Re: Selecting Random Button
vb Code:
Dim button = Me.Controls.OfType(Of Button)().OrderBy(Function(n) rng.NextDouble()).First
-
Apr 14th, 2014, 12:35 PM
#9
Addicted Member
Re: Selecting Random Button
Originally Posted by Shaggy Hiker
Why are you doing the CType in that? The array is already declared as Button.
good point
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Apr 14th, 2014, 12:39 PM
#10
Addicted Member
Re: Selecting Random Button
Originally Posted by ident
vb Code:
Dim button = Me.Controls.OfType(Of Button)().OrderBy(Function(n) rng.NextDouble()).First
This is slick but I got a not defined for rng
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Apr 14th, 2014, 12:40 PM
#11
Addicted Member
Re: Selecting Random Button
Originally Posted by kaliman79912
more like:
Code:
Public arrButton() As Button
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
arrButton = {Button1, Button2, Button3}
End Sub
Private Sub ChangeCaption(sender As System.Object, e As System.EventArgs)
Dim intRnd As New Random
Dim start_1 As Integer = intRnd.Next(1, 16)
arrButton(start_1).Text = "this"
End Sub
very clean. much better than mine.
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Apr 14th, 2014, 12:43 PM
#12
Re: Selecting Random Button
Originally Posted by thetimmer
good point
You also would not seed the random in the method and convert the object each time.
-
Apr 14th, 2014, 03:35 PM
#13
Re: Selecting Random Button
Originally Posted by thetimmer
This is slick but I got a not defined for rng
define at class level private readonly rng as new random
Tags for this Thread
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
|