I have a textbox to display a very long Base64 string. The TextBox.Multline = true
and TextBox.WordWrap = true
.
The issue is caused by the
Smart wrap in too smart for your purposes. Just keep Multiline
, turn off WordWrap
and wrap the text yourself:
public IEnumerable<string> SimpleWrap(string line, int length)
{
var s = line;
while (s.Length > length)
{
var result = s.Substring(0, length);
s = s.Substring(length);
yield return result;
}
yield return s;
}
Update:
An estimate of the number of characters that can fit in a TextBox
using a fixed-width font is:
public int GetMaxChars(TextBox tb)
{
using (var g = CreateGraphics())
{
return (int)Math.Floor(tb.Width / (g.MeasureString("0123456789", tb.Font).Width / 10));
}
}
A variable-width font is harder but can be done with MeasureCharacterRanges
.