0
votes

I'm in the stages of making a program which will require a tabpage to flash when an event happens

I've googled around, and i came across this: Blink tab header on receiving event. This is similar, but uses WPF, and i'm using WinForms, and i'm not even sure that does what i want :L

I've also found this: C#: Flash Window in Taskbar via Win32 FlashWindowEx. This is what i want, but obvious for the whole form, and in the taskbar, not 'in form'

Anyone got any ideas?

1

1 Answers

2
votes

I'm not saying this is the best way or even a great way to accomplish this, but it does work. I've used code similar to this when I needed to something similar.

The tabControl1 has two tabs and I blink tab 1 (the second tab).

For my example that I threw together, I set tabControl1's DrawMode property to "OwnerDrawFixed" and then a couple of buttons which start/stop the timer. The interval was something like 750ms but you could choose whatever, of course. On a timer1_Tick event, I swap out the current color and tell the tabControl1 to refresh itself. That'll make the DrawItem event get raised and then I either draw the rectangle the current color if it is tab page 1 or the backcolor if not. Then I draw the tabpage's text.

It works. Could use some tweaking for sure. Give it a whirl!

public partial class Form1 : Form
{
    Color currentColor = Color.Green;

    public Form1()
    {
        InitializeComponent();
    }

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

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (currentColor == Color.Yellow)
            currentColor = Color.Green;
        else
            currentColor = Color.Yellow;
        tabControl1.Refresh();
    }

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

    }

    private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (timer1.Enabled && e.Index == 1)
        {
            e.Graphics.FillRectangle(new SolidBrush(currentColor), e.Bounds);
        }
        else
        {
            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), e.Bounds);
        }
        Rectangle paddedBounds = e.Bounds;
        paddedBounds.Inflate(-2, -2);
        e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, this.Font, SystemBrushes.HighlightText, paddedBounds);
    }
}