0

I have a tabcontrol in my form, and I also have close button (image) on my tabs. But when I change language from it stops drawing the close button image.

private void arabicToolStripMenuItem_Click(object sender, EventArgs e)
    {
        switch(Thread.CurrentThread.CurrentUICulture.IetfLanguageTag)
        {
            case "ar-AE": Thread.CurrentThread.CurrentUICulture =
                    new System.Globalization.CultureInfo("en-US");
                break;

            case "en-US": Thread.CurrentThread.CurrentUICulture =
                    new System.Globalization.CultureInfo("ar-AE");
                break;

            //default: Thread.CurrentThread.CurrentUICulture =
            //    new System.Globalization.CultureInfo("ar-AE");
            //    break;
        }

        this.Controls.Clear();
        InitializeComponent();
    }

The above code is for changing language.

private void MyTasheelMain_Load(object sender, EventArgs e)
    {
        this.tabControl1.Padding = new Point(12, 4);
        this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;

        this.tabControl1.DrawItem += tabControl1_DrawItem;
        this.tabControl1.MouseDown += tabControl1_MouseDown;
        this.tabControl1.Selecting += tabControl1_Selecting;
        // this.tabControl1.HandleCreated += tabControl1_HandleCreated;   
    }
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
    {
        var CloseImage = Properties.Resources.Close;
        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);
    }

private void tabControl1_MouseDown(object sender, MouseEventArgs e)
    {
        for (var i = 0; i < this.tabControl1.TabPages.Count; i++)
        {
            var tabRect = this.tabControl1.GetTabRect(i);
            tabRect.Inflate(-2, -2);
            var closeImage = Properties.Resources.Close;
            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;
            }
        }
    }

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

The first time it draws the close button, but when i change the language it does not show. All I know is that i need to recall this Drawitem event everytime it changes the language, because when the language is changed it clears controls and then initialize it again, but tabcontrol remains the same.

Any help would be much appreciated. Thanks

  • Writing code like this is indeed a very good way to cause this problem Using the Dispose() method/using statement in winforms code is extremely important. Controls.Clear() is wrong, Controls.RemoveAt() is wrong, forgetting to dispose CloseImage and closeImage is wrong. This exhausts the limited space on the desktop heap for windows and drawing objects, you don't necessarily get an exception for it. Task Manager can be helpful, add the Handles, USER Objects and GDI Objects columns. Steadily climbing numbers, as you should observe with this code, spell doom. – Hans Passant Feb 21 '19 at 12:43
  • @HansPassant Sir I am using tabcontrol1.Dispose() and menustrip1.Dispose(). After the language change the image of close still do not show. What else should I try. Any Help Would be appreciated. Thanks – Hassan Adam Khan Feb 26 '19 at 07:23

0 Answers0