C#: Multiline TextBox with TextBox.WordWrap Displaying Long Base64 String

强颜欢笑 提交于 2019-11-29 09:53:38

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!