Simple Form Validation : Data Validation « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » Data ValidationScreenshots 
Simple Form Validation
Simple Form Validation

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Validation
{
    /// <summary>
    /// Summary description for SimpleValidation.
    /// </summary>
    public class SimpleValidation : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.GroupBox GroupBox1;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.TextBox txtFirstName;
        internal System.Windows.Forms.TextBox txtLastName;
        internal System.Windows.Forms.Button Button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public SimpleValidation()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Disposebool disposing )
        {
            ifdisposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Disposedisposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.txtFirstName = new System.Windows.Forms.TextBox();
            this.txtLastName = new System.Windows.Forms.TextBox();
            this.Button1 = new System.Windows.Forms.Button();
            this.GroupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // GroupBox1
            // 
            this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.Label2,
                                                                                    this.Label1,
                                                                                    this.txtFirstName,
                                                                                    this.txtLastName});
            this.GroupBox1.Location = new System.Drawing.Point(812);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(36888);
            this.GroupBox1.TabIndex = 11;
            this.GroupBox1.TabStop = false;
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(1652);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(6416);
            this.Label2.TabIndex = 8;
            this.Label2.Text = "Last Name:";
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(1628);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(6416);
            this.Label1.TabIndex = 7;
            this.Label1.Text = "First Name:";
            // 
            // txtFirstName
            // 
            this.txtFirstName.Location = new System.Drawing.Point(8424);
            this.txtFirstName.Name = "txtFirstName";
            this.txtFirstName.Size = new System.Drawing.Size(15221);
            this.txtFirstName.TabIndex = 4;
            this.txtFirstName.Text = "";
            this.txtFirstName.Validating += new System.ComponentModel.CancelEventHandler(this.txtName_Validating);
            // 
            // txtLastName
            // 
            this.txtLastName.Location = new System.Drawing.Point(8448);
            this.txtLastName.Name = "txtLastName";
            this.txtLastName.Size = new System.Drawing.Size(15221);
            this.txtLastName.TabIndex = 5;
            this.txtLastName.Text = "";
            this.txtLastName.Validating += new System.ComponentModel.CancelEventHandler(this.txtName_Validating);
            // 
            // Button1
            // 
            this.Button1.Location = new System.Drawing.Point(152208);
            this.Button1.Name = "Button1";
            this.Button1.Size = new System.Drawing.Size(7624);
            this.Button1.TabIndex = 10;
            this.Button1.Text = "OK";
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            // 
            // SimpleValidation
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(514);
            this.ClientSize = new System.Drawing.Size(384242);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.GroupBox1,
                                                                          this.Button1});
            this.Font = new System.Drawing.Font("Tahoma"8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "SimpleValidation";
            this.Text = "SimpleValidation";
            this.GroupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        [STAThread]
        static void Main() 
        {
            Application.Run(new SimpleValidation());
        }

        private void txtName_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (((TextBox)sender).Text == "")
            {
                MessageBox.Show("You must enter a first and last name.""Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                e.Cancel = true;
            }
        }
    }
}


           
       
Related examples in the same category
1.Error Provider ValidationError Provider Validation
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.