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.
Re: Selecting Random Button
If you put them in an array it becomes a thing of just a couple of lines
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.
Re: Selecting Random Button
Quote:
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
Re: Selecting Random Button
Why are you doing the CType in that? The array is already declared as Button.
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
Re: Selecting Random Button
vb Code:
Dim button = Me.Controls.OfType(Of Button)().OrderBy(Function(n) rng.NextDouble()).First
Re: Selecting Random Button
Quote:
Originally Posted by
Shaggy Hiker
Why are you doing the CType in that? The array is already declared as Button.
good point
Re: Selecting Random Button
Quote:
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
Re: Selecting Random Button
Quote:
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.
Re: Selecting Random Button
Quote:
Originally Posted by
thetimmer
good point
You also would not seed the random in the method and convert the object each time.
Re: Selecting Random Button
Quote:
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