Click here to Skip to main content
15,891,513 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
Sergey Alexandrovich Kryukov 4-Jan-13 4:21am    
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.

C#
if (txtDeathFirstName.Text.Length == 0 && e.KeyChar == ' ')
           {
               e.Handled = true;
           }
           else
           {
               e.Handled = false;
           }
 
Share this answer
 
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
 
Share this answer
 
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 :)
 
Share this answer
 

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



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