Wrap text in a rich text box but not word wrap

I have a windows form with a Rich Textbox element in the form. I want to make every line accept only 32 characters of text. After 32 characters, I want the text to go to the next line (I do not want to insert carriages). The WordWrap property almost does this, except that it moves all the text entered, down to the last place in the text, and moves it all. I just want to gently wrap the text right after 32 characters. How can i do this? I am using C #.

+5
source share
4 answers

Well, I found a way to do this (after many searches and links to the Windows API), and I post the solution here if someone else needs to find out. There is no pure .NET solution for this, but, fortunately, the Windows API allows you to override the default procedure that is called during word wrap processing. First you need to import the following DLL:

[DllImport("user32.dll")]

    extern static IntPtr SendMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam);

Then you need to define this constant:

 const uint EM_SETWORDBREAKPROC = 0x00D0;

Then create a delegate and event:

    delegate int EditWordBreakProc(IntPtr text, int pos_in_text, int bCharSet, int action);

    event EditWordBreakProc myCallBackEvent;

Then create our new function for word wrap processing (which in this case, I don’t want it to do anything):

     private int myCallBack(IntPtr text, int pos_in_text, int bCharSet, int action)

    {

        return 0;

    }

Finally, in the Shown event of your form:

        myCallBackEvent = new EditWordBreakProc(myCallBack);

        IntPtr ptr_func = Marshal.GetFunctionPointerForDelegate(myCallBackEvent);

        SendMessage(txtDataEntry.Handle, EM_SETWORDBREAKPROC, IntPtr.Zero, ptr_func);
+4
source

() , MeasureString, 32 , . Thats 32 .

+2

. Key_Down TextBox RichTextBox.

, Word Wrap Multiline - true, KeyCode.Return Keycode.Enter.

: -

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
            {
                e.Handled = true;
            }

        }

true, , Key Press, . Word Wrap .

, .

0

, - event, .

"", : , , , . , , , , , , . TextBox .

, :

public partial class Form1 : Form
{
    [System.Runtime.InteropServices.DllImport("user32.dll")]

    extern static IntPtr SendMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam);

    private const uint EM_SETWORDBREAKPROC = 0x00D0;

    private delegate int SetWordBreakProc(IntPtr text, int pos_in_text, int bCharSet, int action);
    private readonly SetWordBreakProc _wordBreakCallback = (text, pos_in_text, bCharSet, action) => 0;

    public Form1()
    {
        InitializeComponent();
        textBox1.HandleCreated += TextBox1_HandleCreated;
    }

    private void TextBox1_HandleCreated(object sender, EventArgs e)
    {
        IntPtr callback = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(_wordBreakCallback);

        SendMessage(((Control)sender).Handle, EM_SETWORDBREAKPROC, IntPtr.Zero, callback);
    }
}

, TextBox.HandleCreated TextBox.HandleCreated. : -, , , TextBox.Text , , -, , TextBox.Text , , Designer. , .

, , , , , , . , , .

Also note that the above requires explicit initialization of each TextBoxyou added to the form. Obviously, if you want to apply this technique to more than one TextBox, it makes sense to create a new subclass TextBoxthat performs this initialization in its own OnHandleCreated()override OnHandleCreated().

0
source

All Articles