How to remove annoying BEEP with RichTextBox

前端 未结 4 1642
星月不相逢
星月不相逢 2021-01-13 19:28

I placed a RichTextBox control on a new form and launched the project. So the RichTextBox.Text = \"\";

Each time I press Up or Down keys I heard the annoying BEEP so

4条回答
  •  被撕碎了的回忆
    2021-01-13 20:14

    This code below should stop the beeping sound, and works with wrapped and unwrapped text:

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (
            richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart) == 0 && e.KeyData == Keys.Up ||
            richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart) == richTextBox1.GetLineFromCharIndex(richTextBox1.TextLength) && e.KeyData == Keys.Down ||
            richTextBox1.SelectionStart == richTextBox1.TextLength && e.KeyData == Keys.Right ||
            richTextBox1.SelectionStart == 0 && e.KeyData == Keys.Left
        ) e.Handled = true;
    }
    

提交回复
热议问题