Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7638271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:08:08+00:00 2026-05-31T08:08:08+00:00

Basically, I have a TabControl. I am drawing the text for the headers myself

  • 0

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();

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:08:10+00:00Added an answer on May 31, 2026 at 8:08 am

    All of your drawing is going to need to be done in the UI thread anyway so what I would do is to modify channelTabs_DrawItem(object sender, DrawItemEventArgs e) like so:

    channelTabs_DrawItem(object sender, DrawItemEventArgs e)
    {
        if(this.InvokeRequired)
        {
            this.BeginInvoke(() => channelTabs_DrawItem(sender,e));
            return;
        }
        TabControl ct = channelTabs;
        ...
    

    which will ensure that you are always running in the right thread and avoids using the potentially dangerous call to Invoke.
    If you really want to fix the code you are using, what you need to do is to return the value from Invoke like so:

    private TabControl getTab()
    {
        if (this.channelTabs.InvokeRequired)
        {
            getTabDelegate d = new getTabDelegate(getTab);
            return (TabControl)this.Invoke(d);
        }
        else
        {
            return channelTabs;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that extends TabControl, basically to have one with the tabs
I basically have this piece of code. char (* text)[1][80]; text = calloc(2821522,80); The
I basically have a table with headers that I read in from a DB
I have a TabControl in my WPF application. I want my application to basically
I basically have 5 text views that fill in one on top of the
Basically have many huge delimited files that I know I can import as a
I basically have this web page where you can narrow your search results by
I basically have a page which shows a processing screen which has been flushed
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.