views:

47

answers:

3

Hello,

C# certainly isn't my strong suit so I appreciate all the generous folk sharing their knowledge. I'm working with a Windows Form and I've read up on events and have found some excellent help on how to modify a TabControl so I can have an OnDraw event that will add some coloring to the tabs.

The color of each tab is based upon the state of a connection variable: Current (green) Lost (red) Stale (yellow)

The OnDraw event works excellent for updating the color of each tab, but that only occurs when the user selects a different tab to view.

What I would like to happen is for the color of each tab to be updated whenever the connection state changes. For example, let's say Tab#1 is colored green, but then the connection state changes to stale so now the tab needs to be colored yellow but it won't get colored like that until the user clicks on a different tab and the OnDraw event is triggered.

So I'm trying to figure out how to do that. When the OnDraw event is triggered normally (by the user clicking on a different tab) a "DrawItemEventArgs" parameter is passed into the even handler. That variable is already populated with the pertinent data needed to figure out which tab was clicked on, the boundaries of that tab and etc. So I am unsure where it came from or how I can programmatically re-create such a call to re-color the tabs whenever the connection variable changes.

Please let me know if I need to clarify anything! Thank you.

A: 

If you want to have a constant refresh going, then you probably need to create System.Timers.Timer object.

Once you create a Timer and set the timer tick value to whatever interval you need (in milliseconds) it will fire the OnTimerTick event at regular intervals. From this event you can trigger a call to your OnDraw method through the Invalidate() method. Invalidate tells the system that your screen needs to be refreshed and it will call OnDraw and OnPaint at the next available opportunity.

MikeD
Everyone's input was so fast, I apologize for not getting back to you three yesterday - I didn't have a chance to try your suggestion. MikeD, I hadn't considered a timer. That will certainly work great because I need to timeout the connection status (i.e. Current -> Stale) if no new data is received for a period of time. Thanks!
zlarsen
A: 

You can call Invalidate() on the control to force a repaint.

Jake Pearson
Worked perfectly! At first was looking for a missing "using" statement, but then I figured out it was my TabControl that needed to call it, either inside the TabControl class (this.Invalidate()) or in my Form, myTabControl.Invalidate(); Thank you very much!
zlarsen
If I had the rep I would vote you all up 1! ;)
zlarsen
A: 

If you have an event fired when your connection state changes you could do an

InvalidateVisual()

on all of your tabs from within that event.

FlyingStreudel
Thanks for your input!Invalidate() seemed to work fine for me, is there a notable difference between it and InvalidateVisual()?
zlarsen
InvalidateVisual is what you use in WPF, my mistake
FlyingStreudel