create a button to delete the selected tab

AMER SAID 396 Reputation points
2023-04-27T11:18:25.83+00:00

I used the following code to create a button to delete the selected tab.

The problem is that if the tab is changed to be Arabic from right to left, a design problem occurs.

How to change the color of the tab cover header + how to add the delete button when adjusting the control from right to left

left Button

Screenshot2

right button

ScreenshotRIGHT

 Point _imageLocation = new Point(20, 4);
        Point _imgHitArea = new Point(20, 4);
        Image closeImage;
      
        private void Form1_Load(object sender, EventArgs e)
        {
            closeImage = Properties.Resources.close;
            tabControl1.Padding = new Point(20, 4);
        }

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Image img = new Bitmap(closeImage);
            Rectangle r = e.Bounds;
            r = this.tabControl1.GetTabRect(e.Index);
            r.Offset(2, 2);
            Brush TitleBrush = new SolidBrush(Color.Black);
            Font f = this.Font;
            string title = this.tabControl1.TabPages[e.Index].Text;
            e.Graphics.DrawString(title, f, TitleBrush, new PointF(r.X, r.Y));
            e.Graphics.DrawImage(img, new Point(r.X + (this.tabControl1.GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y));
        }

        private void tabControl1_MouseClick(object sender, MouseEventArgs e)
        {
            TabControl tabControl = (TabControl)sender;
            Point p = e.Location;
            int _tabWidth = 0;
            _tabWidth = this.tabControl1.GetTabRect(tabControl.SelectedIndex).Width - (_imgHitArea.X);
            Rectangle r = this.tabControl1.GetTabRect(tabControl.SelectedIndex);
            r.Offset(_tabWidth, _imgHitArea.Y);
            r.Width = 16;
            r.Height = 16;
            if (tabControl1.SelectedIndex >= 1)
            {
                if (r.Contains(p))
                {
                    TabPage tabPage = (TabPage)tabControl.TabPages[tabControl.SelectedIndex];
                    tabControl.TabPages.Remove(tabPage);
                }
            }
        }

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-04-28T08:20:13.0933333+00:00

    Hi,
    Use Rectangle imageRect = new Rectangle(e.Bounds.X + 4, e.Bounds.Y + 4, 16, 16) to modify the image position

      tabControl1_DrawItem:
     
    
           
                    TabPage tabPage = tabControl1.TabPages[e.Index];
                    // Get the current tab page's text
                    string title = this.tabControl1.TabPages[e.Index].Text;
                    // Get the current tab page's image
                    Image image = closeImage;
    
                    Rectangle r = e.Bounds;
                    r = this.tabControl1.GetTabRect(e.Index);
                    r.Offset(2, 2);
                    // Set the bounds for the image
                    Rectangle imageRect = new Rectangle(e.Bounds.X + 4, e.Bounds.Y + 4, 16, 16);
                    Brush TitleBrush = new SolidBrush(Color.Black);
                    e.Graphics.FillRectangle(new SolidBrush(Color.CadetBlue),e.Bounds );
                    // Draw the image and text
                    if (image != null)
                        e.Graphics.DrawImage(image, imageRect);
    
                    e.Graphics.DrawString(title, this.Font, TitleBrush, new PointF(r.X + 22, r.Y + 2));
      
    
            
    
    
            private void tabControl1_MouseClick(object sender, MouseEventArgs e)
            {
                tabControl1.TabPages.Remove(tabControl1.SelectedTab);
            }
    

    change the color of the tab cover header

    Do you mean the color of tabpage? e.Graphics.FillRectangle(new SolidBrush(Color.CadetBlue),e.Bounds );

    enter image description here

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.