TreeView 树节点的处理
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using WinForm_CRUD.BLL; 11 using WinForm_CRUD.Model; 12 13 namespace WinForm_TestTree 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 22 MyORM_BLL myORM_BLL = new MyORM_BLL(); 23 24 /// <summary> 25 /// import Area 26 /// </summary> 27 /// <param name="sender"></param> 28 /// <param name="e"></param> 29 private void btnImportArea_Click(object sender, EventArgs e) 30 { 31 32 } 33 34 /// <summary> 35 /// 窗口加载时 省市联动 36 /// </summary> 37 /// <param name="sender"></param> 38 /// <param name="e"></param> 39 private void Form1_Load(object sender, EventArgs e) 40 { 41 LoadAlllNodes(); 42 } 43 44 /// <summary> 45 /// 加载所有节点 46 /// </summary> 47 private void LoadAlllNodes() 48 { 49 treeView1.Nodes.Clear(); 50 //根据PID=0查询省 51 List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=0"); 52 //for 遍历省 53 foreach (Object obj in list) 54 { 55 T_AREA area = (T_AREA)obj; 56 TreeNode nodePro = new TreeNode(area.NAME); 57 nodePro.Tag = area.ID; 58 //根据省ID 即PID查询市 遍历市 把市节点加入省(即需要省ID 和省节点递归) 59 RecursionCity(nodePro, area); 60 //把省节点加入treeview 61 treeView1.Nodes.Add(nodePro); 62 } 63 } 64 65 /// <summary> 66 /// 递归加载市和县 67 /// </summary> 68 /// <param name="node">上一级节点,比如省节点</param> 69 /// <param name="area">上一级对象,比如省,提供下一级的PID</param> 70 private void RecursionCity(TreeNode node, T_AREA area) 71 { 72 List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=" + area.ID); 73 foreach(Object obj in list) 74 { 75 T_AREA areaCity = (T_AREA)obj; 76 TreeNode nodeCity = new TreeNode(areaCity.NAME); 77 nodeCity.Tag = areaCity.ID; 78 RecursionCity(nodeCity, areaCity); //再次递归获得县节点 79 node.Nodes.Add(nodeCity); 80 } 81 } 82 83 /// <summary> 84 /// 删除节点 85 /// </summary> 86 /// <param name="sender"></param> 87 /// <param name="e"></param> 88 private void btnDeleteNode_Click(object sender, EventArgs e) 89 { 90 //判断是否选中 91 if (treeView1.SelectedNode == null) 92 { 93 MessageBox.Show("请先选中要删除的节点"); 94 return; 95 } 96 string name = treeView1.SelectedNode.Text; 97 int id = (Int32)treeView1.SelectedNode.Tag; 98 //提示是否删除 99 if(MessageBox.Show("您确定要删除节点"+name+"吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)!=DialogResult.OK) 100 { 101 return; 102 } 103 //根据ID删除 递归 104 //在根据ID删除省之前,先要删除省下面所有的市,删除每个市之前,先要删除市下面所有的县 105 RecursionDeleteCity(id); 106 myORM_BLL.DeleteModelById(typeof(T_AREA), id); 107 //重新加载数据 108 MessageBox.Show("删除成功"); 109 LoadAlllNodes(); 110 } 111 112 /// <summary> 113 /// 根据省ID删除下面的所有市,递归 根据市ID删除下面的所有县 114 /// </summary> 115 /// <param name="id"></param> 116 private void RecursionDeleteCity(int id) 117 { 118 //根据省ID查询所有市 119 List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=" + id); 120 //for市 121 foreach(Object obj in list) 122 { 123 T_AREA areaCity = (T_AREA)obj; 124 //根据市ID删除之前,先删除市下面的所有的县 125 RecursionDeleteCity(areaCity.ID); 126 myORM_BLL.DeleteModelById(typeof(T_AREA), areaCity.ID); 127 } 128 } 129 130 /// <summary> 131 /// 双击事件 进行编辑 132 /// </summary> 133 /// <param name="sender"></param> 134 /// <param name="e"></param> 135 private void treeView1_DoubleClick(object sender, EventArgs e) 136 { 137 //是否选中 138 if (treeView1.SelectedNode == null) 139 { 140 MessageBox.Show("请先选中要编辑的节点"); 141 return; 142 } 143 //获得选中的节点 144 TreeNode node = treeView1.SelectedNode; 145 //设置节点为可编辑状态 146 node.BeginEdit(); 147 //获得改变的文本 148 T_AREA area=new T_AREA(); 149 area.NAME = node.Text.Trim(); 150 area.ID = (Int32)node.Tag; 151 area.PID = node.Parent==null?0:(Int32)node.Parent.Tag; //父节点pid 152 //根据节点ID更新 153 bool flag = myORM_BLL.UpdateModel(area); 154 string msg = flag ? "更新成功" : "更新失败"; 155 MessageBox.Show(msg); 156 //刷新 157 LoadAlllNodes(); 158 } 159 160 private event EventHandler _evt; 161 private MyEventArg mea = new MyEventArg(); 162 163 /// <summary> 164 /// 新增节点 165 /// </summary> 166 /// <param name="sender"></param> 167 /// <param name="e"></param> 168 private void btnAddNew_Click(object sender, EventArgs e) 169 { 170 //是否选中父节点,如果不选中,默认父节点为0 171 TreeNode node = treeView1.SelectedNode; 172 int id = node == null ? 0 : (Int32)node.Tag; 173 //获得父节点ID,即PID 174 mea.area = new T_AREA(); 175 mea.area.PID = id; //新增传PID 176 AddNewAndEdit(1); 177 } 178 179 /// <summary> 180 /// 编辑节点 181 /// </summary> 182 /// <param name="sender"></param> 183 /// <param name="e"></param> 184 private void btnEdit_Click(object sender, EventArgs e) 185 { 186 //是否选中 187 if (treeView1.SelectedNode == null) 188 { 189 MessageBox.Show("请先选中要编辑的节点"); 190 return; 191 } 192 //获得节点ID 193 int id = (Int32)treeView1.SelectedNode.Tag; 194 mea.area = new T_AREA(); 195 mea.area.ID = id; //编辑传ID 196 mea.area.PID = treeView1.SelectedNode.Parent == null ? 0 : (Int32)treeView1.SelectedNode.Parent.Tag; 197 AddNewAndEdit(2); 198 } 199 200 /// <summary> 201 /// 编辑和新增 202 /// </summary> 203 /// <param name="p"></param> 204 private void AddNewAndEdit(int p) 205 { 206 mea.Tp = p; 207 Form2 f2 = new Form2(); 208 //注册事件 209 this._evt += new EventHandler(f2.SetText); 210 if(this._evt!=null) 211 { 212 this._evt(this, mea); 213 } 214 if (f2.ShowDialog() == DialogResult.OK) 215 { 216 f2.Close(); 217 LoadAlllNodes(); 218 } 219 else if (f2.ShowDialog() == DialogResult.Cancel) 220 { 221 f2.Close(); 222 } 223 } 224 } 225 public class MyEventArg : EventArgs 226 { 227 /// <summary> 228 /// 1 新增,2 编辑 229 /// </summary> 230 public int Tp { get; set; } 231 /// <summary> 232 /// 节点实例 233 /// </summary> 234 public T_AREA area { get; set; } 235 } 236 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.IO; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 using WinForm_CRUD.BLL; 12 using WinForm_CRUD.Model; 13 14 namespace WinForm_TestTree 15 { 16 public partial class Form1 : Form 17 { 18 public Form1() 19 { 20 InitializeComponent(); 21 } 22 23 MyORM_BLL myORM_BLL = new MyORM_BLL(); 24 25 /// <summary> 26 /// import Area 27 /// </summary> 28 /// <param name="sender"></param> 29 /// <param name="e"></param> 30 private void btnImportArea_Click(object sender, EventArgs e) 31 { 32 33 } 34 35 /// <summary> 36 /// 窗口加载时 省市联动 37 /// </summary> 38 /// <param name="sender"></param> 39 /// <param name="e"></param> 40 private void Form1_Load(object sender, EventArgs e) 41 { 42 LoadAlllNodes(); 43 } 44 45 /// <summary> 46 /// 加载所有节点 47 /// </summary> 48 private void LoadAlllNodes() 49 { 50 treeView1.Nodes.Clear(); 51 //根据PID=0查询省 52 List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=0"); 53 //for 遍历省 54 foreach (Object obj in list) 55 { 56 T_AREA area = (T_AREA)obj; 57 TreeNode nodePro = new TreeNode(area.NAME); 58 nodePro.Tag = area.ID; 59 //根据省ID 即PID查询市 遍历市 把市节点加入省(即需要省ID 和省节点递归) 60 RecursionCity(nodePro, area); 61 //把省节点加入treeview 62 treeView1.Nodes.Add(nodePro); 63 } 64 } 65 66 /// <summary> 67 /// 递归加载市和县 68 /// </summary> 69 /// <param name="node">上一级节点,比如省节点</param> 70 /// <param name="area">上一级对象,比如省,提供下一级的PID</param> 71 private void RecursionCity(TreeNode node, T_AREA area) 72 { 73 List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=" + area.ID); 74 foreach(Object obj in list) 75 { 76 T_AREA areaCity = (T_AREA)obj; 77 TreeNode nodeCity = new TreeNode(areaCity.NAME); 78 nodeCity.Tag = areaCity.ID; 79 RecursionCity(nodeCity, areaCity); //再次递归获得县节点 80 node.Nodes.Add(nodeCity); 81 } 82 } 83 84 /// <summary> 85 /// 删除节点 86 /// </summary> 87 /// <param name="sender"></param> 88 /// <param name="e"></param> 89 private void btnDeleteNode_Click(object sender, EventArgs e) 90 { 91 //判断是否选中 92 if (treeView1.SelectedNode == null) 93 { 94 MessageBox.Show("请先选中要删除的节点"); 95 return; 96 } 97 string name = treeView1.SelectedNode.Text; 98 int id = (Int32)treeView1.SelectedNode.Tag; 99 //提示是否删除 100 if(MessageBox.Show("您确定要删除节点"+name+"吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)!=DialogResult.OK) 101 { 102 return; 103 } 104 //根据ID删除 递归 105 //在根据ID删除省之前,先要删除省下面所有的市,删除每个市之前,先要删除市下面所有的县 106 RecursionDeleteCity(id); 107 myORM_BLL.DeleteModelById(typeof(T_AREA), id); 108 //重新加载数据 109 MessageBox.Show("删除成功"); 110 LoadAlllNodes(); 111 } 112 113 /// <summary> 114 /// 根据省ID删除下面的所有市,递归 根据市ID删除下面的所有县 115 /// </summary> 116 /// <param name="id"></param> 117 private void RecursionDeleteCity(int id) 118 { 119 //根据省ID查询所有市 120 List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=" + id); 121 //for市 122 foreach(Object obj in list) 123 { 124 T_AREA areaCity = (T_AREA)obj; 125 //根据市ID删除之前,先删除市下面的所有的县 126 RecursionDeleteCity(areaCity.ID); 127 myORM_BLL.DeleteModelById(typeof(T_AREA), areaCity.ID); 128 } 129 } 130 131 /// <summary> 132 /// 双击事件 进行编辑 133 /// </summary> 134 /// <param name="sender"></param> 135 /// <param name="e"></param> 136 private void treeView1_DoubleClick(object sender, EventArgs e) 137 { 138 //是否选中 139 if (treeView1.SelectedNode == null) 140 { 141 MessageBox.Show("请先选中要编辑的节点"); 142 return; 143 } 144 //获得选中的节点 145 TreeNode node = treeView1.SelectedNode; 146 //设置节点为可编辑状态 147 node.BeginEdit(); 148 //获得改变的文本 149 T_AREA area=new T_AREA(); 150 area.NAME = node.Text.Trim(); 151 area.ID = (Int32)node.Tag; 152 area.PID = node.Parent==null?0:(Int32)node.Parent.Tag; //父节点pid 153 //根据节点ID更新 154 bool flag = myORM_BLL.UpdateModel(area); 155 string msg = flag ? "更新成功" : "更新失败"; 156 MessageBox.Show(msg); 157 //刷新 158 LoadAlllNodes(); 159 } 160 161 private event EventHandler _evt; 162 private MyEventArg mea = new MyEventArg(); 163 164 /// <summary> 165 /// 新增节点 166 /// </summary> 167 /// <param name="sender"></param> 168 /// <param name="e"></param> 169 private void btnAddNew_Click(object sender, EventArgs e) 170 { 171 //是否选中父节点,如果不选中,默认父节点为0 172 TreeNode node = treeView1.SelectedNode; 173 int id = node == null ? 0 : (Int32)node.Tag; 174 //获得父节点ID,即PID 175 mea.area = new T_AREA(); 176 mea.area.PID = id; //新增传PID 177 AddNewAndEdit(1); 178 } 179 180 /// <summary> 181 /// 编辑节点 182 /// </summary> 183 /// <param name="sender"></param> 184 /// <param name="e"></param> 185 private void btnEdit_Click(object sender, EventArgs e) 186 { 187 //是否选中 188 if (treeView1.SelectedNode == null) 189 { 190 MessageBox.Show("请先选中要编辑的节点"); 191 return; 192 } 193 //获得节点ID 194 int id = (Int32)treeView1.SelectedNode.Tag; 195 mea.area = new T_AREA(); 196 mea.area.ID = id; //编辑传ID 197 mea.area.PID = treeView1.SelectedNode.Parent == null ? 0 : (Int32)treeView1.SelectedNode.Parent.Tag; 198 AddNewAndEdit(2); 199 } 200 201 /// <summary> 202 /// 编辑和新增 203 /// </summary> 204 /// <param name="p"></param> 205 private void AddNewAndEdit(int p) 206 { 207 mea.Tp = p; 208 Form2 f2 = new Form2(); 209 //注册事件 210 this._evt += new EventHandler(f2.SetText); 211 if(this._evt!=null) 212 { 213 this._evt(this, mea); 214 } 215 if (f2.ShowDialog() == DialogResult.OK) 216 { 217 f2.Close(); 218 LoadAlllNodes(); 219 } 220 else if (f2.ShowDialog() == DialogResult.Cancel) 221 { 222 f2.Close(); 223 } 224 } 225 226 /// <summary> 227 /// 加载 制定目录中的所有文件及目录 228 /// </summary> 229 /// <param name="sender"></param> 230 /// <param name="e"></param> 231 private void btnLoadAllDirectories_Click(object sender, EventArgs e) 232 { 233 //加载根目录,创建节点 234 DirectoryInfo dir = new DirectoryInfo(@"G:TianYu"); 235 TreeNode node = new TreeNode(dir.Name); 236 //加入节点集合之前 需要先获得子目录,并都加入根节点 递归 237 RecursionAddDirectory(node, dir); 238 //加入treeview 239 treeviewDirectories.Nodes.Add(node); 240 } 241 242 /// <summary> 243 /// 递归 目录dir下的子目录和文件加入节点node的节点集合中 244 /// </summary> 245 /// <param name="node"></param> 246 /// <param name="dir"></param> 247 private void RecursionAddDirectory(TreeNode node, DirectoryInfo dir) 248 { 249 //获得目录dir下的所有字目录,加入node的节点集合,在加入节点node之前需要递归获取当前的子目录和文件 250 DirectoryInfo[] dirs = dir.GetDirectories(); 251 foreach(DirectoryInfo direct in dirs) 252 { 253 TreeNode nodeDir = new TreeNode(direct.Name); 254 RecursionAddDirectory(nodeDir, direct); //递归 direct目录下的子目录和文件 加入nodeDir下的节点集合 255 node.Nodes.Add(nodeDir); 256 } 257 //获得目录dir下的所有子文件,加入node的节点集合 258 FileInfo[] files = dir.GetFiles(); 259 foreach(FileInfo file in files) 260 { 261 TreeNode nodeFile = new TreeNode(file.Name); 262 node.Nodes.Add(nodeFile); 263 } 264 } 265 } 266 public class MyEventArg : EventArgs 267 { 268 /// <summary> 269 /// 1 新增,2 编辑 270 /// </summary> 271 public int Tp { get; set; } 272 /// <summary> 273 /// 节点实例 274 /// </summary> 275 public T_AREA area { get; set; } 276 } 277 }