我有一个标签控件说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);
任何帮助将不胜感激。 提前致谢
答案 0 :(得分:0)
要为矩形添加背景颜色,您需要执行以下操作:
要绘制一个填充颜色的Rectangle,您需要一个Graphics对象和 从Brush中派生的对象,如SolidBrush或 一个LinearGradientBrush。 Graphics对象提供FillRectangle 方法和Brush对象提供颜色和填充信息。
//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);