Click here to Skip to main content
13,000,469 members (58,257 online)
Rate this:
Please Sign up or sign in to vote.
In Text box at the first whitespace is not allowed and string should be separated by space and number is not allowed .
Posted 26-Aug-12 22:13pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In textbox's keypress event put this condition,
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...
public static Boolean IsNumeric(string stringToTest)
    int result;
    return int.TryParse(stringToTest, out result);

Happy Coding!
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);

e.Handled = false;
e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
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 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..
aarti meswania 27-Aug-12 7:41am
have you used my code or yours?
indrajeet jadhav 27-Aug-12 8:20am
aarti meswania 27-Aug-12 8:22am
ok once, try my solution and do not write line for focus control itself
indrajeet jadhav 27-Aug-12 8:21am
My code
indrajeet jadhav 28-Aug-12 1:23am
I tried ur code its working k bt it als nt allowed space after string
aarti meswania 28-Aug-12 1:35am
c.Text.Length==0 causes to not allow space when text length is 0 else it will allow
c=your textbox (sender)
indrajeet jadhav 28-Aug-12 8:28am
aarti meswania -its nt working ;(
aarti meswania 28-Aug-12 8:40am
you mean to say it's not allow whitespace in between string?
I think some other events are conflicting
you can just check it out
take fresh textbox just declare it's keypress event & copy-paste code,
you will get desired output
indrajeet jadhav 28-Aug-12 9:00am
I mean between two words space is allowed bt nt at the start
aarti meswania 28-Aug-12 9:01am
yes the code will work for same condition
'between two words space is allowed bt nt at the start'
indrajeet jadhav 29-Aug-12 6:04am
bool temp = Utility.IsAlphaNumericWithSpace(e);
//if (temp == false)
// e.KeyChar = '\0';
if ((sender as TextBox).SelectionStart == 0)
e.Handled = (e.KeyChar == (char)Keys.Space);

e.Handled = false;
e.Handled = (char.IsNumber(e.KeyChar));//Isnumber not alws no
IsNumber() not allowed number
perfectly wrkng code..satisfies conditions like
1.First space is not allowd
2.Space betn strings
3.Number is not allowed
aarti meswania 29-Aug-12 6:07am
good, well done!
indrajeet jadhav 29-Aug-12 6:16am
aarti meswania..thnxx fr ur help
aarti meswania 29-Aug-12 6:18am
welcome :)

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

Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 28 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100