vb.net 如何更改每个选项卡的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30039627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:05:36  来源:igfitidea点击:

How do I change the color of each tab?

vb.net

提问by TEC C

I have a form that has four tabs on it I would like each tab to be a different color. The only thing I have been able to find on the internet is how to change the color of the selected tab and the rest of the tabs stay the original color. I have not found anything to give each tab its own color. The code I currently have is.

我有一个表单,上面有四个选项卡,我希望每个选项卡都具有不同的颜色。我在互联网上唯一能找到的是如何更改所选选项卡的颜色,而其余选项卡保持原始颜色。我没有找到任何东西可以给每个标签自己的颜色。我目前拥有的代码是。

Private Sub TabControl1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem

        Dim g As Graphics = e.Graphics
        Dim tp As TabPage = TabControl1.TabPages(e.Index)
        Dim br As Brush
        Dim sf As New StringFormat

        Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)

        sf.Alignment = StringAlignment.Center

        Dim strTitle As String = tp.Text

        If TabControl1.SelectedIndex = e.Index Then

            'this is the background color of the tabpage header
            br = New SolidBrush(Color.LightSteelBlue) ' chnge to your choice
            g.FillRectangle(br, e.Bounds)

            'this is the foreground color of the text in the tab header
            br = New SolidBrush(Color.Black) ' change to your choice
            g.DrawString(strTitle, TabControl1.Font, br, r, sf)

        Else

            'these are the colors for the unselected tab pages 
            br = New SolidBrush(Color.Blue) ' Change this to your preference
            g.FillRectangle(br, e.Bounds)
            br = New SolidBrush(Color.Black)
            g.DrawString(strTitle, TabControl1.Font, br, r, sf)

        End If
    End Sub

回答by alainlompo

There are two things that you need to do:

您需要做两件事:

First is to change the DrawMode of the TabControl and set it to OwnerDrawFixed

首先是改变TabControl的DrawMode,设置为OwnerDrawFixed

drawmode

绘制模式

And the second is to handle the TabControl DrawItem event

第二个是处理TabControl DrawItem事件

Here is an example:

下面是一个例子:

 Private Sub TabControl1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles TabControl1.DrawItem
    Select Case e.Index
        Case 0
            e.Graphics.FillRectangle(New SolidBrush(Color.Red), e.Bounds)
        Case 1
            e.Graphics.FillRectangle(New SolidBrush(Color.Blue), e.Bounds)
        Case 2
            e.Graphics.FillRectangle(New SolidBrush(Color.Magenta), e.Bounds)

    End Select

    Dim paddedBounds As Rectangle = e.Bounds
    paddedBounds.Inflate(-2, -2)
    e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, Me.Font, SystemBrushes.HighlightText, paddedBounds)


End Sub

And here is what it looks like (I change the tab colors of the first three tab pages only, the others can be done easily by adding new cases to select case)

这是它的样子(我只更改前三个标签页的标签颜色,其他的可以通过添加新案例来选择案例轻松完成)

tab colors

标签颜色

回答by Andy

Is TabControl1 a Tab control you are adding to the form via the designer? Why not just set the TabBackColor property for each tab when you create it there?

TabControl1 是您通过设计器添加到表单的 Tab 控件吗?在那里创建时,为什么不为每个选项卡设置 TabBackColor 属性?

If not (you do have to do it via code), just use a loop to cycle through each tab in the TabControl1's collection of tab pages (TabControl1.TabPages) and set the TabBackColor property for each there.

如果没有(您必须通过代码来完成),只需使用循环循环浏览 TabControl1 的选项卡页集合 (TabControl1.TabPages) 中的每个选项卡,并为每个选项卡设置 TabBackColor 属性。