菜单

C# 用面向对象的探究去编制程序

2019年4月14日 - mg娱乐场www4355com

先是新建三个窗体文件,搭建界面完成:

再新建八个函数,分别对应着界面上的combobox选项,左边树状图的Node的具体地方以及左侧textbox里的切切实实法规定条款文的来得:

编排第三个函数GetLaws(),获取到相应的法律法规的名称:

mg娱乐场www4355com 1mg娱乐场www4355com 2

mg娱乐场www4355com 3

View Code

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;
        }

LawHelper:

编写制定第三个和第六个函数,分别收获具体的法律条文的岗位以及出口最终的法律条文消息

再对应编写制定对应的函数代码,具体代码如下展现:

值得1提的是,小编壹起先将对象的实例放在了循环的外面,导致往List赋值的时候背后的值会将前方的值全体覆盖,化解方式正是在循环里实例化

再接上一篇博文,从前写的两篇博文就算完成了效率,可是和控件之间的粘性太大,注重于控件实行操作,所以那篇博文首要用面向对象的思维做八个德姆o,将逻辑层与体现层剥离开

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;
        }
    }
}

mg娱乐场www4355com 4

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

Form1:

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

mg娱乐场www4355com,末段的遵循如图所示:

在LawHelper里新建五个对象,分别寄存法律法规的名目编号和现实的职位:

View Code

 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;
    }

mg娱乐场www4355com 5mg娱乐场www4355com 6

mg娱乐场www4355com 7

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;
        }
    }
}
 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;
        }

 

新建一个类,命名称为LawHelper

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图