3

I wanted to add X button for each tab. The drawMode is OwnerDrawFixed. it works fine if it left to right. Once I allow RightToLeftLayout = true and RightToLeft = true, it does not look good because that he still adds the string from left to right, while that tab add from right to left.

How do I make it that string will also be right to left?

enter image description here

enter image description here

private void addCloseButton(object sender, DrawItemEventArgs e)
{
    //This code will render a "x" mark at the end of the Tab caption. 
    e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - 15 , e.Bounds.Top +4 );
    e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left+4, e.Bounds.Top+4);
    e.DrawFocusRectangle();

}

private void actionClose(object sender, MouseEventArgs e)
{
    //Looping through the controls.
    for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
    {
        Rectangle r = tabControl1.GetTabRect(i);
        //Getting the position of the "x" mark.
        Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 12, 10);
        if (closeButton.Contains(e.Location))
        {
            if (MessageBox.Show("?האם אתה רוצה לסגור טאב זה", "אישור", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.tabControl1.TabPages.RemoveAt(i);
                break;
            }
        }
    }

}
user3305653
  • 51
  • 1
  • 4
  • 4
    Take a look to this answer [Close button to TabPage with RightToLeft property](http://stackoverflow.com/a/34509304/4340666) – user4340666 Dec 29 '15 at 12:30

2 Answers2

0

Pass StringFormat with FormatFlags flag set to StringFormatFlags.DirectionRightToLeft to the DrawString():

StringFormat drawFormat = new StringFormat(StringFormatFlags.DirectionRightToLeft);

var bounds = new RectangleF(.. set actual bound rectangle for text... )    

e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, bounds, drawFormat);
PashaPash
  • 1,972
  • 15
  • 38
  • I do not know what to put in var bounds = new RectangleF(). If I put the point (0,0,845,592) that 845,592 is the size of the tabcontrol, so it first comes out fine and after that it tramples on it. – user3305653 Feb 13 '14 at 13:34
  • try to pass e.Bounds first (bounds of the tab header), then adjust it to avoid text overlap with [X] – PashaPash Feb 13 '14 at 14:50
  • i can't to add after e.Bounds header, i have Top or Right or Left and etc. i have 4 fields that I need to add to new RectangleF(), and I'm get involved. if i put: var bounds = new RectangleF(0, 0, e.Bounds.Top, e.Bounds.Right+30); it does not show me the string. – user3305653 Feb 13 '14 at 15:59
  • try RectangleF(e.Bounds.Left, e.Bounds.Top, e.Bound.Width, e.Bounds.Height) – PashaPash Feb 13 '14 at 16:02
  • It's not work well. It starts from left to right and any additional tab add from left to right As it was before it. – user3305653 Feb 13 '14 at 16:12
  • Make sure that both RightToLeft and RightToLeftLayout is set to true for tab control. RightToLeftLayout controls tab header order (and affects e.Bounds values) – PashaPash Feb 13 '14 at 16:20
  • Of course, RightToLeft is set to Yes and RightToLeftLayout is set to true. – user3305653 Feb 13 '14 at 16:28
-1

check here http://www.microsoft.com/middleeast/msdn/visualstudio2005.aspx you should use textrenderer instead of drawstring