English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am working on a form, and I am having a little bit of trouble. I need to make a button turn a color when it is clicked, but at the same time, I need to computer to turn another button a different color. I have 16 buttons and the "game" must keep going and going, no end to it. I have created the code to make button1 the color red, and when clicked it does turn red, but I need to create the code to have the computer randomly change another button 1-16 a different color. I can't seem to get the right code in there. Could someone please help me with this code. I would appreciate as much help as possible. As you can see, I am a Dummy when it comes to VB. Thanks for the help.

2007-05-26 07:58:43 · 3 answers · asked by machoman 3 in Computers & Internet Programming & Design

3 answers

The following code uses a modified button click event handler which will fire when any of teh 16 buttons is clicked

I am generating a random number from 1 to 15 this random number doesn't count the 16th button as it is the one you clicked.

A for each loop will scan through the forms controls collection looking at each button in the collection if the button that is extracted from the bollection is not the one you clicked than a counter is incremented.

When the counter equals the random number a random color is assigned to that button.

The random color uses the same getRnd function to get a number from 1 to 15. A select case is used to return a unique color which is assigned to the button

Public Class Form1

Private Sub multiButton_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, Button10.Click, Button11.Click, Button12.Click, _
Button13.Click, Button14.Click, Button15.Click, Button16.Click

Dim btn, myBtn As Button
Dim num, cnt As Integer

num = getRnd()
cnt = 0

myBtn = sender
myBtn.BackColor = Color.Red

For Each btn In Me.Controls
If btn.Name = myBtn.Name Then
'skip counting as this is the button that was clicked

Else
'increment the counter
cnt = cnt + 1
End If

If cnt = num Then
'this btn is the one to randomly change
btn.BackColor = getRndColor()
End If


Next

End Sub
Private Function getRndColor() As Color
Dim idx As Integer

idx = getRnd()
Select Case idx
Case 1
Return Color.Aqua
Case 2
Return Color.BurlyWood
Case 3
Return Color.Chartreuse
Case 4
Return Color.Cyan
Case 5
Return Color.DarkGreen
Case 6
Return Color.LightGreen
Case 7
Return Color.AntiqueWhite
Case 8
Return Color.Indigo
Case 9
Return Color.LightSkyBlue
Case 10
Return Color.DarkOrange
Case 11
Return Color.PaleVioletRed
Case 12
Return Color.Tan
Case 13
Return Color.Violet
Case 14
Return Color.Lime
Case 15
Return Color.Blue
Case Else
Return Color.YellowGreen

End Select

End Function
Private Function getRnd() As Integer
Dim randomvalue, upperbound, lowerbound As Integer
upperbound = 15
lowerbound = 1

randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))

Return randomvalue
End Function

2007-05-26 10:04:25 · answer #1 · answered by MarkG 7 · 0 0

I like Mark G's answer. One thing I might do different is set up a control array for the buttons. Instead of button1, button2, etc, name them Button(1), Button(2), etc.
That way you only have to write the click event for Button(x).click and pass the number into it. (x = randomNumber)

Another way to get a random number is to use the current time and get the seconds from the DatePart function.

Dim randomNumber as integer
randomNumber = DatePart("s", Now)
Do until < 16
randomNumber = randomNumber - 16
Loop
This takes the current time and grabs the seconds. Suppose the seconds are currently 43. That is too big a number for your array so you loop through subtracting 16.
43 - 16 = 27
27 - 16 = 11

Eleven becomes your random number, and Button(11).backcolor = red

2007-05-26 18:40:33 · answer #2 · answered by rod 6 · 0 0

Use this site it helped me wen i was using VB

http://www.vbtutor.net/

i would help u more but i dont remember VB too well

2007-05-26 15:07:47 · answer #3 · answered by Jake 7 · 0 0

fedest.com, questions and answers