473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

TextBox Validation In Windows Forms

17
Hallo

I need some help with textboxes, I have a textbox that I need to test if it contains numbers only, I know I can use a masked textbox but it just do’s not fit my use, I have some of the code I need which you can see below here, the part I need is in the second line.

Language: C#
Platform: WinXP
Programming software: VS2005
Type: Windows Forms

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  if (textBoxAddBook_Year.Text.Length == 4)
  4.    if (textBoxAddBook_Year.Text ==  Missing!!!)    
  5.      if (Convert.ToInt32(textBoxAddBook_Year.Text) >= 1600)
  6.        if (Convert.ToInt32(textBoxAddBook_Year.Text) <= DateTime.Now.Year)
  7.           { varYearOK = true; }
  8.  
  9.  

As you can see I need to check if the text in the textbox is numbers only before I check if the year is correct.

NormannTheDane
Nov 2 '07 #1
4 3326
mzmishra
390 Expert 256MB
There is a article on this.You may got some idea from that
http://www.codeproject.com/useritems/Usertextboxcontrol.asp
Nov 2 '07 #2
SammyB
807 Expert 512MB
Add an ErrorProvider control to your form and use the Validated event:
Expand|Select|Wrap|Line Numbers
  1. private void textBox1_Validated(object sender, EventArgs e)
  2. {
  3.     string msg = "";
  4.     int iYear;
  5.     if (textBox1.Text == "")
  6.         msg = "Must be completed.";
  7.     else if (textBox1.Text.Length != 4)
  8.         msg = "Use 4 digit date.";
  9.     else if (!int.TryParse(textBox1.Text, out iYear))
  10.         msg = "Invalid year.";
  11.     else if ((iYear < 1600) || (iYear > DateTime.Now.Year))
  12.         msg = "Year must be greater than 1600";
  13.     errorProvider1.SetError(textBox1, msg);
  14. }
  15.  
See the ErrorProvider help, http://msdn2.microsoft.com/en-us/lib...er(VS.80).aspx for a longer example.
Nov 2 '07 #3
phvfl
173 Expert 100+
Hi Normann,

You can accomplish the first two if conditions using one regular expression. You will need to add a Using statement to include System.Text.RegularExpressions and then the following will define a regular expression:
Expand|Select|Wrap|Line Numbers
  1. Regex reg = new Regex("^[0-9]{4}$");
  2.  
The content of the square brackets is the characters to match (0-9 is all numbers).

The number in the curly brackets is the number of characters to match so this would match with 4 characters.

The "^" matches with the start of the string and the "$" with the end of the string, these are used so that a string of 5 or more numbers is not matched with.

You can then test for the match using the IsMatch member of the Regex object, the return is True if a match is found so your code could be written as:
Expand|Select|Wrap|Line Numbers
  1.   if (reg.IsMatch(textBoxAddBook_Year.Text)){
  2.     Int32 intYr = Convert.ToInt32(textBoxAddBook_Year.Text);
  3.       if (intYr>=1600 && intYr <= DateTime.Now.Year){
  4.       varYearOK = true; 
  5.     }
  6.   } 
  7.  
I have also combined the next two if conditions as it looks tidier to read and only one conversion to an integer needs to be made.
Nov 2 '07 #4
Normann
17
Ok ask a question, and not only get an answer to the question but also get a solution to three other problems, now that is what I like.

Thank you all for your help.

NormannTheDane
Nov 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Gidi | last post by:
Hi, For the last week, i'm looking for a way to make a TextBox always write in English (No matter what the OS default language is). i asked here few times but the answers i got didn't help me. i...
1
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Can any one point me in direction of modify a TextBox control. What i want to do is to add a button, like the ComboBox have. with my own bitmap and execute an Event when clicked. I...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
3
by: Brad Rogers | last post by:
All, Being immersed in vb.net and trying CSharp after almost a year I forgot the differences. I like vb fixing the uppercase/lowercase names and seeming to be more flexible to code entry. ...
14
by: teddysnips | last post by:
WINDOWS FORMS I've a form that has a textbox that allows the user to enter a string. On the LostFocus event, the textbox formats the string into a preferred format. However, if the user...
5
by: ameen.abdullah | last post by:
Hi Guys, I have a textbox in windows form that should only accept alphabets, numbers, spaces and underscore. If the textbox contains anyother character it should display a msg at the time of...
8
by: Filipe Marcelino | last post by:
Hi, I'm trying to create a textbox inheriting from the standard textbox. I would like to: 1. repaint the textbox border; 2. define a color for that border; Till now I made this:
4
by: HenrikL | last post by:
Hi. I have a textbox that have a binding on it with converter and validationrules. When a validation error ouccur the foreground of the textbox will change to red cause it has a style.triggers...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.