VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
当前位置:
主页 > 编程开发 > c#教程 >
  • c#教程之C#小程序——显示输入信息

  • 2016-04-14 07:21 来源:未知
C#小程序——显示输入信息
班级:计本一班       姓名:周志刚     学号:20082851
1、程序功能简介:
将输入的个人信息:姓名、地址、职业、年龄,在点击buttonOk按钮后在结果栏中显示出来。并输入时对各个文本框输入的信息进行数据格式校验。
2、界面设计图:

 
3、源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Ex6_1
{
    public partial class Formy1 : Form
    {
        public Formy1()
        {
            InitializeComponent();
            //this.buttonOK.Enabled=false;
            this.ok.Enabled=false;
            this.textBox1.Tag=false;
            this.textBox2.Tag=false;
            this.textBox3.Tag=false;
            this.textBox4.Tag=false;
            this.textBox1.Validating+=new System.ComponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
            this.textBox2.Validating+=new System.ComponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
            this.textBox3.Validating+=new System.ComponentModel.CancelEventHandler(this.textBox3_Validating);
            //this.textBox4.Validating+=new System.ComponentModel.CancelEventHandler(this.textBox4_Validating);
        }
   
 
 
        private void ok_Click(object sender, EventArgs e)
        {
            string output;
            output = "Name:" + this.textBox1.Text + "\r\n";
            output += "Address:" + this.textBox2.Text + "\r\n";
            output += "Occupation:" + this.textBox3.Text + "\r\n";
            output += "Age:" + this.textBox4.Text + "\r\n";
            this.textBox5.Text = output;
        }
 
        private void help_Click(object sender, EventArgs e)
        {
            string output;
            output = "Name=Your name\r\n";
            output += "Address=Your address\r\n"; ;
            output += "Occupation=Your occupation\r\n";
            output += "Age=Your age\r\n";
            this.textBox5.Text = output;
        }
 
        private void clear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }
 
        private void quit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
   
 
 
        private void textBoxEmpty_Validating (object sender,CancelEventArgs e)
        {
            TextBox tb=(TextBox)sender;
            if(tb.TextLength==0)
            {
                tb.BackColor=Color.Red;
                tb.Tag=false;
            }
            else{
                tb.BackColor=System.Drawing.SystemColors.Window;
                tb.Tag=true;
            }
            ValidateOK();
        }
 
        private void textBox3_Validating (object sender,CancelEventArgs e)
        {
            TextBox tb=(TextBox)sender;
            if(tb.Text.CompareTo("教¨¬师º|")==0||tb.Text.Length==0)
            {
                tb.Tag=true;
                tb.BackColor=System.Drawing.SystemColors.Window;
            }
            else
            {
                tb.Tag=false;
                tb.BackColor=Color.Red;
            }
            ValidateOK();
        }
 
        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if((e.KeyChar<48||e.KeyChar>57)&&e.KeyChar!=8)
                e.Handled=true;
        }
 
        private void ValidateOK()
        {
                this.ok.Enabled=((bool)(this.textBox1.Tag)&&
                             (bool)(this.textBox2.Tag)&&
                             (bool)(this.textBox3.Tag)&&
                             (bool)(this.textBox4.Tag));
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
            TextBox tb=(TextBox)sender;
            if(tb.Text.Length==0&&tb!=textBox3)
            {
                tb.Tag=false;
                tb.BackColor=Color.Red;
            }
            else if(tb==textBox3&&(tb.Text.Length!=0&&tb.Text.CompareTo("教¨¬师º|")!=0))
            {
                tb.Tag=false;
            }
            else
            {
                tb.Tag=true;
                tb.BackColor=SystemColors.Window;
            }
            ValidateOK();
        }
    }
相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号