街道管理

写一个小程序

具体效果如下

第一步建类

Aderss类

 public class Address
   {
       private string street;//街道
       private string jwh;//居委会
       private string floornum;//楼号
          public string Street
       {
           get { return street; }
           set { street = value; }
       }

       public string Jwh
       {
           get { return jwh; }
           set { jwh = value; }
       }

       public string Floornum
       {
           get { return floornum; }
           set { floornum = value; }
       }

      
   }

Commom类(主要用于解析XML文件)

public class Common
    {
        //解析XML文件,并且将解析的Xml文件内容加载到集合中
      //public   List<Address> list=new List<Address>();  key:
       public  Dictionary<string,RoomAddress> list=new Dictionary<string, RoomAddress>(); 
       public  void ReadDataFromXML()
        {
           XmlDocument doc=new XmlDocument();
           doc.Load("Address.xml");
           XmlNode root = doc.DocumentElement;
           foreach (XmlNode item in root.ChildNodes)
           {
               //创建一个地址对象
               //Address address=new Address();
               //address.Street = root.Attributes["name"].InnerText;
               //address.Jwh = item.Attributes["name"].InnerText;
              string street = root.Attributes["name"].InnerText;
              string jwh= item.Attributes["name"].InnerText;
               foreach (XmlNode child in item.ChildNodes)
               {
                   string floornum = child.Attributes["name"].InnerText;
                   foreach (XmlNode node in child.ChildNodes)//child代表的是楼号,一个楼层上有多个房间
                   {
                       //每一个node都是一个房间
                       Address address = new Address();
                       address.Street = street;
                       address.Jwh = jwh;
                       address.Floornum = floornum;
                       RoomAddress roomAddress=new RoomAddress();
                       //RoomAddress类中才能反映出当前房间的具体位置信息
                       //包括房间号,所在楼层,所在的校区,所在的街道
                       //华清嘉园    B 1005  101
                       //羊城花园  +A1004    101
                       roomAddress.Roomnum =node.InnerText;
                       roomAddress.Address = address;
                       list.Add(address.Jwh+address.Floornum + roomAddress.Roomnum, roomAddress);
                       
                   }
               }
              
           }
        }
    }

RoomAdress类

public class RoomAddress
   {
       public RoomAddress()
       {
           address=new Address();
       }

       private string roomnum;
       private Address address;
       public string Roomnum
       {
           get { return roomnum; }
           set { roomnum = value; }
       }

       public Address Address
       {
           get { return address; }
           set { address = value; }
       }
   }

在主主窗体创建方法用于调用XML文件并且与Treeview绑定

  common.ReadDataFromXML();
            TreeNode root=new TreeNode();
            root.Text = "石牌街道";
            tvShow.Nodes.Add(root);
            string jwh= "";
            TreeNode tn = null;
            TreeNode tnfloor = null;
            string floor = "";
            TreeNode child = null;
            foreach (RoomAddress address in common.list.Values)//RoomAddress
            {
                if (floor != address.Address.Floornum)
                {
                    if (jwh != address.Address.Jwh)
                    {
                        tn = new TreeNode();
                        tn.Text = address.Address.Jwh;
                        root.Nodes.Add(tn);
                    }
                    tnfloor = new TreeNode();
                    tnfloor.Text = address.Address.Floornum;
                    tn.Nodes.Add(tnfloor);
                }
                child = new TreeNode();
                child.Text = address.Roomnum;
                tnfloor.Nodes.Add(child);
                jwh = address.Address.Jwh;
                floor = address.Address.Floornum;         
            }

TreeView的选中事件并且将将节点上的文字赋值到TextBox控件上

 //获取当前选中的节点

            TreeNode selectNode = tvShow.SelectedNode;
            if (selectNode!=null&&selectNode.Level==3)
            {
                string parentText = selectNode.Parent.Text;
                string parentandparentText = selectNode.Parent.Parent.Text;
                string treeText = parentandparentText + parentText + selectNode.Text;
                txtJWH.Text = common.list[treeText].Address.Jwh;
                txtStreet.Text = common.list[treeText].Address.Street;
                txtRoomNum.Text = common.list[treeText].Roomnum;
                txtFloor.Text = common.list[treeText].Address.Floornum;
            }
           

这个小程序还有很多不足

 

posted @ 2018-01-26 15:59  小白装大神  阅读(166)  评论(0编辑  收藏  举报