WinForm简介

WinForm:
Windows Form 系统窗体 - 客户端应用程序

窗体分为可视化界面和界面代码两部分
Form是窗体可视化界面,.Designer.cs结尾的文件,是窗体界面的源代码,
界面是通过这个源代码来构建组成的;

简单控件:
button - 按钮
textbox - 文本框
label - 文字显示框

做了一个登陆的界面

C#是用来写后台功能的
Sql是用来存储数据的
界面,是用来展示数据的,并且接收用户操作的


sender是事件的主体,e是事件的数据,这里把sender转换为button类的目的
是获取用户操作,因为事件主体是一个button,所以sender可以转换为button
类。

登录界面

主函数体代码

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 登录界面1.m;

namespace 登录界面1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //登录按钮的点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            //1、需要验证的用户对象
            login log = new login();
            log.username = textBox1.Text;
            log.pwd = textBox2.Text;

            //2、送到方法中去验证
            bool isok = new logindata().su(log);

            //3、返回验证结果
            if (isok)
            {
                MessageBox.Show("登陆成功!");
            }
            else
            {
                MessageBox.Show("登陆失败!");
            }
        }
        //注册按钮的点击事件
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            this.Hide();
            f2.Show();
        }
    }
}

实体类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 登录界面1.m
{
    public class login
    {
        private string _username;

        public string username
        {
            get { return _username; }
            set { _username = value; }
        }

        private string _pwd;

        public string pwd
        {
            get { return _pwd; }
            set { _pwd = value; }
        }

        private bool _sex;

        public bool sex
        {
            get { return _sex; }
            set { _sex = value; }
        }

        private string _ah;

        public string ah
        {
            get { return _ah; }
            set { _ah = value; }
        }

    }
}

注册界面

主函数体代码

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 登录界面1.m;

namespace 登录界面1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                label8.Text = "姓名不能为空!";
                return;
            }
            else
            {
                label8.Text = "";
            }
            if (textBox2.Text != textBox3.Text)
            {
                label6.Text = "两次密码不一致!";
                return;//阻止程序往下继续执行!
            }
            else
            {
                label6.Text = "";
            }

            //1、创建用户对象
            login log1 = new login();
            log1.username = textBox1.Text;
            log1.pwd = textBox2.Text;
            if (radioButton1.Checked)
            {
                log1.sex = true;
            }
            else if (radioButton2.Checked)
            {
                log1.sex = false;
            }
            else
            {
                label7.Text = "请选择您的性别!";
                return;
            }
            if (checkBox1.Checked)
            {
                log1.ah = checkBox1.Text;
            }
            else if (checkBox2.Checked)
            {
                log1.ah = checkBox2.Text;
            }
            else if (checkBox1.Checked && checkBox2.Checked)
            {
                log1.ah = checkBox1.Text + "  " + checkBox2.Text;
            }
            else
            {
                log1.ah = null;
            }

            //2、输送到方法里面,添加到数据库中
            bool isok = new logindata().iu(log1);

            //3、提示注册是否成功
            if (isok)
            {
                MessageBox.Show("注册成功!");                
            }
            else
            {
                MessageBox.Show("注册失败!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.Show();
            this.Close();
        }
    }
}

数据访问类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using 登录界面1.m;

namespace 登录界面1
{
    public class logindata
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        public logindata()
        {
            conn = new SqlConnection("server=.;database=ADO;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }
        public bool su(login l)
        {
            bool isok = false;
            cmd.CommandText = "select *from [login] where username=@a and pwd=@b";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@a", l.username);
            cmd.Parameters.Add("@b", l.pwd);

            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                isok = true;
            }
            conn.Close();

            return isok;
        }
        public bool iu(login l)
        {
            bool isok = false;

            cmd.CommandText = "insert into [login] values(@a,@b,@c,@d);";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@a", l.username);
            cmd.Parameters.Add("@b", l.pwd);
            cmd.Parameters.Add("@c", l.sex);
            cmd.Parameters.Add("@d", l.ah);

            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                isok = true;
            }
            catch { }
            finally
            {
                conn.Close();
            }
            return isok;
        }

    }
}

原文地址:https://www.cnblogs.com/123lucy/p/5665092.html