Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

WinForms TextBox to accept numbers in a range

, 10 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
I will use a label with font color red just below the text box and will adopt the following code private void textBox1_TextChanged(object sender, EventArgs e) { int min=0,max=256; try { label1.Visible = false; ...
I will use a label with font color red just below the text box and will adopt the following code
 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            int min=0,max=256;
            try
            {
                label1.Visible = false;
                int n = int.Parse(textBox1.Text);
                if (n > max || n < min)
                {
                    label1.Text="Number can be only in range "+min.ToString()+" and "+max.ToString();
                    label1.Visible = true;
                   // textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
                }
            }
            catch (Exception ex)
            {
                label1.Text = "Not a Number";
                label1.Visible=true;
                //textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
            }
        }
 
If you un-comment the comment lines, it will remove the last character, though label will not be visible(Also there is a small bug, the cursor will go to first position which can be set to last character).

License

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

Share

About the Author

Grasshopper.iics
CEO Integrated Ideas
India India
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basic objective is to keep in touch and be notified while a member contributes an article, to check out with technology and share what we know. We are the "students" of codeproject.
 
This group is managed by Rupam Das, an active author here. Other Notable members include Ranjan who extends his helping hands to invaluable number of authors in their articles and writes some great articles himself.
 
Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated Ideas Consultancy Services, a research consultancy firm in India. He has been part of projects in several technologies including Matlab, C#, Android, OpenCV, Drupal, Omnet++, legacy C, vb, gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C# more than anything else but is still struck in legacy style of coding.
Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medical Engineering over other technologies.
 
He is frustrated with his poor writing and "grammer" skills but happy that coding polishes these frustrations.
Group type: Organisation

99 members

Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 10 Jan 2012
Article Copyright 2012 by Grasshopper.iics
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid