I have a tab control say tabMain . In the first tab which is my Home tab I have a dropdown and an Add Button. When I click the add button a new tab would be opened with the selected item as tab header. Along with the tab header I need to include a close button . Right now I have the close button But I also need to set a background color for the close button. Here is a portion of my code.
TabPage newTabPage= new TabPage();
newTabPage.Text = cmbType.SelectedItem.ToString() +" X";
tabMain.TabPages.Add(newTabPage);
In my mouse down event I have included the function for close.
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");
}
}
My close button is working fine. But I need to set a color for the close button . I have also tried including a label . Here is the code
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);
Any help would be appreciated. Thanks in advance
Aucun commentaire:
Enregistrer un commentaire