VS 2010 Selecting Random Button-VBForums
Results 1 to 13 of 13

Thread: Selecting Random Button

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Nottingham, UK
    Posts
    41

    Question 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

  2. #2
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    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

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,592

    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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,804

    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

  5. #5
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Selecting Random Button

    Quote Originally Posted by kaliman79912 View Post
    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

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,804

    Re: Selecting Random Button

    Why are you doing the CType in that? The array is already declared as Button.
    My usual boring signature: Nothing

  7. #7
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,592

    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

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: Selecting Random Button

    vb Code:
    1. Dim button = Me.Controls.OfType(Of Button)().OrderBy(Function(n) rng.NextDouble()).First

  9. #9
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Selecting Random Button

    Quote Originally Posted by Shaggy Hiker View Post
    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

  10. #10
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Selecting Random Button

    Quote Originally Posted by ident View Post
    vb Code:
    1. 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

  11. #11
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Selecting Random Button

    Quote Originally Posted by kaliman79912 View Post
    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

  12. #12
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: Selecting Random Button

    Quote Originally Posted by thetimmer View Post
    good point
    You also would not seed the random in the method and convert the object each time.

  13. #13
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: Selecting Random Button

    Quote Originally Posted by thetimmer View Post
    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.