C# → Проверка ввода

Пример реализация проверки ввода на C#. Поля «Имя» и «Адресс» не дылжны оставаться пустыми; Поле «Возраст» должен представлять собой число большее или равное 0; «Профессия» должна быть Programmer или оставаться пустой. При выполнении условий кнопка «ОК» становиться доступной…

проверка ввода C#

Пример проверки ввода C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.btnOK.Enabled = false;

            this.txtName.Tag = false;
            this.txtAge.Tag = false;
            this.txtAddress.Tag = false;
            this.txtOccupation.Tag = false;

            this.txtName.Validating += new CancelEventHandler(this.txtBoxEmpty_Validating);
            this.txtAddress.Validating += new CancelEventHandler(this.txtBoxEmpty_Validating);
            this.txtOccupation.Validating += new CancelEventHandler(txtOccupation_Validating);
            this.txtAge.Validating += new CancelEventHandler(this.txtBoxEmpty_Validating);
            this.txtAge.KeyPress += new KeyPressEventHandler(txtAge_KeyPress);

            this.txtName.TextChanged += new EventHandler(txtBox_TextChanged);
            this.txtAddress.TextChanged += new EventHandler(txtBox_TextChanged);
            this.txtOccupation.TextChanged += new EventHandler(txtBox_TextChanged);
            this.txtAge.TextChanged += new EventHandler(txtBox_TextChanged);
        }

        void txtBox_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;

            if (tb.Text.Length == 0 && tb != txtOccupation)
            {
                tb.Tag = false;
                tb.BackColor = Color.Red;
            }
            else if (tb == txtOccupation && (tb.Text.Length != 0 && tb.Text.CompareTo("Programmer") != 0))
            {
                tb.Tag = false;
            }
            else
            {
                tb.Tag = true;
                tb.BackColor = SystemColors.Window;
            }
            ValidateAll();
        }

        void txtAge_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;
        }

        void txtOccupation_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = (TextBox)sender;

            if (tb.Text.CompareTo("Programmer") == 0 || tb.Text.Length == 0)
            {
                tb.Tag = true;
                tb.BackColor = System.Drawing.SystemColors.Window;
            }
            else
            {
                tb.Tag = false;
                tb.BackColor = Color.Red;
            }

            ValidateAll();
        }

        private void txtBoxEmpty_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = (TextBox)sender;

            if (tb.Text.Length == 0)
            {
                tb.BackColor = Color.Red;
                tb.Tag = false;
            }
            else
            {
                tb.BackColor = System.Drawing.SystemColors.Window;
                tb.Tag = true;
            }

            ValidateAll();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string output;

            output = "Имя: " + this.txtName.Text + "rn";
            output += "Адресс: " + this.txtAddress.Text + "rn";
            output += "Профессия: " + this.txtOccupation.Text + "rn";
            output += "Возраст: " + this.txtAge.Text;

            this.txtOutput.Text = output;
        }

        private void ValidateAll()
        {
            this.btnOK.Enabled = ((bool)(this.txtName.Tag) &&
                                  (bool)(this.txtAddress.Tag) &&
                                  (bool)(this.txtOccupation.Tag) &&
                                  (bool)(this.txtAge.Tag));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}