当前位置: 代码迷 >> C# >> C#中窗体的一些容易运用
  详细解决方案

C#中窗体的一些容易运用

热度:380   发布时间:2016-05-05 05:33:39.0
C#中窗体的一些简单运用

从今天开始,我们进入到学window form的知识,今天简单的学习了一些控件和事件的运用。没有什么很全面的理论,所以今天就总结下所写的程序。一个简单的注册页面程序

 
注册页面程序
 
要求:
 
1:修改所有的控件Name 属性 
      
 
2: 登录事件   检测各个控件是否为空,如果是空  弹出注册失败    如果成功  则显示新窗体 并且 新窗体上面显示    “XXX你好! 欢迎来到云和学院学习Net” 走马灯形式
                    密码输入三次那么登录按钮不可用  3分钟之后可用
                    把注册信息的各个数据按照     [email protected]|18301412747|男|足球,篮球,排球”写入到一个文本文件中
 
 
 
代码:
 
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         
        int num = 1;   //定义num是为了获取输入错误的次数
 
        private void btnregster_Click(object sender, EventArgs e)
        { 
            //如果达到三次则注册按钮将不能使用
            if (num == 3)
            {
                this.btnregster.Enabled = false;
            }
            //定义字符串来接收文本数据
            string user = this.txtname.Text.Trim();
            string pwd = this.txtpwd.Text.Trim();  
            string email = this.txtemail.Text.Trim();
            string phone = this.txtphone.Text.Trim();
  
    //判断用户名、密码、邮箱、手机、性别、爱好是否为空,如果为空,则提示注册失败,否则则提示注册成功,进入下一个界面
                if (string.IsNullOrEmpty(user))
                {
                    MessageBox.Show("注册失败,未输入用户名!");
                    ++num; //计时器的累加
                }
 
                else if (string.IsNullOrEmpty(pwd))
                {
                    MessageBox.Show("注册失败,未输入密码!");
                    ++num;
                }
 
               else if (txtaginpwd.Text != pwd)
                {
                    MessageBox.Show("注册失败,确认密码必须保持一致");
                    ++num;
                }
 
                else if (string.IsNullOrEmpty(email))
                {
                    MessageBox.Show("注册失败,未输入邮箱");
                    ++num;
                }
 
                else if (string.IsNullOrEmpty(phone))
                {
                    MessageBox.Show("注册失败,未输入手机号");
                    ++num;
                }
 
                else if (cbkbasketball.Checked==false && cbkpaiqiu.Checked==false && cbkscore.Checked==false)//只有在都没有被选中的情况下才显示注册失败
                {
                    MessageBox.Show("注册失败,请选择爱好!");
                    ++num;
                }
 
                else if (radman.Checked==false && radwomen.Checked==false  )
                {
                    MessageBox.Show("注册失败,请选择性别"); 
                     ++num;
                }
 
               else
              {
                   MessageBox.Show("注册成功");
                   Form2 fm = new Form2(user);//打开Form2的窗体,这里传入一个参数user。
                   fm.Show();
                   this.Hide();    //隐藏Form1的窗体  
             }
 
           //创建一个Regster文本文档,并写入注册信息,且以分隔符(|)隔开
                string gender = string.Empty;
                string like = string.Empty;
<br>              //判断性别被选中的是哪个,就获取哪个的文本
                if (radman.Checked == true)
                {
                    gender = radman.Text;
                }
                else
                {
                    gender = radwomen.Text;
                }
              //判断爱好哪几个被选中,则获取选中的文本
                 
                if (this.cbkbasketball.Checked)
                {
                    like += cbkbasketball.Text + ",";
                    
                }
                if (this.cbkpaiqiu.Checked)
                {
                    like += cbkpaiqiu.Text+",";
                   
                }
                if (this.cbkscore.Checked)
                {
                    like += cbkscore.Text+",";
                }
                string[] array = { txtname.Text, txtpwd.Text, txtemail.Text, txtphone.Text, gender,like };//定义一个数组来接收注册信息的数据
                string strs = string.Empty;
                foreach (var item in array)
               {
                    strs += item;
                    strs = string.Join("|",array);//注册信息在文本文档中以分隔符隔开
           }
                File.WriteAllText("Regster.txt", strs);//若只写文档名字,则默认的路径是在本项目的bin目录下。
         }
 
            private void btnconsole_Click(object sender, EventArgs e)//取消按钮
 
      { 
                 
            txtname.Focus();//让用户名重新获取焦点
            txtname.Text = "";
            txtpwd.Text = "";
            txtaginpwd.Text = "";
            txtemail.Text = "";
            txtphone.Text = "";
            radman.Checked = false;
            radwomen.Checked = false;
            cbkbasketball.Checked = false;
            cbkpaiqiu.Checked = false;
            cbkscore.Checked = false;
             
        }
 
        private void timer1_Tick(object sender, EventArgs e)
 
        {
             //输入三次错误后,计时器停止输入3分钟后再重新输入
                this.btnregster.Enabled = true;
               
        }
 
        private void Form1_Activated(object sender, EventArgs e)
 
       {
            txtname.Focus();//首先让用户名文本框获得焦点
     }      
        
  相关解决方案