简体   繁体   中英

winform C# custom tabcontrol close button

I had custom tabcontrol with a close button built, and it works perfectly but doesn't close the last tab standing, and I would love to change the style of the tab to match chrome's style. my problem is that I can't close all of the forms, there is always remaining the last one that can't be closed.

here is the code that I wrote to override

public static Rectangle GetRTLCoordinates(Rectangle container, Rectangle drawRectangle)
{
    return new Rectangle(
        container.Width - drawRectangle.Width - drawRectangle.X,
        drawRectangle.Y,
        drawRectangle.Width,
        drawRectangle.Height);
}
Image CloseImage;

private void frmMain_Load(object sender, EventArgs e)
{
    tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
    tabControl1.DrawItem += tabControl1_DrawItem;
    CloseImage = Pharmacy_Management.Properties.Resources.Close;
    tabControl1.Padding = new Point(10, 3);
}

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    try
    {
        var tabRect = this.tabControl1.GetTabRect(e.Index);
        tabRect.Inflate(-2, -2);
        var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
                                 tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
                                 CloseImage.Width,
                                 CloseImage.Height);

        var sf = new StringFormat(StringFormat.GenericDefault);
        if (this.tabControl1.RightToLeft == System.Windows.Forms.RightToLeft.Yes &&
            this.tabControl1.RightToLeftLayout == true)
        {
            tabRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, tabRect);
            imageRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, imageRect);
            sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
        }

        e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.Font, Brushes.Black, tabRect, sf);
        e.Graphics.DrawImage(CloseImage, imageRect.Location);

    }
    catch (Exception) { }
}

private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
    for (var i = 1; i < this.tabControl1.TabPages.Count; i++)
    {
        var tabRect = this.tabControl1.GetTabRect(i);
        tabRect.Inflate(-2, -2);
        var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
                                 tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
                                 CloseImage.Width,
                                 CloseImage.Height);
        if (imageRect.Contains(e.Location))
        {
            this.tabControl1.TabPages.RemoveAt(i);

            break;
        }
    }

}

and here is the code that i call a new form and show it in a new panel

private void button4_Click(object sender, EventArgs e)

{
    FRM_AMAT frm = new FRM_AMAT();
    tabControl1.Visible = true;
    frm.TopLevel = false;
    frm.Visible = true;
    frm.FormBorderStyle = FormBorderStyle.None;
    frm.Dock = DockStyle.Fill;
    TabPage tabPage = new TabPage();
    tabPage.Text = frm.Text;
    tabPage.Controls.Add(frm);
    tabControl1.Controls.Add(tabPage);
    tabControl1.SelectedTab = tabPage;
    frm.Show();
}
for (var i = 0; i < this.tabControl1.TabPages.Count; i++)
            {
                var tabRect = this.tabControl1.GetTabRect(i);
                tabRect.Inflate(-2, -2);
                var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
                                         tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
                                         CloseImage.Width,
                                         CloseImage.Height);
                if (imageRect.Contains(e.Location))
                {
                    this.tabControl1.TabPages.RemoveAt(i);
                    if (i == 0)
                    {
                        this.tabControl1.Visible = false;
                    }

                    break;
                }

            }

var i = 0 that would close the last tab:D:D:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM