Results 1 to 13 of 13

Thread: Append a variable to a button name

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    175

    Append a variable to a button name

    I have generated a random number between 1 to 9, each number corresponding to each of 9 buttons.

    How can I append the random number to a button, like so:

    visual basic code: Function generate() As Integer
    Dim choose As Integer = Int(Rnd() * 9 + 1)
    Button & "choose" & .Text = "text here"
    End Function



    The Button & "choose" part is incorrect, I know, but it's just to give you a general idea of what I would like to achieve.

    Each of my 9 buttons are named from Button1 to Button9.

    Any suggestions on how I could do this?
    Gary W Web Developer & Designer
    Visual Basic 6

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    If its only between 0 and 9, use the select case construct

    eg
    Select RandomNumber

    case 0

    Button1.text = whatever

    case 1
    Button2. text = some other text
    .....
    end case

  3. #3
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    VB Code:
    1. Dim array(8) As Button
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         array(0) = Button1
    4.         array(1) = Button2
    5.         array(2) = Button3
    6.         array(3) = Button4
    7.         array(4) = Button5
    8.         array(5) = Button6
    9.         array(6) = Button7
    10.         array(7) = Button8
    11.         array(8) = Button9
    12.     End Sub
    13.  
    14.     Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, _
    15.                                                                                                     Button4.Click, Button5.Click, Button6.Click, _
    16.                                                                                                     Button7.Click, Button8.Click, Button9.Click
    17.         Dim choose As Integer = Int(Rnd() * 9 + 1)
    18.         array(choose).Text = "changed text"
    19.     End Sub
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    175
    Thanks robbedaya, it works
    Gary W Web Developer & Designer
    Visual Basic 6

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    As a side note you could also build the array like this:
    VB Code:
    1. Dim btnArray() As Button={Button1,Button2,Button3,Button4,
    2.                                            Button5,Button6,Button7,Button8,
    3.                                            Button9}
    4.  
    5.     Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
    6.  Button1.Click, Button2.Click, Button3.Click, _
    7.  Button4.Click, Button5.Click, Button6.Click, _
    8.  Button7.Click, Button8.Click, Button9.Click
    9.         Dim choose As Integer = Int(Rnd() * 9 + 1)
    10.         btnArray(choose).Text = "changed text"
    11.     End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    175
    I've got
    VB Code:
    1. Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    2.                             Handles Button1.Click, Button2.Click, Button3.Click, _
    3.                                                                                                   Button4.Click, Button5.Click, Button6.Click, _
    4.                                                                                                   Button7.Click, Button8.Click, Button9.Click
    5.  
    6.     Dim choose As Integer = Int(Rnd() * 9 + 1)
    7.     'If button(choose) Is "" Then
    8.     button(choose).Text = "X"
    9.     'End If
    10.   End Sub

    How can I make it so that when one of the buttons is clicked, it will change its text? I'm not sure how to do this, because this sub handles all the buttons.
    Gary W Web Developer & Designer
    Visual Basic 6

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    sender is a reference to the button that was clicked

    sender.Text = "beauh"

    better yet, cast it to a button, for the ske of neatness

    Ctype(sender, Button).Text = "beauh"
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    175
    Originally posted by Cander
    sender is a reference to the button that was clicked

    sender.Text = "beauh"

    better yet, cast it to a button, for the ske of neatness

    Ctype(sender, Button).Text = "beauh"
    Thanks for the help!
    Last edited by Gary W; Jul 23rd, 2003 at 03:03 PM.
    Gary W Web Developer & Designer
    Visual Basic 6

  9. #9
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    Originally posted by Cander
    Ctype(sender, Button).Text = "beauh"
    the Ctype function is a VB6 function that is stil supported in .NET. You shouldn't use the VB6 functions in .NET

    And your sugestion doest do what he Gary wants. He doesn't want to change the text of the button he clicked but, one random chosen other button.
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  10. #10
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    Originally posted by robbedaya
    the Ctype function is a VB6 function that is stil supported in .NET. You shouldn't use the VB6 functions in .NET
    Oh no its not!

  11. #11
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    if you want to make sender to be a button again, you should use this: (this is the .NET-way)

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim btn As Button = sender
    3.         btn.Text = "test"
    4.     End Sub
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  12. #12
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    That will cause a compile error if Option Strict is on!

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    175
    Thanks for the help everyone, I got the program to work exactly how I want to.

    Thanks again!
    Gary W Web Developer & Designer
    Visual Basic 6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width