Thread: [C#] Validation class for events?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    101

    Question [C#] Validation class for events?

    Hi,

    I develop an application that have 20 windows forms with many TextBoxes.
    All TextBoxes have the same validation...
    I write in all classes the same validation like that:



    Code:
    		
    		//if the user press a key down at txtDisplacement we check which one was pressed down
    		#region KeyDownInTxtDisplacement code
    		private void KeyDownInTxtDisplacement(object sender, System.Windows.Forms.KeyEventArgs e)
    		{
    			CheckWhichKeyDown(e);
    		}
    		#endregion
    
    
    
    		//if the user press a key down at txtModel we check which one was pressed down
    		#region KeyDownInTxtModel code
    		private void KeyDownInTxtModel(object sender, System.Windows.Forms.KeyEventArgs e)
    		{
    			CheckWhichKeyDown(e);
    		}
    		#endregion
    
    
    
    		//we check which key is pressed
    		//if key Enter or Return or Tab is pressed
    		//we go throw DoWhenOneTextBoxIsMarkedRed()
    		#region CheckWhichKeyDown code
    		private void CheckWhichKeyDown(System.Windows.Forms.KeyEventArgs e)
    		{
    			if(e.KeyCode==Keys.Enter || e.KeyCode==Keys.Return || e.KeyCode==Keys.Tab)
    			{
    				ValidateThis();
    			}		
    		}
    		#endregion
    
    
    
    		//when special key is press we enable the button
    		#region ValidateThis code
    		private void ValidateThis()
    		{
    			cmdAddToDataBase.Enabled = true;
    		}
    		#endregion



    CheckWhichKeyDown and ValidateThis should be in one validation class.
    How I can write a validation class that validate ALL TextBoxes from all classes.
    Any ideas?
    Or do you know some sample validation classes available on www?


    gicio

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you derive your own textbox and include all your validating there, you should be able to have all your textboxes have the same validation mechanism without extra code.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    101

    Exclamation

    Hi,

    I write a new class... like this:

    Code:
    using System;
    using System.Windows.Forms;
    using System.Windows.Forms.ComponentModel;
    
    namespace CarTrade
    {
    	/// <summary>
    	/// Summary description for TestTextBox.
    	/// </summary>
    	public class TestTextBox : System.Windows.Forms.TextBox
    	{
    		private bool booOnlyNumericCharsAllowed;
    		private bool booSpecialKeyIsPress;
    		
    		public TestTextBox.
    		{
    	
    		}
    
    		//---------------------------------------------------------------
    		//---------------------------PROPERTIES--------------------------
    		//---------------------------------------------------------------
    
    		//we allowed in the TestTextBox only numeric chars
    		//when the user set the proparty OnlyOnlyNumericCharsAllowed
    		//to true
    		internal bool OnlyNumericCharsAllowed
    		{
    			get
    			{
    				//return the value how OnlyOnlyNumericCharsAllowed
    				//is set
    				return booOnlyNumericCharsAllowed;
    			}
    			set
    			{
    				if(value)
    				{
    					this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.DoWhenKeyPress);
    				}
    
    				booOnlyNumericCharsAllowed = value;
    			}
    		}
    
    		internal bool SpecialKeyIsPress
    		{
    			get
    			{
    				return booSpecialKeyIsPress;
    			}
    			set
    			{
    				if(value)
    				{
    					this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CheckIfSpecialKeysArePressed);
    				}
    
    				booSpecialKeyIsPress = value;
    			}
    		}
    
    		private void CheckIfSpecialKeysArePressed(object sender, System.Windows.Forms.KeyEventArgs e)
    		{
    			bool booEnter = e.KeyCode == Keys.Enter;
    			bool booReturn = e.KeyCode==Keys.Return;
    			bool booTab = e.KeyCode==Keys.Tab;
    
    			if(booEnter || booReturn || booTab)
    			{
    		
    			}
    		}
    
    		private void DoWhenKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    		{
    			if((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 )
    			{
    				e.Handled = true;
    			}
    
    			if(Text.Length == 0 && e.KeyChar == 48)
    			{
    				e.Handled = true;
    			}
    		}
    
    	}
    }


    Any coments are welcome!


    gicio

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you want to use this in all your applications, you might consider making your internal properties public instead, so you can set them from the form they are on.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    101
    nvoigt,

    Yes I know. It's just a sample. I already write the final version of my customer TextBox.



    gicio

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM