问题描述
我有一个文本框来显示一个很长的 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
.
这个问题是由 TextBox 本身的自动词边界检测引起的.Base64 字符串将+"作为 Base64 编码的 64 个字符之一.因此,TextBox 会将其包裹在 '+' 字符处,这不是我想要的(因为用户可能会认为 '+' 字符周围有换行符).
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).
我只希望我的Base64字符串在TextBox中以Mulitline-mode显示,但没有字边界检测,即如果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.
推荐答案
Smart wrap in 太聪明了,不适合你的目的.只保留Multiline
,关闭WordWrap
,自己换行:
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;
}
更新:
使用固定宽度字体的 TextBox
中可以容纳的字符数估计为:
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 字符串的多行文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!