Okay so I've always been a fan of creating and calling my own functions. With C# it's pretty simple. We're going to start out with a basic Windows Forms Application for this in the end your application will animate a label on the form though multiple colors. Only difference here is we will be creating our own functions to do it. :D
Create a label on the form (name this myLabel) and create a timer (name this myTimer). Set the timer interval at 500, and set the fore color of the label to Red. Now double click the form to view the source code and automatically add the form load event.
Your source should be pretty empty:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace myApp // Your namespace can be different. This is your project name.
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Okay so I'm just going to put in what we're working with for the code box so we don't have a gigantic post in the end. :D
Anyways, in the section that says "Form1_Load" we're going to start our timer. We do this by typing in myTimer.Start();
private void Form1_Load(object sender, EventArgs e)
{
myTimer.Start();
}
Now we want to go back to viewing our form and double click the timer to set up the timer tick event. Witch should add the following to the bottom of your source:
private void myTimer_Tick(object sender, EventArgs e)
{
}
Okay so now we are going to create our first function. :)
Above the "Form1_Load" event we are going to start by typing in void functionOne().
void functionOne()
{
}
With your new function created we must give it something to do and then call it to work. So let's start by making it check our label's fore color. We will do this with an if statement. We want it to see if our label is red since that's what we set it to in the beginning.
void functionOne()
{
if (myLabel.ForeColor == Color.Red)
{
}
}
So we have it checking for our label color, but what to do once it gets there? Okay well we have to make it change the color. So let's change it to blue.
void functionOne()
{
if (myLabel.ForeColor == Color.Red)
{
myLabel.ForeColor = Color.Blue;
}
}
Okay so now we have our first if statement up and running. Now we need to make it change from blue back to red. So we use an else if statement.
void functionOne()
{
if (myLabel.ForeColor == Color.Red)
{
myLabel.ForeColor = Color.Blue;
}
else if (myLabel.ForeColor == Color.Blue)
{
}
}
All that's left for our function is to make it change the label color back to red. So basically let's do the same thing we did to turn it blue.
void functionOne()
{
if (myLabel.ForeColor == Color.Red)
{
myLabel.ForeColor = Color.Blue;
}
else if (myLabel.ForeColor == Color.Blue)
{
myLabel.ForeColor = Color.Red;
}
}
Wow, we're almost done. If you've made it this far all you have to do is call your function. This is the easiest part. Since we're calling it every time the timer ticks, we are going to call it in the myTimer_Tick function. So in there you simply say, functionOne(); and that's it. :D
// Complete source.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace myApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void functionOne()
{
if (myLabel.ForeColor == Color.Red)
{
myLabel.ForeColor = Color.Blue; // Change label color to blue.
}
else if (myLabel.ForeColor == Color.Blue)
{
myLabel.ForeColor = Color.Red; // Change label color to red.
}
}
private void Form1_Load(object sender, EventArgs e)
{
myTimer.Start(); // Start the timer.
}
private void myTimer_Tick(object sender, EventArgs e)
{
functionOne(); // Call our new function every time the timer ticks.
}
}
}
All done, that was simple enough right? This will still work for the events with event arguments but you have to specify the argument conditions in the () of the call or you'll get an error. Like if your event argument is a boolean then you'll have to put 'true' or 'false' in the ().
functionOne(true);
or
functionOne(false);
:D
Cheers,
xX.TaCo.Xx