简体   繁体   中英

How to break line for a specific character in a richtextbox in c#

当我们在richtextbox中键入时,如果单词尚未完成但该行已完成,则整个单词将转到下一行。所以有人可以告诉我如何对特殊字符进行操作。通常在空格字符处发生,但是我想要制作另一个字符(Ascii一个)。

Check in the KeyPress event if the line-break character of your choice was pressed:

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '-')
    {
        int curserPosition = richTextBox1.SelectionStart;
        richTextBox1.Text += "-\r\n";
        richTextBox1.SelectionStart = curserPosition + 2;
        e.Handled = true;
    }
}

If pasting strings into the RichTextBox you will need to parse the strings and replace the character with the character plus carriage return and linebreak characters:

string rtbText = string.Empty;
rtbText.Replace("-", "-\r\n");

If you want to ONLY break when the current line is full you will have to put in some more logic along the lines of:

if (richTextBox1.Lines[richTextBox1.Lines.Length - 1].Length > 100)
{
    // parse the string for the last occurance of "-" and insert a CRLF behind it
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM