C# 用面向对象的思想去编程

再接上一篇博文,之前写的两篇博文虽然实现了功能,但是和控件之间的粘性太大,依赖于控件进行操作,所以这篇博文主要用面向对象的思想做一个Demo,将逻辑层与显示层剥离开

首先新建一个窗体文件,搭建界面完毕:

新建一个类,命名为LawHelper

在LawHelper里新建两个对象,分别存放法律法规的名称编号和具体的位置:

 public class Law
    {
        public string Nmae;
        public int ID;
    }

    public class LawNode
    {
        public string Name;
        public int Level;
        public int Parent;
        public int ID;
        public string LawName;
        public int LawID;
    }

再新建三个函数,分别对应着界面上的combobox选项,左边树状图的Node的具体位置以及右边textbox里的具体法律条款的显示:

public List<Law> GetLaws()
        {
            //实例化Law类
            Law law = new Law();
            return null;
        }

        public List<LawNode> GetLawNodes(Law law)
        {
            LawNode lawNode = new LawNode();
            return null;
        }

        public string GetLawText(LawNode lawnode)
        {
            return null;
        }

编写第一个函数GetLaws(),获取到对应的法律法规的名称:

 public List<Law> GetLaws()
        {
            //新建List
            List<Law> laws = new List<Law>();

            //输入想关联的数据库的基本信息
            SqlConnection conn = new SqlConnection(@"server = DESKTOP-5SDB4D4;Initial Catalog= test;User ID=sa;Password=lmz123LMZ");
            //打开数据库连接
            conn.Open();
            //sql语句
            string str = " select * from [test].[dbo].[TRaw] where c1 = 0";
            //创建命令对象
            SqlCommand lo_cmd = conn.CreateCommand();
            lo_cmd.CommandText = str;
            SqlDataReader dtr = lo_cmd.ExecuteReader();
            while (dtr.Read())
            {
                //实例化Law类
                Law law = new Law();
                //法律名称
                law.Nmae = (string)dtr["c3"];
                //法律代码
                law.ID = (int)dtr["c0"];
                //将值赋到List中
                laws.Add(law);
            }
                return laws;
        }

值得一提的是,我一开始将对象的实例放在了循环的外面,导致往List赋值的时候后面的值会将前面的值全部覆盖,解决方法就是在循环里实例化

编写第二个和第三个函数,分别获取具体的法律条文的位置以及输出最后的法律条文信息

编写完成后去主界面编写程序,首先实例化LawHelper类:

 //实例化LawHelper类
        LawHelper lawhelper = new LawHelper();
        Law law = new Law();
        List<LawNode> lawslocation = new List<LawNode>();

再对应编写对应的函数代码,具体代码如下显示:

LawHelper:

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


namespace WindowsFormsApp4
{
    public class Law
    {
        public string Name;
        public int ID;
    }

    public class LawNode
    {
        //节点的名称
        public string name;
        //法律的层级
        public int Level;
        //父节点
        public int Parent;
        //具体法律条例的ID
        public int ID;
        //所选法律名称对应的编号
        public int LawID;
    }

    public class LawHelper
    {
        //获取带法律名称及对应的法律编号
        public List<Law> GetLaws()
        {
            //新建List
            List<Law> laws = new List<Law>();
            //输入想关联的数据库的基本信息
            SqlConnection conn = new SqlConnection(@"server = DESKTOP-5SDB4D4;Initial Catalog= test;User ID=sa;Password=lmz123LMZ");
            //打开数据库连接
            conn.Open();
            //sql语句
            string str = " select * from [test].[dbo].[TRaw] where c1 = 0";
            //创建命令对象
            SqlCommand lo_cmd = conn.CreateCommand();
            lo_cmd.CommandText = str;
            SqlDataReader dtr = lo_cmd.ExecuteReader();
            while (dtr.Read())
            {
                //实例化Law类
                Law law = new Law();
                //法律名称
                law.Name = (string)dtr["c3"];
                //法律代码
                law.ID = (int)dtr["c0"];
                //将值赋到List中
                laws.Add(law);
            }
            dtr.Close();
            conn.Close();
            return laws;
        }

        //获取法律条文的位置
        public List<LawNode> GetLawNodes(Law law)
        {
            //新建List存放对应的值
            List<LawNode> lawslocation = new List<LawNode>();
            SqlConnection conn = new SqlConnection(@"server = DESKTOP-5SDB4D4;Initial Catalog= test;User ID=sa;Password=lmz123LMZ");
            //打开数据库连接
            conn.Open();
            //sql语句
            string str = " select * from [test].[dbo].[TRaw] where c0 =" + law.ID;
            //创建命令对象
            SqlCommand lo_cmd = conn.CreateCommand();
            lo_cmd.CommandText = str;
            SqlDataReader dtr = lo_cmd.ExecuteReader();
            while (dtr.Read())
            {
                //实例化LawNode类
                LawNode lawNode = new LawNode();
                //层级
                lawNode.Level = (int)dtr["c4"];
                //法律名称代码
                lawNode.LawID = (int)dtr["c0"];
                //父节点
                lawNode.Parent = (int)dtr["c1"];
                //法律名称代码
                lawNode.ID = (int)dtr["c2"];
                //将值赋到List中
                lawslocation.Add(lawNode);
            }
            dtr.Close();
            conn.Close();
            return lawslocation;
        }

        //获取具体的法律条文
        public string GetLawText(LawNode lawnode)
        {

            //获取到法律的层级
            int level = lawnode.Level;
            //获取到法律的编号
            int ID = lawnode.ID;
            //获取法律名称的ID
            int LawID = lawnode.LawID;

            string lawItem = "";

            SqlConnection conn = new SqlConnection(@"server = DESKTOP-5SDB4D4;Initial Catalog= test;User ID=sa;Password=lmz123LMZ");
            //打开数据库连接
            conn.Open();
            //sql语句
            string str = "select * from [test].[dbo].[TRaw] where c4 =" + level + "and c0 = " + LawID + "and c2 = " + ID;
            //创建命令对象
            SqlCommand lo_cmd = conn.CreateCommand();
            lo_cmd.CommandText = str;
            SqlDataReader dtr = lo_cmd.ExecuteReader();
            while (dtr.Read())
            {
                if((int)dtr["c6"] == 1)
                {
                    //获取到具体的法律条例
                    lawItem = (string)dtr["c3"];
                }
            }
            dtr.Close();
            conn.Close();
            return lawItem;
        }
    }
}
View Code

Form1:

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;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        //实例化LawHelper类
        LawHelper lawhelper = new LawHelper();
        Law law = new Law();
        List<LawNode> lawslocation = new List<LawNode>();

        public Form1()
        {
            InitializeComponent();
            SetComboboxValue();
            comboBox1.SelectedIndex = 0;
        }

        //设置combobox1的值
        public void SetComboboxValue()
        {
            List<Law> laws = lawhelper.GetLaws();
            for (int i = 0;i< laws.Count; i++)
            {
                comboBox1.Items.AddRange(new object[] { laws[i].Name});
            }
        }

        //选择不同的法律法规
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            //获取到选中的法律名称
            int inx = comboBox1.SelectedIndex;
            //树节点清空
            treeView1.Nodes.Clear();
            List<Law> laws = lawhelper.GetLaws();
            law.Name = laws[inx].Name;
            law.ID = laws[inx].ID;
            lawslocation = lawhelper.GetLawNodes(law);
            int count = 1;

            //循环所有的节点
            for (int i = 0; i < lawslocation.Count; i++)
            {
                //当level层级为1时,生成所有的父节点
                if (lawslocation[i].Level == 1)
                {
                    TreeNode node = new TreeNode();
                    node.Text = "" + NumberToChinese(count) + "";
                    treeView1.Nodes.Add(node);
                    count++;
                    lawslocation[i].name = node.Text;
                }
                //当父节点ID与法律法规ID相吻合的时候,生成子节点
                //if ()
                //{

                //}

            }
        }

        //单击节点弹出消息
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode TN = e.Node;
            int inx = comboBox1.SelectedIndex;
            List<Law> laws = lawhelper.GetLaws();
            law.Name = laws[inx].Name;
            law.ID = laws[inx].ID;
            // List<LawNode> lawslocation = lawhelper.GetLawNodes(law);
            for (int i = 0; i < lawslocation.Count; i++)
            {
                LawNode getinfo = new LawNode();
                if (TN.Text == lawslocation[i].name)
                {
                    getinfo.ID = lawslocation[i].ID;
                    getinfo.LawID = lawslocation[i].LawID;
                    getinfo.Level = lawslocation[i].Level;
                    textBox1.Text = lawhelper.GetLawText(getinfo);
                    break;
                }
              
            }
        }

        //数字转中文
        public string NumberToChinese(int number)
        {
            string res;
            string str = number.ToString();
            string schar = str.Substring(0, 1);
            switch (schar)
            {
                case "1":
                    res = "";
                    break;
                case "2":
                    res = "";
                    break;
                case "3":
                    res = "";
                    break;
                case "4":
                    res = "";
                    break;
                case "5":
                    res = "";
                    break;
                case "6":
                    res = "";
                    break;
                case "7":
                    res = "";
                    break;
                case "8":
                    res = "";
                    break;
                case "9":
                    res = "";
                    break;
                default:
                    res = "";
                    break;
            }
            if (str.Length > 1)
            {
                switch (str.Length)
                {
                    case 2:
                    case 6:
                        res += "";
                        break;
                    case 3:
                    case 7:
                        res += "";
                        break;
                    case 4:
                        res += "";
                        break;
                    case 5:
                        res += "";
                        break;
                    default:
                        res += "";
                        break;
                }
                res += NumberToChinese(int.Parse(str.Substring(1, str.Length - 1)));
            }

            res = res.Replace("十零", "").Replace("一十", "");
            return res;
        }
    }
}
View Code

最后的效果如图所示:

 

posted @ 2017-06-20 10:47  StephenCurry  阅读(852)  评论(0编辑  收藏  举报