Wrapping Text in a rich textbox, but not word wrapping it

后端 未结 4 1246
醉梦人生
醉梦人生 2021-01-13 07:06

I have a Windows Form with a Rich Textbox control on the form. What I want to do is make it so that each line only accepts 32 characters of text. After 32 characters, I want

4条回答
  •  醉梦人生
    2021-01-13 07:23

    I've been looking into this problem myself and have found a really easy way to get around this. You need to place a small piece of code in the Key_Down Event on your TextBox or RichTextBox control.

    Ensure that both Word Wrap and Multiline properties are still set to true, then, you just need to check for the Key Press of KeyCode.Return & Keycode.Enter.

    Like Below :-

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

    Setting the handled value you here to true, sends back the message that we have dealt with the Key Press event, and nothing happens. The Text Control continues to use Word Wrap and blocks the carriage return.

    Hope this helps.

提交回复
热议问题