Hey guys.

Hope you all well.

A quick question, how can I make a form change backcolors so that it appears almost like its 'flashing'. Basically what I am trying to achieve is making a form more visible and getting the attention of the user.

I can imagine it must be something quite simple, but I cant get it to work.

All that it should do for example is change from backcolor blue to backcolor red every half second until the user clicks a button.

Fanx guys.

Go well and happy coding :)!

HMM. you could use the timer.
http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingwithTimerControlinCSharp11302005054911AM/WorkingwithTimerControlinCSharp.aspx

And then

if (this.BackColor == Color.Red)
     this.BackColor == Color.Blue;
else
     this.BackColor == Color.Red;

Hi Finito

Fanx for the reply.

I tried the following, but I am new to C# and especially the timer object, so pardon for any 'newbie code':
private void timer1_Tick(object sender, EventArgs e)
{

timer1.Interval = 6000;
timer1.Start();

if (timer1.Interval == 3000)
{
BackColor = Color.Orange;

}
else if (timer1.Interval == 6000)
{
BackColor = Color.Blue;
}

timer1.Stop();

}

Fanx!

Go well and happy coding :)!

you kind of have to call the timer class either in form load or by clicking a button. timer1.Start(); in formload

timer1.Stop(); once your said button is clicked.

you kind of have to call the timer class either in form load or by clicking a button. timer1.Start(); in formload timer1.Stop(); once your said button is clicked.

Hey finitio

I tried what you suggetsed:

    private void timer1_Tick(object sender, EventArgs e)
    {

         if (timer1.Interval == 3000)
        {
            BackColor = Color.Red;

        }
        else if (timer1.Interval == 6000)
        {
            BackColor = Color.Blue;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        timer1.Interval = 6000;
        timer1.Start();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Stop();
    }

its does change to blue, put it doesnt somehow restart the timer - meaning that it doest alternate between red and blue - what could i be doing wrong?

Fanx :)

This should work:
Start a new forms app, drop a button on it and implement its click event.
Next fill in the timer code you see below:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Timer T = new Timer();

        public Form1()
        {
            InitializeComponent();
            T.Interval = 1000; //Tick every second
            T.Tick += new EventHandler(T_Tick);
            T.Start();          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            T.Stop();
        }

        private void T_Tick(object sender, EventArgs e)
        {
            if (this.BackColor == Color.Red)
                this.BackColor = Color.Blue;
            else
                this.BackColor = Color.Red;
        }
    }
}

u said u want to change the color every half second
so you could do the following

private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 500;
timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{

if (BackColor == Color.Red)
{
BackColor = Color.Blue;

}
else {
BackColor = Color.Red;
}
}

sorry ddanbe...i guess we thought of it at the same time....

First of all I want you to use code tag.

Second of all I don't know why you don't see the problem. Even though I flat out gave you the right code.

Read your code. you telling the machine to do something at the interval of 3 seconds and then do something else at the interval or 6 seconds.

I want you to figure out the rest. no spoonfeeding.

wtf babbu no spoonfeeding you want him to learn. GOSH

shame on you ddanbe.

Fanx to ddanbe and babbu it works great!

Finitio - sincere apologies if I somehow offended you or the daniweb sight in anyway. If you are demanding me to use Tags I suppose you must be the administrator and if so please could you either explain waht you wish for me to do or what help link I should go to to adhere to daniweb rules.

I really do apologise for being new in programming and if you mean peopling helping me out as spoonfeeding then I apologise as well to ddanbe and babbu

many thanks.

Go well and happy coding :)

geoNeo, I am not a mod,

I asked you to use code tagging cause its easier to read the code for the person solving.

you did nothing wrong. But I wanted you t figure out the problem yourself. what babbu and ddanbe did is not give you chance to figure out the answer to your own question.

I am new here too I joined 3 days ago.

Thank finitio

Will endevour to try harder next time.

Go well and happy coding :)

{
            Random rnd = new Random();
            switch(rnd.Next(1, 10))
            {
                case 1:
                    this.BackColor = Color.AliceBlue;
                    break;
                case 2:
                    this.BackColor = Color.AliceBlue;
                    break;
                case 3:
                    this.BackColor = Color.AliceBlue; 
                    break;
                case 4:
                    this.BackColor = Color.BlanchedAlmond;
                    break;
                case 5:
                    this.BackColor = Color.Khaki;
                    break;
                case 6:
                    this.BackColor = Color.LightSlateGray;
                    break;
                case 7:
                    this.BackColor = Color.Peru;
                    break;
                case 8:
                    this.BackColor = Color.YellowGreen;
                    break;
                case 9:
                    this.BackColor = Color.Tan;
                    break;
                case 10:
                    this.BackColor = Color.Navy;
                    break;
                   
            }
        }

This will make the colours random

hey edgareatis!

Yea that is kinda cool fanx man :)

go well and happy coding :)

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.