ME and a freind of mine have a vB class. We are using the schools outdated microsoft visual basic 2003. We in are spare time are trying to make a "Siezure" program. The point of this program is to simply have the user click a button and the background begin to flash Random colors. We could easily use a timer with a set of colors but we want to use random colors so they are unpredictable. We have tried multiple sets of code but can't seem to get anything to work. Could anyone tell me the code for making the background color of a forum flash with random colors?

Dani AI

Generated

A quick heads-up for VB 2003: you are on VB.NET/WinForms, not classic VB6. Several replies here use VB6-style Rnd/Randomize and assign a numeric Long to BackColor. In VB.NET, BackColor is a Color, so prefer Color.FromArgb and a single System.Random instance. Reusing one Random avoids repeating patterns that happen when you reseed every tick. Also, multiplying channels (like a*b*c) skews heavily toward dark values and can overflow; let the RNG choose each channel independently.

This drops into your form and uses a Timer to flash unpredictable colors. Use Me inside the form class rather than Form1:

' VB.NET 2003 / WinForms
Private ReadOnly rng As New Random()

Private Sub btnStart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnStart.Click
    Timer1.Interval = 200   ' 200 ms; adjust as needed
    Timer1.Start()
End Sub

Private Sub btnStop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnStop.Click
    Timer1.Stop()
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    Me.BackColor = Color.FromArgb(rng.Next(256), rng.Next(256), rng.Next(256))
End Sub

Notes tied to the thread:

  • ’s 0..16777215 idea is fine too, but in VB.NET convert that integer to a Color (e.g., split into R/G/B and call Color.FromArgb) instead of assigning the integer directly.
  • is right about 0..255 per channel; just let the timer drive it instead of looping.
  • Avoid naming your own function Randomize as in /; it shadows VB’s legacy statement and confuses things.
  • /: prefer Color.FromArgb(r,g,b) over RGB(...) integers in VB.NET forms.

Last, please add a Stop button and a warning. Rapid flashing can trigger seizures for some users.

Recommended Answers

All 7 Replies

Generate a random number from 0 thru 16777215 (hex FFFFFF) and load the background color property.

ME and a freind of mine have a vB class. We are using the schools outdated microsoft visual basic 2003. We in are spare time are trying to make a "Siezure" program. The point of this program is to simply have the user click a button and the background begin to flash Random colors. We could easily use a timer with a set of colors but we want to use random colors so they are unpredictable. We have tried multiple sets of code but can't seem to get anything to work. Could anyone tell me the code for making the background color of a forum flash with random colors?

RGB color value is 0 to 255. so use the for loop and generate number from 0 to 255. and then set the value of for loop to RGB value.

otherwise generate random number

Randomize()

int r=rnd()*10

'this is a script i used the other day, it randomizes the backcolor of the form

'code begins

Private Sub Command1_Click()
Text1.Text = Randomize("-1", "256")
Text2.Text = Randomize("-1", "256")
Text3.Text = Randomize("-1", "256")
Text4 = Text1 + Val(Text2) + Val(Text3)
Me.BackColor = Text4.Text
End Sub

Function Randomize(Lowerbound As Long, Upperbound As Long)
Randomize = Int(Rnd * Upperbound) + Lowerbound
End Function

Private Sub Form_Load()
Text1.Text = Randomize("-1", "256")
Text2.Text = Randomize("-1", "256")
Text3.Text = Randomize("-1", "256")
Text4 = Text1 + Val(Text2) + Val(Text3)
Me.BackColor = Text4.Text
End Sub

'code ends
'its a verry small code, so it shouldnt be tht hard
'right now it only cycles through the red colors, if you are able to get it to go any other colors please let me know, thnx
'if your using tht script try to keep the +val(text)'s in there
'im pretty sure it requires tht...
'it will keep comeing up with errors every 4 clicks and end your prog if you take out...

ME and a freind of mine have a vB class. We are using the schools outdated microsoft visual basic 2003. We in are spare time are trying to make a "Siezure" program. The point of this program is to simply have the user click a button and the background begin to flash Random colors. We could easily use a timer with a set of colors but we want to use random colors so they are unpredictable. We have tried multiple sets of code but can't seem to get anything to work. Could anyone tell me the code for making the background color of a forum flash with random colors?

Private Sub Form_Click()
Dim a As Long
Dim b As Long
Dim c As Long
Dim d As Long
a = randomise(0, 255)
b = randomise(0, 255)
c = randomise(0, 255)

d = a * b * c

Form1.BackColor = d
End Sub

Function randomise(lowerbound As Long, upperbound As Long)
 randomise = Int(Rnd * upperbound) + lowerbound
End Function

This is the actual code and it works :)

And a little bit simpler :)

Option Explicit

Private Sub Form_Click()
Form1.BackColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)
End Sub

I have a easier way to make a random color generater

here is the code

Function Randomize(Lowerbound As Long, Upperbound As Long)
Randomize = Int(Rnd * Upperbound) + Lowerbound
End Function

Private Sub command1_click()
timer1.enabled = true
end sub

Private Sub Timer1_Timer()
Dim a As Double
Dim b As Double
Dim c As Double
a = Randomize("0", "255")
b = Randomize("0", "255")
c = Randomize("0", "255")

form1.BackColor = RGB(a, b, c)
End Sub


hope this helps. I worked for me 100%, just add a timer and set enabled as false.

Button1_Click()
Button1.Backcolor=RGB(255rnd,255rnd,255*rnd)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.