WinForm简单人员管理系统(未设置弹出唯一窗口限制)

类:

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

namespace 简单人员管理系统1.App_Code
{
    public partial class Users
    {
        Data0425DataContext con = new Data0425DataContext();
        public string NationName()
        {
            return con.Nation.Where(r => r.NationCode == this.Nation).First().NationName;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 简单人员管理系统1.App_Code
{
    public class NationData
    {
        Data0425DataContext con = new Data0425DataContext();

        public List<Nation> Select()
        {
            return con.Nation.ToList();
        }


    }
}

操作类:

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

namespace 简单人员管理系统1.App_Code
{
    public class UsersData
    {
        Data0425DataContext con = new Data0425DataContext();
        //登录验证
        public Users Select(string uname, string pwd)
        {
            return con.Users.Where(r => r.UserName == uname && r.PassWord == pwd).FirstOrDefault();
        }
        //查询显示
        public List<Users> Select()
        {
            return con.Users.ToList();
        }
        //删除选中的信息
        public void Delete(string key)
        {
            Users u = con.Users.Where(r => r.UserName == key).FirstOrDefault();
            if (u != null)
            {
                con.Users.DeleteOnSubmit(u);
                con.SubmitChanges();
            }
        }
        //添加
        public void Inserti(Users u)
        {
            con.Users.InsertOnSubmit(u);
            con.SubmitChanges();
        }
    }
}

登录界面

后台代码:

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 简单人员管理系统1.App_Code;

namespace 简单人员管理系统1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1.Click += button1_Click;
        }

        void button1_Click(object sender, EventArgs e)
        {
            string uname = textBox1.Text;
            string pwd = textBox2.Text;

            Users u = new UsersData().Select(uname, pwd);

            if (u != null)
            {
                Form2 f2 = new Form2(this);
                f2.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("用户名密码输入有误!");
            }
        }
    }
}

主界面:

后台代码:

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 简单人员管理系统1.App_Code;

namespace 简单人员管理系统1
{
    public partial class Form2 : Form
    {
        Form1 F1 = null;
        public Form2(Form1 fff)
        {
            InitializeComponent();
            F1 = fff;

            this.FormClosing += Form2_FormClosing;

            DataBind();

        }

        void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            F1.Close();
        }
public void DataBind() { listView1.Items.Clear(); List<Users> list = new UsersData().Select(); if (list.Count > 0) { int code = 1; foreach (Users u in list) { ListViewItem li = new ListViewItem(); li.Text = code.ToString(); li.SubItems.Add(u.UserName); li.SubItems.Add(u.PassWord); li.SubItems.Add(u.NickName); li.SubItems.Add((bool)u.Sex ? "" : ""); li.SubItems.Add(((DateTime)u.Birthday).ToString("yyyy年MM月dd日")); li.SubItems.Add(u.NationName()); listView1.Items.Add(li); code++; } } } //刷新 private void button4_Click(object sender, EventArgs e) { DataBind(); } //删除 private void button3_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { DialogResult dia = MessageBox.Show("数据删除后不可恢复,是否要删除吗?", "警告!", MessageBoxButtons.OKCancel); if (dia == DialogResult.OK) { foreach (ListViewItem li in listView1.SelectedItems) { new UsersData().Delete(li.SubItems[1].Text); } DataBind(); } } else { MessageBox.Show("请选中要删除的项!"); } } private void button1_Click(object sender, EventArgs e) { //1、打开添加窗口 Form3 f3 = new Form3(this); f3.Show(); } private void button2_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0) { MessageBox.Show("请先选中要修改的项"); return; } if (listView1.SelectedItems.Count > 1) { MessageBox.Show("只能选择一条数据进行修改"); return; } string uname = listView1.SelectedItems[0].SubItems[1].Text; string pwd = listView1.SelectedItems[0].SubItems[2].Text; Users u =new UsersData().Select(uname,pwd); Form3 f3 = new Form3(this, u); f3.Show(); } } }

添加与修改界面:

后台代码:

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 简单人员管理系统1.App_Code;

namespace 简单人员管理系统1
{
    public partial class Form3 : Form
    {
        Form2 F2 = null;
        Users uuu = null;
        public Form3(Form2 f2)
        {
            InitializeComponent();
            F2 = f2;
            List<Nation> Nlist = new NationData().Select();

            comboBox1.DataSource = Nlist;
            comboBox1.DisplayMember = "NationName";
            comboBox1.ValueMember = "NationCode";
        }

        public Form3(Form2 f2, Users u)
        {
            InitializeComponent();
            F2 = f2;
            List<Nation> Nlist = new NationData().Select();

            comboBox1.DataSource = Nlist;
            comboBox1.DisplayMember = "NationName";
            comboBox1.ValueMember = "NationCode";

            //变成修改界面
            label1.Text = "修改用户";
            button1.Text = "确认修改";

            uuu = u;

            textBox1.Text = u.UserName;
            textBox1.ReadOnly = true;
            textBox2.Text = u.PassWord;
            textBox3.Text = u.NickName;
            if ((bool)u.Sex)
                radioButton1.Checked = true;
            else
                radioButton2.Checked = true;

            dateTimePicker1.Value = (DateTime)u.Birthday;
            comboBox1.SelectedValue = u.Nation;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (uuu == null)
            {
                Users u = new Users();
                u.UserName = textBox1.Text;
                u.PassWord = textBox2.Text;
                u.NickName = textBox3.Text;

                u.Sex = radioButton1.Checked;
                u.Birthday = dateTimePicker1.Value;
                u.Nation = (comboBox1.SelectedItem as Nation).NationCode;

                new UsersData().Inserti(u);
                MessageBox.Show("添加成功!");
            }
            else
            {
                MessageBox.Show("修改成功!");
            }
            F2.DataBind();
            this.Close();
        }
    }
}

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