Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#winforms中为选项卡页设置关闭按钮的颜色_C#_Winforms_Tabcontrol_Tabpage - Fatal编程技术网

如何在c#winforms中为选项卡页设置关闭按钮的颜色

如何在c#winforms中为选项卡页设置关闭按钮的颜色,c#,winforms,tabcontrol,tabpage,C#,Winforms,Tabcontrol,Tabpage,我有一个tab控件,比如tabMain。在第一个选项卡(我的主页选项卡)中,我有一个下拉列表和一个添加按钮。单击“添加”按钮时,将打开一个新选项卡,其中所选项目作为选项卡标题。除了选项卡标题外,我还需要包括一个关闭按钮。现在我有关闭按钮,但我还需要为关闭按钮设置背景色。这是我代码的一部分 TabPage newTabPage= new TabPage(); newTabPage.Text = cmbType.SelectedItem.ToString() +" X"; tabMai

我有一个tab控件,比如tabMain。在第一个选项卡(我的主页选项卡)中,我有一个下拉列表和一个添加按钮。单击“添加”按钮时,将打开一个新选项卡,其中所选项目作为选项卡标题。除了选项卡标题外,我还需要包括一个关闭按钮。现在我有关闭按钮,但我还需要为关闭按钮设置背景色。这是我代码的一部分

 TabPage newTabPage= new TabPage();   
 newTabPage.Text = cmbType.SelectedItem.ToString() +"  X";
 tabMain.TabPages.Add(newTabPage);
在我的鼠标按下事件中,我包含了关闭功能

 private void tabMain_MouseDown(object sender, MouseEventArgs e)
    {
        try
        {                
            if (tabMain.SelectedIndex > 0)
            {
                Rectangle r = tabMain.GetTabRect(tabMain.SelectedIndex);                    
                Rectangle closeButton = new Rectangle(r.Right - 15, r.Y, 15, 18);

                if (closeButton.Contains(e.Location))
                {
                    if (MessageBox.Show("Would you like to Close this Tab?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        this.tabMain.TabPages.RemoveAt(tabMain.SelectedIndex);
                    }
                }
            } 
        }
        catch(Exception)
        {
            MessageBox.Show("Exception in closing a tab");
        }
    }
我的关闭按钮工作正常。但是我需要为关闭按钮设置一种颜色。我也尝试过添加标签。这是密码

TabPage newTabPage = new TabPage();                
Label labelClose = new Label();
labelClose.Text = "  X";
labelClose.BackColor = System.Drawing.Color.Red;               
newTabPage.Text = cmbType.SelectedItem.ToString() + labelClose.Text;
tabMain.TabPages.Add(newTabPage); 
任何帮助都将不胜感激。
提前感谢

要为矩形添加背景色,您需要执行以下操作:

要绘制填充颜色的矩形,需要图形对象和 从笔刷派生的对象,如SolidBrush或 线性辐射刷。图形对象提供圆角矩形 方法,笔刷对象提供颜色和填充信息


你没有一个关闭按钮,你有一个名称为closeButton的矩形,有点像一个按钮。如果您想要一个closebutton并将其着色,请使用一个真实的按钮,并将eventhandler附加到click事件。此代码将比您尝试执行的操作简单得多。@PhilipStuyck:是..对。。我还可以添加一个关闭按钮。。但是,尽管我添加了一个按钮,但是如何只为它设置颜色呢?当然,通过它的属性,一旦你使用一个普通的按钮,你的问题就变得非常简单了。看看你自己设置标签背景色的代码。你应该定制你的标签控件。这可能对你有帮助。另外,请选中此项或转到WPF,在WPF中,在选项卡标题中添加按钮要容易得多。问题是你们真的想在标题上加个按钮吗?谢谢。但这没什么帮助。。我也试过了,但没用(图形是一次性对象,请使用使用机构或在完成后处置图形对象。
//Create graphic object for the current form
Graphics gs = this.CreateGraphics();
//Create a rectangle object
Rectangle closeButton = new Rectangle(r.Right - 15, r.Y, 15, 18);
//Fill the rectangle with red color
gs.FillRectangle(new SolidBrush(Color.Red), closeButton);