Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
In Text box at the first whitespace is not allowed and string should be separated by space and number is not allowed .
Posted

1 solution

In textbox's keypress event put this condition,
C#
if (e.KeyChar == " " && c.Text.Length==0)//for block first whitespace 
  { e.Handled = True;}
if (IsNumeric(e.Keychar))//for block numbers
  { e.Handled = True;}


IsNumeric function...
C#
public static Boolean IsNumeric(string stringToTest)
{
    int result;
    return int.TryParse(stringToTest, out result);
}


Happy Coding!
:)
 
Share this answer
 
v3
Comments
indrajeet jadhav 27-Aug-12 6:12am    
bool temp = Utility.IsAlphaNumericWithSpace(e);
if (temp == false)
e.KeyChar = '\0';
if ((sender as TextBox).SelectionStart == 0)
e.Handled = (e.KeyChar == (char)Keys.Space);

else
e.Handled = false;
e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
txtCategoryName.Focus();
Aarti Meswania 27-Aug-12 6:20am    
nice effort, :)
'txtCategoryName.Focus();' why u wrote this line?
indrajeet jadhav 27-Aug-12 7:01am    
@aarti meswania thnxx for appreciation & help....Focus() This a type of validation method..in case user forget to enter in this field such situation focus automatically comes in that control.
Aarti Meswania 27-Aug-12 7:06am    
ok, but it is keypress event at that time focus will default in that textbox, you should write that code for focus in leave event or if want in this keypress event then you should check if textbox is empty and tab key is pressed then focus itself.
most welcome :)
indrajeet jadhav 27-Aug-12 7:39am    
With in this code i am nt able to split the string e.g ramesh gupta
after entering the string the cursor not allowed space or i press space the cursor transfer goes back to the first position..

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