文本框在加号前显示换行符

时间:2014-03-19 14:55:14

标签: c# string winforms textbox line-breaks

当我在文本框中有一个类似QgMAAB+LCAAAAAAABAB1UtFuwiAU/RXDsw9QrO36tmi2ly3L4mPTmGulSqTQwGWJMf77wBlXExpe4NxzOedcuBCJot8IdKSa1Rci3bsyO1Bvxn7CEMAOlBNzAs6ZVgKK/的字符串时,它会在每个加号(+)前自动显示换行符:

我可以使用自动换行符(WordWrap)而不是让加号让它进入新行吗?

我希望它看起来像这样:

(我删除了所有加号以进行演示)

6 个答案:

答案 0 :(得分:2)

您实际上可以通过继承自己的文本框来覆盖文本框的分词功能。

这对我有用:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace myNamespace
{
    class MyTextBox : TextBox
    {
        const int EM_SETWORDBREAKPROC = 0x00D0;
        const int EM_GETWORDBREAKPROC = 0x00D1;

        delegate int EditWordBreakProc(string lpch, int ichCurrent, int cch, int code);

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            if (!this.DesignMode)
            {
                SendMessage(this.Handle, EM_SETWORDBREAKPROC, IntPtr.Zero, Marshal.GetFunctionPointerForDelegate(new EditWordBreakProc(MyEditWordBreakProc)));
            }
        }

        [DllImport("User32.DLL")]
        public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        int MyEditWordBreakProc(string lpch, int ichCurrent, int cch, int code)
        {
            return 0;
        }
    }
}

这是对此处的优秀EditWordBreakProc示例的修改:

Underscore acts as a separator C# RTF Box

答案 1 :(得分:1)

此文本框功能似乎是设计使然。请改用RichTextBox。它不会在+号上打破。

答案 2 :(得分:0)

您可以设置WordWrap = false

或者您可以使用RichTextBox而不是TextBox P / S:您可以使用RichTextBox而不是TextBox(但您也必须为RichTextBox设置WordWrap = false

答案 3 :(得分:0)

实际上,在我们的案例中,文本格式很奇怪。如果你添加:

 TextAlignment="Justify"

可以解决'+'的问题,但在\之后仍然会有一个中断行,因为他等待\n\t\r

答案 4 :(得分:0)

可能有点奇怪,但你可以解决\使用

的问题
String.replace(String, String)

或类似的东西。您甚至可以尝试编写自己的功能。 您可以检查字符串,如果它出现“\”,您将用“\\”替换它。我认为应该这样做。

我很抱歉,但根据fisherXO的回答,我没有足够的声誉来添加评论。

答案 5 :(得分:0)

这对我有用:

using System;
using System.Windows.Forms;

namespace Namespace
{
    class CustomTextBox : TextBox
    {
        string _text;
        public CustomTextBox()
        {
            this.Multiline = true;
            this.Wordwrap = true;
            this.KeyDown += new KeyEventHandler(CustomTextBox_KeyDown);
            this.KeyPress += new KeyPressEventHandler(CustomTextBox_KeyPress);
        }

        void CustomTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '+')
                e.Handled = true;
            else
                _text = _text + e.KeyChar.ToString();
        }

        void CustomTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                e.Handled = true;
            else if (e.KeyCode == Keys.Add)
                _text = _text + @"+";
            this.Text = _text;
        }

    }
}
相关问题