locked
How can i check if text box contain letters ? RRS feed

  • Question

  • I want to use IF statement so IF textbox1.text contain letters i mean if the user typed letters lets say abcde or adfgfghdfg whatever letters he type so the user will get some messagebox message .

     

    But only letters.

     

    How do i do it ?

     

    Thanks for help .


    danieli
    Wednesday, April 21, 2010 3:04 PM

Answers

All replies

  • Here is the code

     

    int size = textBox1.Text.Length;
                if (size!= 0)
                {
                    Form2 f2 = new Form2();
                    f2.Show();
                }

     


    if the post helpful please mark it right else feel free to correct me
    • Proposed as answer by uahmed Saturday, April 24, 2010 4:49 PM
    Wednesday, April 21, 2010 3:22 PM
  • Seems to be working but how do i make an exception to check if its realy working ?

    Here is the part of my code and what shouldi  change so it will throw me exception here just to check if its realy working the letters thing ?

     

    private void timer1_Tick(object sender, EventArgs e)
            {

                try
                {


                    numbers = Convert.ToInt64(textBox1.Text);


                    if (numbers <= 0)
                    {

                        timer1.Stop();

                        button1.Enabled = true;
                        textBox1.Enabled = true;
                        MessageBox.Show("Value cannot be zero or below zero");
                        textBox1.Text = "";
                    }
                    else
                    {
                        Numbers_Timer = Numbers_Timer + 1;
                        if (Numbers_Timer == numbers)
                        {

                            filesdownload();

                            button1.Enabled = false;
                            Numbers_Timer = 0;

                        }
                        //    String str = string.Format("Hours {0} Minutes {1} Seconds {2}", c, d, e);


                        time_left = numbers - Numbers_Timer;

                        label1.Text = "Time left for next download: " + time_left;

                        //   y = numbers - 1;
                        //  label1.Text = "Time left for next download: " +y;
                        /*  if (a == 1)
                          {
                              a=numbers;
                              button1.Enabled = false;
                          } */
                        //String str = string.Format("Hours {0} Minutes {1} Seconds {2}", c, d, e);
                        //    this.label1.Text = str;
                        /*    String str = string.Format("{0} {1}", c, d);
                            this.textBox1.Text = str;
                            c = c - 1;
                            d = d - 1; */
                    }

                }
                catch (Exception General_Exceptions)
                {
                    timer1.Stop();
                    int size = textBox1.Text.Length;
                    if (size != 0)
                    {
                        MessageBox.Show("Sorry you cannot enter letters into the textbox.");

                    }
                    else
                    {
                        Logger.Write("Exception: " + General_Exceptions);
                    }
                   

                    textBox1.Clear();
                    button1.Enabled = true;
                    textBox1.Enabled = true;

                }

            }


    danieli
    Wednesday, April 21, 2010 3:43 PM
  • I understand till now that u want that if some letters are writeen in text box it show the message box right ?

    so in my code Form2 is your message box

    and the if statement will decide about the presence of letters or not


    if the post helpful please mark it right else feel free to correct me
    Wednesday, April 21, 2010 3:54 PM
  • True its working .

    But now i want to check what happen if there is exception .

    I want to check the else after the if of the letters .

    So i wonder what can i change there for a moment just for testing to see if there is exception it will throw me exception message and if i type letters it will throw the letters message .

     

    JUst for testing i want somehow to make it throw exception .

     

     


    danieli
    Wednesday, April 21, 2010 4:02 PM
  • If you're going to make a check to see if a checkbox only has letters in it you can either:

    1) Iterate through all chars in the textBox.Text and check if they're letters or not (use string.ToCharArray() and Char.IsLetter())

    2) Add an event handler for textBox.KeyPress (I think, if not it's textBox.KeyDown) and just filter the events so only letters will be entered into the textbox (e.Handled = false;)

    Wednesday, April 21, 2010 5:32 PM
  • Hi chocolade,

    Please don't dulplicate thread. You question had been answered here: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/4b3a02dc-5144-4101-b440-23a24d9078a6

    Best Regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, April 26, 2010 3:32 AM
    Thursday, April 22, 2010 9:37 AM