Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-02-2009
Member
 
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!
Reply With Quote
  #2  
Old 10-02-2009
Member
 
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.
Reply With Quote
  #3  
Old 10-02-2009
Member
 
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());
    }
}
Reply With Quote
  #4  
Old 10-02-2009
Member
 
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.
Reply With Quote
  #5  
Old 10-02-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "C#: Validation inside a Textbox?"
Thread Thread Starter Forum Replies Last Post
Excel/VBA: Copying text from UserForm Textbox to a Worksheet Textbox Neil Ives Software Development 2 20-08-2010 01:23 AM
ProgressBar In TextBox Level8 Software Development 5 06-02-2010 08:55 PM
Textbox validation in ASP.NET Kasper Software Development 4 19-01-2010 08:28 PM
Numerical value on TextBox KABIRA16 Software Development 3 29-10-2009 04:25 PM
How to recover a value in a textbox S_Asnodkar Software Development 4 29-04-2009 03:37 PM


All times are GMT +5.5. The time now is 05:39 PM.