Basically, I have a TabControl. I am drawing the text for the headers myself so they can be coloured when required. The calls to change colour are on a different thread then the one the TabControl is, so I am using delegates and such for cross-thread operations. Unfortunatly my method is not exactly reliable.
Here is the cross-threading part:
delegate TabControl getTabDelegate();
private TabControl getTab()
{
if (this.channelTabs.InvokeRequired)
{
getTabDelegate d = new getTabDelegate(getTab);
this.Invoke(d);
return null;
}
else
{
return channelTabs;
}
}
and here is the drawing code:
private void channelTabs_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
TabControl ct = getTab();
using (Brush br = new SolidBrush(TabColors[ct.TabPages[e.Index]]))
{
e.Graphics.FillRectangle(br, e.Bounds);
SizeF sz = e.Graphics.MeasureString(ct.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(ct.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2, e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
Rectangle rect = e.Bounds;
rect.Offset(0, 1);
rect.Inflate(0, -1);
e.Graphics.DrawRectangle(Pens.DarkGray, rect);
e.DrawFocusRectangle();
}
}
catch(Exception err)
{
MessageBox.Show(err.Message, "1");
}
}
As you can see, in some cases getTab(); returns null, which isn't exactly helpful. Is there any more...reliable method of doing this?
Here is the method that is called from the second thread to change the header colour:
private void SetTabHeader(TabPage page, Color color)
{
TabColors[page] = color;
channelTabs.Invalidate();
}
Without the cross-threading part, of course, I get exceptions thrown.
And as you can probably imagine, channelTabs is the GUI tab control.
Any help is apperciated, thanks!
-- Oh yea, and if it is actually helpful: private Dictionary TabColors = new Dictionary();