I have a form with multiple TextBox controls. Some have multiline set to true others to no. When using large fonts it becomes obvious that the text positioning is different. When multiline is true some kind of additional margin appears around the text.
The following code fragment
TextBox textBox = new TextBox();
textBox.Text = "Test";
textBox.Font = new Font("Segoe UI", 16);
Console.WriteLine("Single Line:" + textBox.GetPositionFromCharIndex(0));
textBox.Multiline = true;
Console.WriteLine("Multi Line:" + textBox.GetPositionFromCharIndex(0));
outputs
Single Line:{X=1,Y=0}
Multi Line:{X=7,Y=1}
My questions are:
Why is this margin added in multiline mode?
Can this be avoided/controlled?
I already found out that the text in multiline mode is drawn with
TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak | TextFormatFlags.NoClipping
while in single line mode
TextFormatFlags.TextBoxControl | TextFormatFlags.NoPadding | TextFormatFlags.SingleLine
is used.
Is there a way to control the TextFormatFlags used by the TextBox?
Thank you for any effort you put on answering my questions!
You will have to create your own class derived from TextBox and handle the Paint event by overriding OnPaint.
In there you will have full control over how the text is rendered for the TextBox using the proper TextFormatFlags as desired.
Cheers