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

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

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

做了一个登陆的界面

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

sender是点击的事件主体,sender可以转换为点击的事件的类。e是点击事件的数据。

 

 

登录界面

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;
using 第一次窗体.model;
using 第一次窗体.logindata;

namespace 第一次窗体
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        
     //注册按钮点击事件
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 fa = new Form2();
            fa.Show();
        }
        //登录按钮点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            //需要验证的用户对象
            login log = new login();
            log.Username = textBox1.Text;
            log.Password = textBox2.Text;

            //送到方法中去验证
            bool isok = new Logindata().selectuser(log);
            //提示注册是否成功
            if (isok)
            {
                MessageBox.Show("登录成功");
            }
            else
            {
                MessageBox.Show("登录失败");
            }
        }
    }
}

实体类

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

namespace 第一次窗体.model
{
    public class login
    {
        private string _username;

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

        private string _password;

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        private bool _sex;

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

        private string _fuyuan;

        public string Fuyuan
        {
            get { return _fuyuan; }
            set { _fuyuan = value; }
        }

    }
}

数据访问类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using 第一次窗体.model;

namespace 第一次窗体.logindata
{
    public class Logindata
    {
        SqlConnection conn=null;
        SqlCommand com=null;
        public Logindata()
        {
            conn=new SqlConnection("server=.;database=student;user=sa;pwd=123");
            com=conn.CreateCommand();
        }
        public bool selectuser(login l)
        {
            bool isok=false;
            com.CommandText="select *from [login] where username=@a and password=@b";
            com.Parameters.Clear();
            com.Parameters.Add("@a", l.Username);
            com.Parameters.Add("@b", l.Password);

            conn.Open();
            SqlDataReader de=com.ExecuteReader();
            if(de.HasRows)
            {
                isok=true;
            }

            conn.Close();

            return isok;
        }

        public bool insertuser(login l)
        {
            bool isok = false;
            com.CommandText = "insert into login values (@a,@b,@c,@d)";
            com.Parameters.Clear();
            com.Parameters.Add("@a", l.Username);
            com.Parameters.Add("@b", l.Password);
            com.Parameters.Add("@c", l.Sex);
            com.Parameters.Add("@d", l.Fuyuan);
            try
            {
                conn.Open();
                com.ExecuteNonQuery();
                isok = true;
            }
            catch { }
            finally
            {
                conn.Close();
            }
            return isok;
        }

    }
}

注册界面

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;
using 第一次窗体.logindata;
using 第一次窗体.model;

namespace 第一次窗体
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //登录按钮点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != textBox3.Text)
            {
                label5.Text = ("两次密码不一致!");
                return;
            }
            if (textBox1.Text == "")
            {
                MessageBox.Show("用户不能为空");
                return;
            }
            //创建用户对象
            login loo = new login();
            loo.Username = textBox1.Text;
            loo.Password = textBox2.Text;

            if (radioButton1.Checked)
            {
                loo.Sex = true;
            }
            else if (radioButton2.Checked)
            {
                loo.Sex = false;
            }
           
            if (checkBox1.Checked && checkBox2.Checked)
            {
                loo.Fuyuan = checkBox1.Text + " " + checkBox2.Text;
            }
            else if (checkBox1.Checked)
            {
                loo.Fuyuan=checkBox1.Text;
            }
            else if (checkBox2.Checked)
            {
                loo.Fuyuan=checkBox2.Text;
            }

            
            //输送到方法里面。添加到数据库中
            bool isok = new Logindata().insertuser(loo);
            //提示注册是否成功
            if (isok)
            {
                MessageBox.Show("注册成功");
                this.Close();
            }
            else
            {
                MessageBox.Show("注册失败");
            }
        }
    }
}

 

posted on 2016-07-13 17:33  斐雪  阅读(9071)  评论(0编辑  收藏  举报