.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

TextBox Validation Control for Windows Application

Posted By:Narayanan       Posted Date: November 27, 2012    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

Today , we will discuss about step by step creation of User control and How to handle in the Windows Application? Validation is an important part of Development as well as Application side. No Validation Control for windows Application.
 

Purpose of this Article:

·         Text Box Validation[Int,Double and String].

·         Using Enumeraion in the User control.

·         Call the User control in the another Project.

Create User Control in C#.Net:

·         Name of the User Control: ValidateTextBox

·         File Extension : .dll.


In Project Section:

·         Open your VS 2008/2005.

·         Select “Class Library” in the VS Template.  

·         Type “Class Library” name in the Name Input Box Eg: ValidateTextBox

·         Click “Ok” Button.

·         Right Click on the “Project Name”.

·         Select Add >>“New Item”

·         Select “Windows Forms” on the Left side of the Window.

·         Select “user Control” on the Right side of the Window.

·         Type “User Control “ Name in the Inpyt Box Eg: ValidateTextBox

·         Click “Add” button.

·         Now User control added in the Project.

·         Drag “Text Box” from Tool Box and Drop into User control.

·         Now , Copy and paste in the Code View Code.



   public enum ValidationType

        {

            None,

            Int,

            Double

        }

        private ValidationType m_align; 

        public ValidationType ValidationTypes

        {

            get { return m_align; }

            set { m_align = value; }

        }

        public void ValidateTextNumber(KeyPressEventArgs e)

        {

            if (e.KeyChar >= 65 && e.KeyChar <= 122 || e.KeyChar == 32)

            {

                e.Handled = true;

            }

            else

            {

                e.Handled = false;

            }

        }

        public bool IsValidNumber(string number)

        {

            int position = number.IndexOf(".");

            if (position != -1)

            {

                int decdigits = number.Substring(position + 1, number.Length - number.IndexOf('.') - 1).Length;

                if (decdigits == 2)

                {

                    return true;

                }

                else

                {

                    return false;

                }

            }

            return false;

        }

      private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (ValidationTypes == ValidationType.Double)

            {

                ValidateTextNumber(e);

            }

            else if (ValidationTypes == ValidationType.Int)

            {

                ValidateTextNumber(e);

            }

        }

        public void ValidateTextDouble(TextBox t)

        {

            bool val = IsValidNumber(t.Text);

            if (!val)

            {

                MessageBox.Show("Invalid number");

                t.Focus();

            }

        }

      public void ValidateTextInt(TextBox t)

      {

          bool val = IsValidNumber(t.Text);

          if (val)

          {

              MessageBox.Show("Invalid number");

              t.Focus();

          }

      }

        private void textBox1_Leave(object sender, EventArgs e)

        {

            if (ValidationTypes == ValidationType.Double)

            {

                ValidateTextDouble(textBox1);

            }

            else if (ValidationTypes == ValidationType.Int)

            {

                ValidateTextInt(textBox1);

            }

        }


Rebuild a Project.

Go To Project Folder (Bin>>Debug) 

File Name:“ValidateTextBox.dll”


Now , You add this DLL in your Project .

How to add?


Go to Add Reference

Click “Browse” button

Select your Path of the DLL .


How to Test it?


Drag “ValidateTextBox” from Toolbar

Drop “ValidateTextBox“ into Form.

Drag “Button” from Toolbar

Drop “Button“ into Form.

Change the Value of “ValidationType” Property in the ValidateTextBox.


If you select “Int” – Only Interger value will type(10).

   you select “Double” – Only Double value will type (like 10.10).

   you select “None” – string value will type (like GOOD0205).



Conclusion:     

    Thank you for reading this Article .and I hope , You enjoy it.



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend