سؤال

When I have a string like QgMAAB+LCAAAAAAABAB1UtFuwiAU/RXDsw9QrO36tmi2ly3L4mPTmGulSqTQwGWJMf77wBlXExpe4NxzOedcuBCJot8IdKSa1Rci3bsyO1Bvxn7CEMAOlBNzAs6ZVgKK/ in a textbox, it automatically displays a line break before each plus (+) sign:

Can I use auto line breaks (WordWrap) and not let the plus sign make it enter a new line?

I want it to look like this:

(I removed all the plus signs for demonstration)

هل كانت مفيدة؟

المحلول

You can actually override the word break function of the textbox by subclassing your own textbox.

This worked for me:

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

This is an modification of the excellent EditWordBreakProc example found here:

Underscore acts as a separator C# RTF Box

نصائح أخرى

This Textbox functionality seems to be by design. Use RichTextBox instead. It does not break on the + sign.

You can set WordWrap = false.

Or you can use RichTextBox instead of TextBox P/S: you can use RichTextBox instead of TextBox ( But you have to set WordWrap = false for RichTextBox too)

Well actually the text format is weird in our case. If you add:

 TextAlignment="Justify"

that would solve your problem with '+' but you will still have a break line after \ because he waits a \n or \t or \r.

It could be a little weird but you can solve the problem of the \ using

String.replace(String, String)

or something similar. You can even try to code your own function. You can check your string and if it occours a " \ ", you'll replace it with " \\ ". I think it should do the trick.

I'm sorry but I don't have enough reputation to add a comment under the fisherXO's answer.

This worked for me:

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

    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top