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

前端 未结 1 1384
我寻月下人不归
我寻月下人不归 2020-12-19 03:37

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

相关标签:
1条回答
  • 2020-12-19 04:00

    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.

    0 讨论(0)
提交回复
热议问题