学生信息管理系统 (初级版)

form1

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 WindowsFormsApplication1.DataOperation;
using WindowsFormsApplication1.Model;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ShuaXin();
        }



        public void ShuaXin()
        {
            listView1.Items.Clear();
            List<Student> data = new StudentData().SelectAll();

            foreach (Student s in data)
            {
                ListViewItem li = new ListViewItem();
                li.Text = s.Code;
                li.SubItems.Add(s.Name);
                li.SubItems.Add(s.Sex ? "" : "");
                li.SubItems.Add(s.Birthday.ToString("yyyy年MM月dd日"));
                li.SubItems[3].Tag = s.Birthday;

                li.SubItems.Add(s.Score.ToString("#.##"));

                listView1.Items.Add(li);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Owner = this;
            f2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Student s = new Student();
            if (listView1.SelectedItems.Count != 1)
            {
                MessageBox.Show("只能选择一个学员进行修改!");
                return;
            }

            foreach (ListViewItem li in listView1.SelectedItems)
            {
                s.Code = li.Text;
                s.Name = li.SubItems[1].Text;
                if (li.SubItems[2].Text == "")
                {
                    s.Sex = true;
                }
                else
                {
                    s.Sex = false;
                }
                s.Birthday = Convert.ToDateTime(li.SubItems[3].Tag);
                s.Score = Convert.ToDecimal(li.SubItems[4].Text);
            }
            Form3 f3 = new Form3(s);
            f3.Owner = this;
            f3.Show();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

form2

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 WindowsFormsApplication1.DataOperation;
using WindowsFormsApplication1.Model;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 F1 = null;
        public Form2(Form1 f1)
        {
            InitializeComponent();

            label_Code.Text = new StudentData().ReturnCode();
            F1 = f1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // text =="" && text == null
            if (String.IsNullOrEmpty(textBox1.Text))
            {
                name_error.Text = "不能为空";
                return;
            }
            else
            {
                name_error.Text = "";
            }

            Student s = new Student();
            s.Code = label_Code.Text;
            s.Name = textBox1.Text;
            s.Sex = radioButton_Nan.Checked;
            s.Birthday = dateTimePicker1.Value;
            s.Score = Convert.ToDecimal(maskedTextBox1.Text);

            bool isok = new StudentData().Insert(s);
            if (isok)
            {
                MessageBox.Show("添加成功!");
                if (F1 != null)
                {
                    F1.ShuaXin();
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("添加失败!");
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

form3

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 WindowsFormsApplication1.DataOperation;
using WindowsFormsApplication1.Model;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3(Student s)
        {
            InitializeComponent();

            label_Code.Text = s.Code;
            textBox1.Text = s.Name;
            if (s.Sex)
            {
                radioButton_Nan.Checked = true;
            }
            else
            {
                radioButton_Nv.Checked = true;
            }
            dateTimePicker1.Value = s.Birthday;
            maskedTextBox1.Text = s.Score.ToString();

        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }
    }
}
原文地址:https://www.cnblogs.com/suiyuejinghao123/p/5636335.html