now, we want this:
private void timer1_Tick(object sender, EventArgs e)
{
Random r = new Random();
int i = r.Next(0, 255);
int y = r.Next(0, 255);
int u = r.Next(0, 255);
groupBox1.BackColor = Color.FromArgb(i,y,u);
groupBox1.ForeColor = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
}
i will explain this. Also, from now on anythign in this color, is an explanantion of theory, and can be ignored, unless you want to know WHY.
timer1_Tick, Form1_Load, and button1_Click are events. Most Object Oriented Programming is also Event Driven, meaning something has to happen for code to run. Clicking the button is an event you initiate. Running the program runs Form1_Load, and the nature of the Timer control causes it to fire off the timer1_Tick event on a regular basis (i will explain the ins and outs of the Timer control at another time)
anyway, this is the breakdown of the code above.
Random r = new Random();
in this line, we instantiate a Random object. Think of it as grabbing a hammer. we instantiate it as "r". So we tak eout a hammer, now we will call that hammer "r". No other hammers can be r, nor can any wrenches, screwdrivers, etc.
r is a new Random.
now, we declare some variables. If you have taken Algebra, you are at least familiar with the concept of a variable. Inprogramming, variables arent always numbers, but right here they are.
this:
int i = r.Next(0, 255);
int y = r.Next(0, 255);
int u = r.Next(0, 255);
translates to this:
make an integer object called i, and make it's value some random number from 0 to 255. (more on why in a minute)
from now on, these letters (i,y,and u) will be a specific number.
the next part:
groupBox1.BackColor = Color.FromArgb(i,y,u);
groupBox1.ForeColor = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
is where the fun happens. groupBox1.BackColor defines the background color of the groupbox i had you guys create earlier. Color.FromArgb is a neat little Method that takes number values from 0 through 255 and translates them into a color. Anyone who has played with custom colors on your computer hasd probably seen that every color possible in a computer can be defined with varying values of Red Blue and Green (hence RGB). this lets oyu do it with numbers. which makes for some fun.
Anyway, the back color will change to whatever combination of numbers your 3 int variables recieved.
the one with groupBox1.ForeColor defines the text color of the groupbox. this one is here to illustrate that you dont HAVE to assign variables when calling r.Next. let me know if you have any questions.
For fun, go back to Form1.cs [Design] and click on the groupbox, and find Text in properties. Chenge it to Colors! or whatever text you want. now hit Ctrl-F5 to compile and launch your program.
Isnt that fun? Kind of psychadelic...
here is how it works. When you launch the program, Form1_Load fires off as part of the program startup. It in turn runs timer1.Start();, which gets the timer1 ball rolling. Now, every second, timer1 executes timer1_Tick, thereby generating random numbers and getting a color value from them, and assigning the new colors to the text and background of the groupbox, making it all flashy and fun. :)
|