Results 1 to 5 of 5

Thread: C#: Validation inside a Textbox?

  1. #1
    Join Date
    Jan 2009
    Posts
    18

    C#: Validation inside a Textbox?

    Hi,
    I want to put validation inside a textbox & I need some help regarding the same.

    I am working with C# visual studio 2005.

    Please help me!

  2. #2
    Join Date
    May 2008
    Posts
    115

    Re: C#: Validation inside a Textbox?

    Consider for eg we want to validate for 2 Textbox then we have to write code like this:

    TextBox t1=new TextBox();
    TextBox t2=new TextBox();

    t1+=new CancelEventHandler(Validatingfunction)
    t2+=new CancelEventHandler(Validatingfunction)

    using the Code i sent to you without worrying abt the
    Validation u can simply code it like this:

    NumberBox n1=new NumberBox();
    NumberBox n2=new NumberBox();

    The above 2 will create 2 textbox and has all the features of textbox along with the Number validation in built.

  3. #3
    Join Date
    May 2008
    Posts
    63

    Re: C#: Validation inside a Textbox?

    Please have alook at this code which will help you understand the textbox validation in C# .

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void numberBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int numberEntered = int.Parse(numberBox.Text);
                if (numberEntered < 1 || numberEntered > 10)
                {
                    e.Cancel = true;
                    MessageBox.Show("You have to enter a number between 1 and 10");
                }
            }
            catch (FormatException)
            {
                e.Cancel = true;
                MessageBox.Show("You need to enter an integer");
            }
        }
    
        private void numberBox_Validated(object sender, EventArgs e)
        {
            MessageBox.Show("Well done, you managed to enter a valid number");
        }
    
        private void okButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    
    }
    partial class Form1
    {
        private void InitializeComponent()
        {
            this.numberBox = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.okButton = new System.Windows.Forms.Button();
            this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox();
            this.SuspendLayout();
            // 
            // numberBox
            // 
            this.numberBox.Location = new System.Drawing.Point(253, 15);
            this.numberBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.numberBox.Name = "numberBox";
            this.numberBox.Size = new System.Drawing.Size(57, 22);
            this.numberBox.TabIndex = 0;
            this.numberBox.Validated += new System.EventHandler(this.numberBox_Validated);
            this.numberBox.Validating += new System.ComponentModel.CancelEventHandler(this.numberBox_Validating);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(16, 18);
            this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(201, 16);
            this.label1.TabIndex = 1;
            this.label1.Text = "Enter a number between 1 and 10";
            // 
            // okButton
            // 
            this.okButton.Location = new System.Drawing.Point(335, 11);
            this.okButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.okButton.Name = "okButton";
            this.okButton.Size = new System.Drawing.Size(100, 28);
            this.okButton.TabIndex = 2;
            this.okButton.Text = "OK";
            this.okButton.Click += new System.EventHandler(this.okButton_Click);
            // 
            // maskedTextBox1
            // 
            this.maskedTextBox1.Location = new System.Drawing.Point(0, 0);
            this.maskedTextBox1.Name = "maskedTextBox1";
            this.maskedTextBox1.Size = new System.Drawing.Size(100, 23);
            this.maskedTextBox1.TabIndex = 3;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(453, 246);
            this.Controls.Add(this.maskedTextBox1);
            this.Controls.Add(this.okButton);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.numberBox);
            this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();
    
        }
    
        private System.Windows.Forms.TextBox numberBox;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button okButton;
        private System.Windows.Forms.MaskedTextBox maskedTextBox1;
    }
    public class TextBoxValidation
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: C#: Validation inside a Textbox?

    You need to test the length of the text in the textbox
    Your textbox has a 'Text' attribute, as well as others.
    The Text is of the type 'string' and strings have many attributes, one of wich is Length.
    You need to test that Length is greater than zero.
    So if your Textbox is called TextBox1, in the onclick event of the button you test like so.

    if(TextBox1.Text.Length <= 0)
    {
    //Show the message
    MessageBox.Show("You Messaage");
    }
    else
    {
    //Do whatever else you wanted to do
    }

    I have ot used C# windows for a while so Im not sure syntax is exact, especially the messagebox.show bit.
    But that should be a start.

  5. #5
    Join Date
    May 2008
    Posts
    35

    Re: C#: Validation inside a Textbox?

    Please have alook at this page it will explain you the textbox validation in detail.

Similar Threads

  1. Replies: 2
    Last Post: 20-08-2010, 01:23 AM
  2. ProgressBar In TextBox
    By Level8 in forum Software Development
    Replies: 5
    Last Post: 06-02-2010, 08:55 PM
  3. Textbox validation in ASP.NET
    By Kasper in forum Software Development
    Replies: 4
    Last Post: 19-01-2010, 08:28 PM
  4. Numerical value on TextBox
    By KABIRA16 in forum Software Development
    Replies: 3
    Last Post: 29-10-2009, 04:25 PM
  5. How to recover a value in a textbox
    By S_Asnodkar in forum Software Development
    Replies: 4
    Last Post: 29-04-2009, 03:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,949,978.15668 seconds with 16 queries