TextBox validation : TextBox « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » TextBox 
23.17.9.TextBox validation
TextBox validation
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 < || 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(25315);
        this.numberBox.Margin = new System.Windows.Forms.Padding(4444);
        this.numberBox.Name = "numberBox";
        this.numberBox.Size = new System.Drawing.Size(5722);
        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(1618);
        this.label1.Margin = new System.Windows.Forms.Padding(4040);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(20116);
        this.label1.TabIndex = 1;
        this.label1.Text = "Enter a number between 1 and 10";
        // 
        // okButton
        // 
        this.okButton.Location = new System.Drawing.Point(33511);
        this.okButton.Margin = new System.Windows.Forms.Padding(4444);
        this.okButton.Name = "okButton";
        this.okButton.Size = new System.Drawing.Size(10028);
        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(00);
        this.maskedTextBox1.Name = "maskedTextBox1";
        this.maskedTextBox1.Size = new System.Drawing.Size(10023);
        this.maskedTextBox1.TabIndex = 3;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(453246);
        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(4444);
        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());
    }
}
23.17.TextBox
23.17.1.Get input value from TextBox and display it vis a messagebox
23.17.2.TextBox key pressed event
23.17.3.TextBox Modified and TextChangedTextBox Modified and TextChanged
23.17.4.TextBox: Multiline and BorderStyleTextBox: Multiline and BorderStyle
23.17.5.TextBox Keyevent handerTextBox Keyevent hander
23.17.6.TextBox cancel eventTextBox cancel event
23.17.7.Count lines in the TextBox and read text in TextBox line by lineCount lines in the TextBox and read text in TextBox line by line
23.17.8.TextBox selectionTextBox selection
23.17.9.TextBox validationTextBox validation
23.17.10.Add ContextMenu to TextBoxAdd ContextMenu to TextBox
23.17.11.Use Regular Expression to Check the input for a TextBoxUse Regular Expression to Check the input for a TextBox
23.17.12.Multiline TextBoxMultiline TextBox
23.17.13.Simple Editor
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.