如何使用TabControl更改选项卡上的文本颜色?

问题描述:

我正在使用带有两个TabPages的TabControl。



TabPage 1文本:测试数据并且具有带有列表视图的测试数据UserControl

TabPage 2文本:真实数据并且具有带有列表视图的真实数据用户控件



我正在尝试将TabPage 2的文本颜色从黑色更改为将新项目添加到列表时为蓝色。



TabPage 2:

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() - 此方法只是进入ReadDataUserControl并获取列表中的项目数。 br />


有人可以帮助我。



如果您有任何其他问题或更多见解,我正在尝试请告诉我。



谢谢



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

首先你需要一个新的listview控件因为当前的一个没有ItemAdded事件,所以;



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

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;

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);
        }
...





然后在你的表格中(tabcontrol所在的位置);





Then in your form (where tabcontrol is located);

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





将tabcontrol的绘制模式更改为OwnerDrawFixed我们应该手动绘制标签cuz标题不要拥有forecolor属性;







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


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







将tabpage的标签设置为null并将tabcontrol刷新为返回默认颜色。




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