Click here to Skip to main content
14,739,403 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All,

I need to restrict "Space" button for text box. Means don't want to give enter "Space" button for text box. I am using windows application with c#.

Thanks and Regards,
This behavior strongly depends on the exact type of "TextBox". There are different types under the same name, so you need to give us full name of it. Generally, you always need to tag the UI library you use, when the question is about UI. Please, exact type.

I tried like this it's working fine.

if (txtDeathFirstName.Text.Length == 0 && e.KeyChar == ' ')
               e.Handled = true;
               e.Handled = false;
You could just add this to the class

protected override void OnPreviewKeyDown(KeyEventArgs e)
  e.Handled = e.Key == Key.Space;

Hope, this helps.

Abhay Bansal

you can do that using on key press event[^]

Check char code and omit it :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900