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 4609772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:02:11+00:00 2026-05-22T01:02:11+00:00

I have a textbox on a form where I’m trying to detect the keys

  • 0

I have a textbox on a form where I’m trying to detect the keys the user types in. The TextBox is multilined with wordwrap on. I don’t want the user the press the enter key (as I want all text entered on ONE line, wrapped) so I used the following code:

private void txtPlain_KeyPress(object sender, KeyPressEventArgs e) {
    if (e.KeyChar == (char)13) {
        MessageBox.Show("Enter keys are not allowed");
        e.KeyChar = (char)0;
    }
}

This worked fine in my tests, but when I tested for CTRL+ENTER it didn’t work as I’m not sure how to detect for the control key. From my googling I found that I need to use the KeyUp/Down events so I now have the following Code:

private void txtPlain_KeyUp(object sender, KeyEventArgs e) {
    //if (e.KeyData == (Keys.Control | Keys.Enter)) {

    if (e.KeyCode == Keys.Enter || (e.KeyCode == Keys.Enter && e.Control)) {            
        MessageBox.Show("Enter keys are not allowed:");
        //e.KeyValue = Keys.None;
    }
}

The first commented out line didn’t work for some reason so if anyone could explain why this would be useful.

The problem with the KeyUp/Down event is that I don’t know how to REMOVE the enter key from the text – unlike the KeyPress event when I can set the KeyChar to zero. The event captures both the Enter and Ctrl+Enter keys, but the cursor still goes to the next line in the TextBox.

Thanks for any help on this.

  • 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-05-22T01:02:12+00:00Added an answer on May 22, 2026 at 1:02 am

    Hmm, there’s no reason to disallow the Enter key by handling the KeyDown or KeyUp events. You can simply set the AcceptsReturn property of the textbox control to False. This will prevent a multiline textbox from responding to a press of the Enter key.

    Of course, this doesn’t solve the problem of Ctrl+Enter. In fact, that’s the expected way to create a new line when the AcceptsReturn property is set to False. To solve that, you will need to handle one of the keyboard events and prevent the control from receiving this input.

    KeyDown is a good place to start. What you want to do is filter out any keyboard events that include the Keys.Enter flag. That will catch them no matter which other modifier key they might be combined with. Then, once you’ve found an Enter keypress, you want to set the e.Handled property to True in order to prevent it from being passed on to the control.

    But unfortunately, we’re not quite done yet. The textbox control tries to handle certain keys internally, and you’re not going to be able to override that in a key event handler method. You also need to tell the control not to interpret that particular key as an input key. There are two primary ways of doing this. The first (and recommended way) is to inherit from the base TextBox class to create your own custom control, and then override the protected IsInputKey method. The second (somewhat simpler) way is just to handle the PreviewKeyDown event, and set the IsInputKey property to False.

    Sample code:

    private void txtPlain_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        // Check if the KeyCode value has the Keys.Enter flag set
        if ((e.KeyCode & Keys.Enter) == Keys.Enter)
        {
            // Set the IsInputKey property to False
            e.IsInputKey = false;
        }
    }
    
    private void txtPlain_KeyDown(object sender, KeyEventArgs e)
    {
        // Check if the KeyCode value has the Keys.Enter flag set
        if ((e.KeyCode & Keys.Enter) == Keys.Enter)
        {            
            // Show the user a message
            MessageBox.Show("Enter keys are not allowed in this textbox.");
    
            // Prevent the key event from being passed on to the control
            e.Handled = true;
        }
    }
    

    And, though I assume this is for testing purposes only, you definitely want to take that MessageBox call out of there for production code. Find another way to alert the user that their input was not allowed, such as a short beep sound and an ErrorProvider component placed next to the textbox. Showing a message box is very jarring, and not very user-friendly. See my answer here for other hints and tips.

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

Sidebar

Related Questions

I have a web form with textbox and button. I want after ENTER key
I have a web form where I have a textbox in which the user
C# question: I have a textBox in a default form Form1 and I want
i have a textbox on a form and i would like the user to
I have a textbox on dialog form, I want to default focus on it
I have a textbox in my form. On the form load event, i want
I am creating a user form. In this I have textbox and a lot
I have a textbox in a web form where an admin user can add
I have a TextBox control on my Form. I use the Leave event on
I have got a textbox on a form with a method being called from

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.