Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using a TabControl with two TabPages.

TabPage 1 Text: "Test Data" and has a Test Data UserControl with a listview
TabPage 2 Text: "Real Data" and has a Real Data UserControl with a listview

I am trying to change the text color of TabPage 2 from black to blue when a new item gets added to the list.

TabPage 2:
public void RealDataCount()
{
  int total = this._realDataView.GetRealDataCountInList();
  this.RealDataPage.Text = string.Format("Real Data: {0}", total);
}


GetRealDataCountInList() - this method just goes into the ReadDataUserControl and get a count of items in the list.

Can someone please help me.

If you have any other questions or more insight what I am trying to do please let me know.

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 16-Apr-14 15:47pm    
Did you try ForeColor?
—SA
Member 10622146 16-Apr-14 15:51pm    
Yes, it does not change the tab text.

1 solution

First of all you need a new listview control cuz current one does not have ItemAdded event, so;

C#
public class MyListView : ListView
{
    public event EventHandler ItemAdded;

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        switch (m.Msg)
        {
            case 0x1007:    //ListViewItemAdd-A
                if (ItemAdded != null)
                    ItemAdded(this, null);
                break;
            case 0x104D:    //ListViewItemAdd-W
                if (ItemAdded != null)
                    ItemAdded(this, null);
                break;
            case 0x1008: //Item removed
                System.Diagnostics.Debug.WriteLine("Item removed");
                break;
            default:
                break;
        }
    }
}


then put it in your usercontrol, and your usercontrol should be something like this;

C#
public partial class ReadDataUserControl : UserControl
    {
        public event EventHandler ItemAddedToListView;

        public ReadDataUserControl()
        {
            InitializeComponent();
            myListView1.ItemAdded += this.myListView1_ItemAdded;
        }

        private void myListView1_ItemAdded(object sender, EventArgs e)
        {
            if (ItemAddedToListView != null)
                ItemAddedToListView(this, e);
        }
...


Then in your form (where tabcontrol is located);

C#
public partial class FormHome : Form
    {

        public FormHome()
        {
            InitializeComponent();

            readDataUserControl1.ItemAddedToListView += readDataUserControl1_ItemAddedToListView;
        }


        private void readDataUserControl1_ItemAddedToListView(object sender, EventArgs e)
        {
            if ((sender as Control).Parent is TabPage)
            {
                (sender as Control).Parent.Tag = Color.Blue;
                tabControl1.Refresh();
            }
        }


Change your tabcontrol's draw mode to OwnerDrawFixed we should draw tabs manually cuz headers don't have forecolor property;


C#
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    Graphics g = e.Graphics;
    Brush _TextBrush;

    TabPage _TabPage = tabControl1.TabPages[e.Index];

    Rectangle _TabBounds = tabControl1.GetTabRect(e.Index);
    e.DrawBackground();

    if (tabControl1.TabPages[e.Index].Tag != null)
    {
        _TextBrush = new SolidBrush((Color)tabControl1.TabPages[e.Index].Tag);
    }
    else
    {
        _TextBrush = new System.Drawing.SolidBrush(e.ForeColor);
    }

    Font _TabFont = e.Font;

    // Draw string. Center the text.
    StringFormat stringFlags = new StringFormat();
    stringFlags.Alignment = StringAlignment.Center;
    stringFlags.LineAlignment = StringAlignment.Center;
    g.DrawString(tabControl1.TabPages[e.Index].Text, _TabFont, _TextBrush,
                 _TabBounds, new StringFormat(stringFlags));
}



Set tag of tabpage to null and refresh tabcontrol to return to default color.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900