Click here to Skip to main content
15,861,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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

Hi Smiley,. Try the following:
C#
int ch1 = 0;
int.TryParse(txtuname.ToString(), out ch1);
 
Share this answer
 
Comments
laxmi smiley 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 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;
}
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:
C#
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));
    }
 
Share this answer
 

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