Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WinForm
for (int p = 1; p < txtuname.Text.Length; p++)
               {
                   string ch =txtuname.Text.Substring(p, 1);
                   int ch1 = Convert.ToInt32(ch);
                 
                   if (ch1 <= 0 || ch1 >= 8)
                   {
                       flag = false;
                       break;
 
                   }
 

               }
               if (flag == false)
               {
                   MessageBox.Show("invalid character");
                   txtuname.Focus();
               }
Posted 5-Mar-13 23:45pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Smiley,. Try the following:
int ch1 = 0;
int.TryParse(txtuname.ToString(), out ch1);
  Permalink  
Comments
lakshmi smiley at 6-Mar-13 5:21am
   
my requirement is allow numbers from 0 to 9 entered in textbox but not characters.
if user enters character should show message "invalid character".
willington.d at 6-Mar-13 6:13am
   
Hi Smiley
 
use the following in Textbox keypress event
 
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The most obvious cause is that you are extracting the wrong sub-string, or the user has made an entry error.
 
Try using int.TryParse instead:
int ch1;
if (!int.TryParse(ch, out ch1))
    {
    MessageBox.Show(string.Format("Unable to convert \"{0}\" to a number: please check your input and try again.\n\"{1}\"", ch, txtuname.Text));
    }
  Permalink  

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

  Print Answers RSS
0 Mathew Soji 330
1 BillWoodruff 260
2 Sergey Alexandrovich Kryukov 240
3 OriginalGriff 216
4 Afzaal Ahmad Zeeshan 208
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 6 Mar 2013
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