How to align the right edges of a control and tooltips in C #

I want to show the ToolTip message below the TextBox , but also want them to be right-aligned.

I managed to place the ToolTip message on the right edge of the text box, so I tried to move the message to the left of the message length.

So, I tried to get the length of the string using TextRenderer.MeasureText (), but the position is a bit discharged, as shown below.

current result

 private void button1_Click(object sender, EventArgs e) { ToolTip myToolTip = new ToolTip(); string test = "This is a test string."; int textWidth = TextRenderer.MeasureText(test, SystemFonts.DefaultFont, textBox1.Size, TextFormatFlags.LeftAndRightPadding).Width; int toolTipTextPosition_X = textBox1.Size.Width - textWidth; myToolTip.Show(test, textBox1, toolTipTextPosition_X, textBox1.Size.Height); } 

I tried using different flags in the MeasureText () function, but that did not help, and since the ToolTip message is indented, I went for TextFormatFlags.LeftAndRightPadding.

To be clear, this is what I would like to achieve:

desired result

+5
source share
2 answers

You can set the OwnerDraw property of the ToolTip to true. You can then control the appearance and location of the tooltip in the Draw event. In the following example, I found a tooltip handle and moved it to the desired location using the MoveWindow function of the Windows API:

 [System.Runtime.InteropServices.DllImport("User32.dll")] static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw); private void toolTip1_Draw(object sender, DrawToolTipEventArgs e) { e.DrawBackground(); e.DrawBorder(); e.DrawText(); var t = (ToolTip)sender; var h = t.GetType().GetProperty("Handle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var handle = (IntPtr)h.GetValue(t); var c = e.AssociatedControl; var location = c.Parent.PointToScreen(new Point(c.Right - e.Bounds.Width, c.Bottom)); MoveWindow(handle, location.X, location.Y, e.Bounds.Width, e.Bounds.Height, false); } 

enter image description here

+5
source

The ToolTip font is larger than SystemFonts.DefaultFont, so the measurement is incorrect. I don't know what the exact variable for the ToolTip font is, but many other SystemFonts are set to Segoe UI / size 9, which is the Tooltip font on my PC. In addition, you must add 6px to fill.

 private void button1_Click(object sender, EventArgs e) { ToolTip myToolTip = new ToolTip(); string test = "This is a test string."; int textWidth = TextRenderer.MeasureText(test, SystemFonts.CaptionFont, textBox1.Size, TextFormatFlags.LeftAndRightPadding).Width; textWidth += 6; int toolTipTextPosition_X = textBox1.Size.Width - textWidth; myToolTip.Show(test, textBox1, toolTipTextPosition_X, textBox1.Size.Height); } 

For perfect control, you can draw a tooltip yourself using Tooltip.OwnerDraw and the Tooltip.Draw event, choosing a font, padding and appearance.

+1
source

Source: https://habr.com/ru/post/1261839/


All Articles