3

Using WinForms, I have a string which I want to set into a ToolTip. The string is separated by lines with Environment.NewLine, like so:

x.ToolTipText = "aaaaaa" + Environment.NewLine + "bbb";

when this string is set to the ToolTip it looks:

aaaaaa
bbb

But my problem is when I want it to be localized to Arabic and ToolTip does not support the default RightToLeft property, so it looks very bad. I want it to look like:

aaaaaa
   bbb

Also: String.PadLeft does not help!

Any idea?

Abel
  • 56,041
  • 24
  • 146
  • 247
song
  • 31
  • 1
  • 2

4 Answers4

11

Second try. Didn't manage to get the previous solution attempt to behave correctly. Found an alternate way by rendering the tooltip using a custom draw method.

Setup the controls:

string tip = "aaaaaa" + Environment.NewLine + "bbb";           
toolTip1.OwnerDraw = true;
toolTip1.Draw += toolTip1_Draw;
toolTip1.SetToolTip(textBox1, tip);

Handle the draw event:

private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
{
    e.Graphics.FillRectangle(SystemBrushes.Info, e.Bounds);
    e.DrawBorder();           
    e.DrawText(TextFormatFlags.RightToLeft | TextFormatFlags.Right);
}
PHeiberg
  • 29,411
  • 6
  • 59
  • 81
3

If it's windows forms I guess you have to rely on the unicode control characters to mark the tooltip text as RTL.

string myToolTipText = "aaaaaa"+Environment.newLine+"bbb";
char rle_char = (char)0x202B;// RLE embedding 
control.RightToLeft = RightToLeft.Yes;
control.ToolTipText = rle_char + myToolTipText;

Check this blog for more information on authoring RTL applications.

If it's a web application you can check the w3c authoring RTL applications guidelines. Basically it comes down to the same solution as for windows forms, adding RLE chars.

PHeiberg
  • 29,411
  • 6
  • 59
  • 81
  • I unfortunately didn't have time to test it out, so I trusted the blog. Found another solution that I managed to get to work. I posted in a new reply. – PHeiberg Nov 25 '09 at 14:53
3

The ToolTip control already supports this. The key is that the control for which you display the tip has a right-to-left layout. This example form demonstrates this:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        ToolTip mTip = new ToolTip();
        public Form1() {
            InitializeComponent();
            this.RightToLeft = RightToLeft.Yes;
            this.RightToLeftLayout = true;
        }
        protected override void OnMouseDown(MouseEventArgs e) {
            mTip.Show("Hi\nthere", this);
        }
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Just curious, but how does that work with mixed RTL / LTR text? – Abel Nov 26 '09 at 00:00
  • This does not work if only RightToLeft = RightToLeft.Yes is set. I did not test with RightToLeftLayout = true which I cannot use in my application. – Elmue Aug 29 '21 at 19:36
0

Just some general thoughts to give an idea about what's going on: the tooltip class does not draw text like a multiline textbox. The whole tool-tip is considered one line without a binding rectangle, hence RTL vs LTR have no meaning (other then the order of characters within the text).

Result: using normal right-to-left or left-to-right switches either with Unicode RTL codepoints or otherwise, won't help when it comes to aligning: none is applied.

One way out is (ab)using a TextBox and place it nicely over the place of the tooltip. But others have shown prettier ways with owner-drawn tooltips, which is definitely the way to go.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • This answer is defintely wrong. A tooltip text may contain linebreaks which are displayed correctly by Windows. A tooltip is NOT considered "one line". – Elmue Aug 29 '21 at 19:30
  • @Elmue, this was back in 2009..., but yeah, it's vague. I think I didn't mean to imply that tooltips cannot _show_ multiple lines (by virtue of using the crlf characters), but that its default behavior is not tailored for bi-drectional stuff. As the top answer showed, you can get the handle of the tooltip and change this behavior, but only for the whole window. It just won't listen to [Unicode RTL marks](https://en.wikipedia.org/wiki/Right-to-left_mark) etc, disallowing inline bi-directional text. – Abel Oct 18 '21 at 16:59