<i id='WfooK'><tr id='WfooK'><dt id='WfooK'><q id='WfooK'><span id='WfooK'><b id='WfooK'><form id='WfooK'><ins id='WfooK'></ins><ul id='WfooK'></ul><sub id='WfooK'></sub></form><legend id='WfooK'></legend><bdo id='WfooK'><pre id='WfooK'><center id='WfooK'></center></pre></bdo></b><th id='WfooK'></th></span></q></dt></tr></i><div id='WfooK'><tfoot id='WfooK'></tfoot><dl id='WfooK'><fieldset id='WfooK'></fieldset></dl></div>
    1. <small id='WfooK'></small><noframes id='WfooK'>

      <tfoot id='WfooK'></tfoot>

      • <bdo id='WfooK'></bdo><ul id='WfooK'></ul>

    2. <legend id='WfooK'><style id='WfooK'><dir id='WfooK'><q id='WfooK'></q></dir></style></legend>

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

      时间:2023-06-09

        <small id='zGjlk'></small><noframes id='zGjlk'>

          <bdo id='zGjlk'></bdo><ul id='zGjlk'></ul>
          <legend id='zGjlk'><style id='zGjlk'><dir id='zGjlk'><q id='zGjlk'></q></dir></style></legend>

              <tbody id='zGjlk'></tbody>
            1. <i id='zGjlk'><tr id='zGjlk'><dt id='zGjlk'><q id='zGjlk'><span id='zGjlk'><b id='zGjlk'><form id='zGjlk'><ins id='zGjlk'></ins><ul id='zGjlk'></ul><sub id='zGjlk'></sub></form><legend id='zGjlk'></legend><bdo id='zGjlk'><pre id='zGjlk'><center id='zGjlk'></center></pre></bdo></b><th id='zGjlk'></th></span></q></dt></tr></i><div id='zGjlk'><tfoot id='zGjlk'></tfoot><dl id='zGjlk'><fieldset id='zGjlk'></fieldset></dl></div>
              <tfoot id='zGjlk'></tfoot>
                本文介绍了C#:使用 TextBox.WordWrap 显示长 Base64 字符串的多行文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个文本框来显示一个很长的 Base64 字符串.TextBox.Multline = trueTextBox.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 字符串的多行文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:WinForms:是否有将标签与文本框相关联的概念? 下一篇:在 WPF 中运行时调整文本框的大小

                相关文章

              • <tfoot id='zjr8t'></tfoot>
              • <i id='zjr8t'><tr id='zjr8t'><dt id='zjr8t'><q id='zjr8t'><span id='zjr8t'><b id='zjr8t'><form id='zjr8t'><ins id='zjr8t'></ins><ul id='zjr8t'></ul><sub id='zjr8t'></sub></form><legend id='zjr8t'></legend><bdo id='zjr8t'><pre id='zjr8t'><center id='zjr8t'></center></pre></bdo></b><th id='zjr8t'></th></span></q></dt></tr></i><div id='zjr8t'><tfoot id='zjr8t'></tfoot><dl id='zjr8t'><fieldset id='zjr8t'></fieldset></dl></div>

                1. <legend id='zjr8t'><style id='zjr8t'><dir id='zjr8t'><q id='zjr8t'></q></dir></style></legend>

                  <small id='zjr8t'></small><noframes id='zjr8t'>

                    • <bdo id='zjr8t'></bdo><ul id='zjr8t'></ul>