|
-
Jul 22nd, 2003, 09:34 PM
#1
Thread Starter
Addicted Member
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
-
Jul 22nd, 2003, 11:51 PM
#2
Hyperactive Member
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
-
Jul 23rd, 2003, 03:49 AM
#3
Fanatic Member
VB Code:
Dim array(8) As Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
array(0) = Button1
array(1) = Button2
array(2) = Button3
array(3) = Button4
array(4) = Button5
array(5) = Button6
array(6) = Button7
array(7) = Button8
array(8) = Button9
End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click, _
Button7.Click, Button8.Click, Button9.Click
Dim choose As Integer = Int(Rnd() * 9 + 1)
array(choose).Text = "changed text"
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.
-
Jul 23rd, 2003, 11:14 AM
#4
Thread Starter
Addicted Member
Thanks robbedaya, it works
Gary W Web Developer & Designer
Visual Basic 6
-
Jul 23rd, 2003, 11:18 AM
#5
As a side note you could also build the array like this:
VB Code:
Dim btnArray() As Button={Button1,Button2,Button3,Button4,
Button5,Button6,Button7,Button8,
Button9}
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click, _
Button7.Click, Button8.Click, Button9.Click
Dim choose As Integer = Int(Rnd() * 9 + 1)
btnArray(choose).Text = "changed text"
End Sub
-
Jul 23rd, 2003, 02:48 PM
#6
Thread Starter
Addicted Member
I've got
VB Code:
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click, _
Button7.Click, Button8.Click, Button9.Click
Dim choose As Integer = Int(Rnd() * 9 + 1)
'If button(choose) Is "" Then
button(choose).Text = "X"
'End If
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
-
Jul 23rd, 2003, 02:51 PM
#7
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"
-
Jul 23rd, 2003, 03:00 PM
#8
Thread Starter
Addicted Member
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
-
Jul 24th, 2003, 04:16 AM
#9
Fanatic Member
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.
-
Jul 24th, 2003, 05:03 AM
#10
Addicted Member
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!
-
Jul 24th, 2003, 05:17 AM
#11
Fanatic Member
if you want to make sender to be a button again, you should use this: (this is the .NET-way)
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As Button = sender
btn.Text = "test"
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.
-
Jul 24th, 2003, 05:43 AM
#12
Addicted Member
That will cause a compile error if Option Strict is on!
-
Jul 24th, 2003, 09:39 AM
#13
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|