Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:41:58+00:00 2026-06-14T19:41:58+00:00

I have the following code: private void richTextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode

  • 0

I have the following code:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.N)
        {
            richTextBox1.Select(1, 3);
        }
    }

When I press the N key , the selected text is replaced with “n”. I read this Selecting text in RichTexbox in C# deletes the text ,but it had no effects.

I am using Windows Forms.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:41:59+00:00Added an answer on June 14, 2026 at 7:41 pm

    Try it yourself:
    Open up the editor, type some text, mark some of this text and press N. What happens? The marked text is replaced with n.
    The same thing happens in your RichTextBox. Important to understand here is, that with the event you set up, you only add some functionality and leave the default event handling (handled by the OS) intact.

    So with your code, on a key press you just do

    richTextBox1.Select(1, 3);
    

    which selects some characters and afterwards the default event handling kicks in. Thus there is some marked text which gets replaced with N.
    So, you simply have to mark the event as handled by yourself. Not using the Handled-property, but with the SuppressKeyPress-property.

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.N)
        {
            richTextBox1.Select(1, 3);
            e.SuppressKeyPress = true;
        }
    }
    

    The documentation of Handled clearly states:

    If you set Handled to true on a TextBox, that control will
    not pass the key press events to the underlying Win32 text
    box control, but it will still display the characters that the user typed.
    

    Here is the official documentation of SuppressKeyPress.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: private void _DoValidate(object sender, DoWorkEventArgs e) { this.BeginInvoke(new MethodInvoker(()
I have the follwing code (which is not working): private void Window_PreviewKeyDown(object sender, KeyEventArgs
I have following code: private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e) { System.Diagnostics.Process execute =
I have the following code: private void EntryBrowserLoad(object sender, EventArgs e) { var ase
I have the following code: private Point initialpoint; private void ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
I have the following code: SaveFileDialog saveGC1File = new SaveFileDialog(); private void GCSavePlacementOneButton_Click(object sender,
I have the following code: private void form1_closing(object sender, FormClosingEventArgs e) { e.Cancel =
I have following piece of code: private void nameTextBox_Leave(object sender, EventArgs e) { var
I have the following code: private void button1_Click(object sender, EventArgs e) { var answer
I have the following code- private void button1_Click(object sender, EventArgs e) { _soundplayer.Play(); timer1_Tick();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.