c# textbox 学习

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 Demo02
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            string strName, strPassword;
            strName = txtName.Text.Trim();
            strPassword = txtPassword.Text.Trim();
            Login(strName,strPassword);
        }
        //按下回车键

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Return)
            {
                string strName, strPassword;
                //去掉空格  txtName.Text可以取出来 textbox的内容
                strName = txtName.Text.Trim();
                strPassword = txtPassword.Text.Trim();
                Login(strName,strPassword);
            }

        }

        private void Login(string strName,string strPassword)
        {
            
            if (strName == "" && strPassword == "")
            {
                MessageBox.Show("用户名和 密码不能为空");

            }
            else if (strName == "" || strPassword == "")
            {
                if (strName == "")
                {
                    MessageBox.Show("用户名不能为空");
                }
                else
                    MessageBox.Show("密码不能为空");
            }

            else if (strName == "admin" && strPassword == "123")
            {
                MessageBox.Show("登录成功");
            }
            else
            {
                if (strName != "admin" && strName != "")

                    MessageBox.Show("用户名输入错误");

                if (strPassword != "123" && strPassword != "")
                    MessageBox.Show("密码输入错误");

            }
        }





    }
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页