Click here to Skip to main content
11,478,174 members (68,882 online)
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 Sergey Alexandrovich Kryukov 229
1 F-ES Sitecore 170
2 Sascha Lefèvre 160
3 Frankie-C 120
4 OriginalGriff 105
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,321
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,325


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 6 Mar 2013
Copyright © CodeProject, 1999-2015
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