Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Application
Hi,
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 23:13pm

1 solution

Rate this: bad
good
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!
Smile | :)
  Permalink  
v3
Comments
indrajeet jadhav at 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 at 27-Aug-12 6:20am
   
nice effort, :)
'txtCategoryName.Focus();' why u wrote this line?
indrajeet jadhav at 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 at 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 at 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 at 27-Aug-12 7:41am
   
have you used my code or yours?
indrajeet jadhav at 27-Aug-12 8:20am
   
My
aarti meswania at 27-Aug-12 8:22am
   
ok once, try my solution and do not write line for focus control itself
indrajeet jadhav at 27-Aug-12 8:21am
   
My code
indrajeet jadhav at 28-Aug-12 1:23am
   
I tried ur code its working k bt it als nt allowed space after string
aarti meswania at 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 at 28-Aug-12 8:28am
   
aarti meswania -its nt working ;(
aarti meswania at 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 at 28-Aug-12 9:00am
   
I mean between two words space is allowed bt nt at the start
aarti meswania at 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 at 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);
 
else
{
e.Handled = false;
e.Handled = (char.IsNumber(e.KeyChar));//Isnumber not alws no
txtRoleName.Focus();
}
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 at 29-Aug-12 6:07am
   
good, well done!
:)
indrajeet jadhav at 29-Aug-12 6:16am
   
aarti meswania..thnxx fr ur help
aarti meswania at 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
0 OriginalGriff 240
1 Kamal Rocks 184
2 BillWoodruff 173
3 PIEBALDconsult 160
4 CPallini 155
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 28 Aug 2012
Copyright © CodeProject, 1999-2014
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