C#:多行文本框与TextBox.WordWrap显示长字符串的Base64 [英] C#: Multiline TextBox with TextBox.WordWrap Displaying Long Base64 String

查看:861
本文介绍了C#:多行文本框与TextBox.WordWrap显示长字符串的Base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新年快乐!

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

I have a textbox to display a very long Base64 string. The TextBox.Multline = true and TextBox.WordWrap = true.

该问题是由文本框本身的自动字边界检测引起的。基于64位字符串有'+'的64个字符的Base64编码之一。因此,文本框会包起来的+字,这是不是我想要的(因为使用可能会觉得周围有'+'字符换行符)。

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).

我只是想显示在文本框Mulitline模式的Base64我的字符串,但无字边界检测,也就是说,如果 TextBox.Width 只能包含80个字符,然后每行应该有确切的80个字符,除了最后一行。

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.

我不知道我是否说​​清楚了。

I'm not sure if I made myself clear.

感谢。

彼得

推荐答案

在太聪明你的目的智能包装。只要保持,关闭自动换行和包装自己的文字:

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

更新:

这可使用固定宽度字体适合在文本框字符数的估计是:

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

一个可变宽度字体是很难但可以用 MeasureCharacterRanges 来完成。

A variable-width font is harder but can be done with MeasureCharacterRanges.

这篇关于C#:多行文本框与TextBox.WordWrap显示长字符串的Base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆