TextBox控件的使用,MaskedTextBox控件的使用

来源:互联网 发布:软件检测工程师 编辑:程序博客网 时间:2024/06/05 19:58

1、TextBox控件的使用

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Text.RegularExpressions;namespace TextBox控件的使用{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void buttonOK_Click(object sender, EventArgs e)        {            string msg;            msg = "用户名:" + this.textBoxName.Text + Environment.NewLine;            msg += "电子邮箱:" + this.textBoxEmail.Text + Environment.NewLine;            msg += "年龄:" + this.textBoxAge.Text + Environment.NewLine;            this.textBoxOut.Text = msg;        }        private void buttonCancel_Click(object sender, EventArgs e)        {            Application.Exit();        }        private void buttonHelp_Click(object sender, EventArgs e)        {            string msg;            msg = "在“用户名”文本框中输入您的姓名" + Environment.NewLine;            msg += "在“电子邮箱”文本框中输入您的电子邮件地址,注意使用正确的格式" + Environment.NewLine;            msg += "在“年龄”文本框中输入您的年龄";            this.textBoxOut.Text = msg;        }        private void ValidateOK()        {            if (this.textBoxAge.Tag == "1" && this.textBoxName.Tag == "1" && this.textBoxEmail.Tag == "1")                this.buttonOK.Enabled = true;        }        private void textBoxEmail_Validating(object sender, CancelEventArgs e)        {            TextBox tb = (TextBox)sender;            Regex re = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");            if (tb.Text == "" || !re.Match(tb.Text).Success)            {                tb.Tag = "0";                tb.BackColor = Color.FromArgb(255, 128, 128);            }            else            {                tb.Tag = "1";                tb.BackColor = SystemColors.Window;            }            this.ValidateOK();        }        private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)        {            if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)            {                //如果输入键不是0-9之间的数字表示无效                e.Handled = true;            }        }        private void textBoxEmpty_Validating(object sender, CancelEventArgs e)        {            TextBox tb = (TextBox)sender;            if (tb.Text == "")            {                tb.BackColor = Color.FromArgb(255, 128, 128);                tb.Tag = "0";            }            else            {                tb.Tag = "1";                tb.BackColor = SystemColors.Window;            }            this.ValidateOK();        }        private void textBox_TextChanged(object sender, EventArgs e)        {            TextBox tb = (TextBox)sender;            Regex re = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");            if (tb.Text == "" && tb != this.textBoxEmail)            {                tb.Tag = "0";                tb.BackColor = Color.FromArgb(255, 128, 128);            }            else if (tb == this.textBoxEmail && tb.Text != "" && !re.Match(this.textBoxEmail.Text).Success)            {                tb.Tag = "0";                tb.BackColor = Color.FromArgb(255, 128, 128);            }            else            {                tb.Tag = "1";                tb.BackColor = SystemColors.Window;            }            this.ValidateOK();        }    }}

2、MaskedTextBox控件的使用

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace MaskedTextBox控件的使用{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void buttonOK_Click(object sender, EventArgs e)        {            string msg;            msg = "用户名:" + this.textBox1.Text + Environment.NewLine;            msg += "出生日期:" + this.maskedTextBox1.Text + Environment.NewLine;            msg += "身份证号:" + this.maskedTextBox2.Text + Environment.NewLine;            msg += "邮政编码:" + this.maskedTextBox3.Text + Environment.NewLine;            this.textBox2.Text = msg;        }        private void buttonCancel_Click(object sender, EventArgs e)        {            Application.Exit();        }        private void MaskedTextBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)        {            this.statusStrip1.Items[0].Text = "只能输入数字!";        }        private void maskedTextBox_KeyDown(object sender, KeyEventArgs e)        {            this.statusStrip1.Items[0].Text = "";        }        private void textBox1_Leave(object sender, EventArgs e)        {            if (this.textBox1.Text == "")            {                this.statusStrip1.Items[0].Text = "用户名不能为空白";                this.textBox1.Focus();            }        }    }}


原创粉丝点击