资源列表:
业务逻辑BLL
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace student { public class StudentInstance { public DataChange onDataChange = null; private List<StudentClass> StuList = new List<StudentClass>(); public List<StudentClass> stuAry { get { return StuList; } } public void AddStudent(StudentClass ParamStu) { if (StuList.Contains(ParamStu)) throw new Exception("student is exist!"); StuList.Add(ParamStu); if (onDataChange != null) onDataChange(); } public void delStudent(StudentClass ParamStu) { StuList.Remove(ParamStu); if (onDataChange != null) onDataChange(); } public void loadAllData() { DataSourceManage ds = new DataSourceManage(); StuList = ds.getStudentData(); if (onDataChange != null) { onDataChange(); } } } }
控制器部分:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace student { class MVcontrol { StudentInstance stu; public MVcontrol(StudentInstance ParamStu) { stu = ParamStu; } public void UpdateTree(TreeView tv) { tv.Nodes.Clear(); foreach (StudentClass m in stu.stuAry) { TreeNode node = new TreeNode(); node.Tag = m; node.Text = m.Name; tv.Nodes.Add(node); } } public static void UpdateCbx(ComboBox cbx,Type type) { cbx.Items.Clear(); if (type.IsEnum) { string[] enumStr = Enum.GetNames(type); foreach (string m in enumStr) { cbx.Items.Add(m); } } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace student { public delegate void DataChange(); public delegate void ThreadDelegate(int js); }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace student { class ThreadClass { public ThreadDelegate MainThreadUpdate = null; public void ThreadFun() { int i = 0; while (true) { ++i; MainThreadUpdate(i); Thread.Sleep(2000); } } } }
UI 及逻辑
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 System.Threading; namespace student { public partial class MainForm : Form { Thread thread1; ThreadClass th; StudentInstance stu; MVcontrol mvc; public MainForm() { InitializeComponent(); stu = new StudentInstance(); stu.onDataChange = dataChange; mvc= new MVcontrol(stu); this.Load += new EventHandler(MainForm_Load); this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing); btn_start.Click += new EventHandler(btn_start_Click); btn_add.Click += new EventHandler(btn_add_Click); btn_del.Click += new EventHandler(btn_del_Click); tv_student.AfterSelect += new TreeViewEventHandler(tv_student_AfterSelect); th = new ThreadClass(); th.MainThreadUpdate = ThreadUpdate; thread1 = new Thread(new ThreadStart(th.ThreadFun)); thread1.Start(); } void tv_student_AfterSelect(object sender, TreeViewEventArgs e) { StudentClass tag = (StudentClass)e.Node.Tag; this.txt_id.Text = tag.Id.ToString(); this.txt_name.Text = tag.Name; this.cbx_sex.SelectedIndex = (int)tag.Sex; this.txt_age.Text = tag.Age.ToString(); } void btn_del_Click(object sender, EventArgs e) { stu.delStudent((StudentClass)this.tv_student.SelectedNode.Tag); } void btn_add_Click(object sender, EventArgs e) { try { stu.AddStudent(new StudentClass( Convert.ToInt32(this.txt_id.Text.Trim()), this.txt_name.Text.Trim(), (Sex)this.cbx_sex.SelectedIndex, Convert.ToInt32(this.txt_age.Text.Trim()))); } catch (Exception e1) { MessageBox.Show(e1.Message); } } void btn_start_Click(object sender, EventArgs e) { stu.loadAllData(); } void MainForm_Load(object sender, EventArgs e) { MVcontrol.UpdateCbx(this.cbx_sex, typeof(Sex)); } private void dataChange() { mvc.UpdateTree(this.tv_student); } void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (thread1.IsAlive) { thread1.Abort(); } } public void ThreadUpdate(int js) { if (this.label5.InvokeRequired) { ThreadClass th1 = new ThreadClass(); th1.MainThreadUpdate = ThreadUpdate; this.Invoke(th1.MainThreadUpdate, new object[] { js }); } else { this.label5.Text=js.ToString(); } } } }
其它部分请下载完整代码查看:
链接:https://pan.baidu.com/s/1acW5ho7kDkft_pHeiwB-2A
提取码:uk3c
--来自百度网盘超级会员V4的分享
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


