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,
Murali.K
Posted
Comments
   
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.
—SA

I tried like this it's working fine.

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



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


Hope, this helps.

Thanks,
Abhay Bansal
   
v2
Hi,

you can do that using on key press event

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx[^]

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