C#:使用 TextBox.WordWrap 显示长 Base64 字符串的多行文本框

发布于 2024-10-10 03:25:48 字数 359 浏览 0 评论 0原文

我有一个文本框来显示很长的 Base64 字符串。 TextBox.Multline = trueTextBox.WordWrap = true

该问题是由 TextBox 本身的自动字边界检测引起的。 Base64 字符串将“+”作为 Base64 编码的 64 个字符之一。因此,文本框会将其包裹在“+”字符处,这不是我想要的(因为用户可能认为“+”字符周围有一个换行符)。

我只想在 TextBox 中以多行模式显示我的 Base64 字符串,但没有字边界检测,也就是说,如果 TextBox.Width 只能包含 80 个字符,那么每行应该有精确的 80 个字符,除了最后一行。

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 auto-word-boundary detection of the TextBox itself. The Base64 string has '+' as one of the 64 characters for Base64 encoding. Therefore, the TextBox will wrap it up at the '+' character, which is not what I want (because the use might think there is a newline character around the '+' character).

I just want my Base64 string displayed in Mulitline-mode in TextBox, but no word boundary detection, that is, if the TextBox.Width can only contain 80 characters, then each line should have exact 80 characters except the last line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩扇题诗 2024-10-17 03:25:48

智能包裹对于您的目的来说太智能了。只需保留Multiline,关闭WordWrap并自行换行即可:

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;
}

更新:

估计中可以容纳的字符数使用固定宽度字体的 >TextBox 是:

public int GetMaxChars(TextBox tb)
{
    using (var g = CreateGraphics())
    {
        return (int)Math.Floor(tb.Width / (g.MeasureString("0123456789", tb.Font).Width / 10));
    }
}

可变宽度字体较难,但可以使用 MeasureCharacterRanges 来完成。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文