TreeView 树节点的处理
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;
using WinForm_CRUD.BLL;
using WinForm_CRUD.Model; namespace WinForm_TestTree
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} MyORM_BLL myORM_BLL = new MyORM_BLL(); /// <summary>
/// import Area
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImportArea_Click(object sender, EventArgs e)
{ } /// <summary>
/// 窗口加载时 省市联动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
LoadAlllNodes();
} /// <summary>
/// 加载所有节点
/// </summary>
private void LoadAlllNodes()
{
treeView1.Nodes.Clear();
//根据PID=0查询省
List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=0");
//for 遍历省
foreach (Object obj in list)
{
T_AREA area = (T_AREA)obj;
TreeNode nodePro = new TreeNode(area.NAME);
nodePro.Tag = area.ID;
//根据省ID 即PID查询市 遍历市 把市节点加入省(即需要省ID 和省节点递归)
RecursionCity(nodePro, area);
//把省节点加入treeview
treeView1.Nodes.Add(nodePro);
}
} /// <summary>
/// 递归加载市和县
/// </summary>
/// <param name="node">上一级节点,比如省节点</param>
/// <param name="area">上一级对象,比如省,提供下一级的PID</param>
private void RecursionCity(TreeNode node, T_AREA area)
{
List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=" + area.ID);
foreach(Object obj in list)
{
T_AREA areaCity = (T_AREA)obj;
TreeNode nodeCity = new TreeNode(areaCity.NAME);
nodeCity.Tag = areaCity.ID;
RecursionCity(nodeCity, areaCity); //再次递归获得县节点
node.Nodes.Add(nodeCity);
}
} /// <summary>
/// 删除节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeleteNode_Click(object sender, EventArgs e)
{
//判断是否选中
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请先选中要删除的节点");
return;
}
string name = treeView1.SelectedNode.Text;
int id = (Int32)treeView1.SelectedNode.Tag;
//提示是否删除
if(MessageBox.Show("您确定要删除节点"+name+"吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)!=DialogResult.OK)
{
return;
}
//根据ID删除 递归
//在根据ID删除省之前,先要删除省下面所有的市,删除每个市之前,先要删除市下面所有的县
RecursionDeleteCity(id);
myORM_BLL.DeleteModelById(typeof(T_AREA), id);
//重新加载数据
MessageBox.Show("删除成功");
LoadAlllNodes();
} /// <summary>
/// 根据省ID删除下面的所有市,递归 根据市ID删除下面的所有县
/// </summary>
/// <param name="id"></param>
private void RecursionDeleteCity(int id)
{
//根据省ID查询所有市
List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=" + id);
//for市
foreach(Object obj in list)
{
T_AREA areaCity = (T_AREA)obj;
//根据市ID删除之前,先删除市下面的所有的县
RecursionDeleteCity(areaCity.ID);
myORM_BLL.DeleteModelById(typeof(T_AREA), areaCity.ID);
}
} /// <summary>
/// 双击事件 进行编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_DoubleClick(object sender, EventArgs e)
{
//是否选中
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请先选中要编辑的节点");
return;
}
//获得选中的节点
TreeNode node = treeView1.SelectedNode;
//设置节点为可编辑状态
node.BeginEdit();
//获得改变的文本
T_AREA area=new T_AREA();
area.NAME = node.Text.Trim();
area.ID = (Int32)node.Tag;
area.PID = node.Parent==null?:(Int32)node.Parent.Tag; //父节点pid
//根据节点ID更新
bool flag = myORM_BLL.UpdateModel(area);
string msg = flag ? "更新成功" : "更新失败";
MessageBox.Show(msg);
//刷新
LoadAlllNodes();
} private event EventHandler _evt;
private MyEventArg mea = new MyEventArg(); /// <summary>
/// 新增节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAddNew_Click(object sender, EventArgs e)
{
//是否选中父节点,如果不选中,默认父节点为0
TreeNode node = treeView1.SelectedNode;
int id = node == null ? : (Int32)node.Tag;
//获得父节点ID,即PID
mea.area = new T_AREA();
mea.area.PID = id; //新增传PID
AddNewAndEdit();
} /// <summary>
/// 编辑节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEdit_Click(object sender, EventArgs e)
{
//是否选中
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请先选中要编辑的节点");
return;
}
//获得节点ID
int id = (Int32)treeView1.SelectedNode.Tag;
mea.area = new T_AREA();
mea.area.ID = id; //编辑传ID
mea.area.PID = treeView1.SelectedNode.Parent == null ? : (Int32)treeView1.SelectedNode.Parent.Tag;
AddNewAndEdit();
} /// <summary>
/// 编辑和新增
/// </summary>
/// <param name="p"></param>
private void AddNewAndEdit(int p)
{
mea.Tp = p;
Form2 f2 = new Form2();
//注册事件
this._evt += new EventHandler(f2.SetText);
if(this._evt!=null)
{
this._evt(this, mea);
}
if (f2.ShowDialog() == DialogResult.OK)
{
f2.Close();
LoadAlllNodes();
}
else if (f2.ShowDialog() == DialogResult.Cancel)
{
f2.Close();
}
}
}
public class MyEventArg : EventArgs
{
/// <summary>
/// 1 新增,2 编辑
/// </summary>
public int Tp { get; set; }
/// <summary>
/// 节点实例
/// </summary>
public T_AREA area { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinForm_CRUD.BLL;
using WinForm_CRUD.Model; namespace WinForm_TestTree
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} MyORM_BLL myORM_BLL = new MyORM_BLL(); /// <summary>
/// import Area
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImportArea_Click(object sender, EventArgs e)
{ } /// <summary>
/// 窗口加载时 省市联动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
LoadAlllNodes();
} /// <summary>
/// 加载所有节点
/// </summary>
private void LoadAlllNodes()
{
treeView1.Nodes.Clear();
//根据PID=0查询省
List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=0");
//for 遍历省
foreach (Object obj in list)
{
T_AREA area = (T_AREA)obj;
TreeNode nodePro = new TreeNode(area.NAME);
nodePro.Tag = area.ID;
//根据省ID 即PID查询市 遍历市 把市节点加入省(即需要省ID 和省节点递归)
RecursionCity(nodePro, area);
//把省节点加入treeview
treeView1.Nodes.Add(nodePro);
}
} /// <summary>
/// 递归加载市和县
/// </summary>
/// <param name="node">上一级节点,比如省节点</param>
/// <param name="area">上一级对象,比如省,提供下一级的PID</param>
private void RecursionCity(TreeNode node, T_AREA area)
{
List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=" + area.ID);
foreach(Object obj in list)
{
T_AREA areaCity = (T_AREA)obj;
TreeNode nodeCity = new TreeNode(areaCity.NAME);
nodeCity.Tag = areaCity.ID;
RecursionCity(nodeCity, areaCity); //再次递归获得县节点
node.Nodes.Add(nodeCity);
}
} /// <summary>
/// 删除节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeleteNode_Click(object sender, EventArgs e)
{
//判断是否选中
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请先选中要删除的节点");
return;
}
string name = treeView1.SelectedNode.Text;
int id = (Int32)treeView1.SelectedNode.Tag;
//提示是否删除
if(MessageBox.Show("您确定要删除节点"+name+"吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)!=DialogResult.OK)
{
return;
}
//根据ID删除 递归
//在根据ID删除省之前,先要删除省下面所有的市,删除每个市之前,先要删除市下面所有的县
RecursionDeleteCity(id);
myORM_BLL.DeleteModelById(typeof(T_AREA), id);
//重新加载数据
MessageBox.Show("删除成功");
LoadAlllNodes();
} /// <summary>
/// 根据省ID删除下面的所有市,递归 根据市ID删除下面的所有县
/// </summary>
/// <param name="id"></param>
private void RecursionDeleteCity(int id)
{
//根据省ID查询所有市
List<Object> list = myORM_BLL.SelectModelByField(typeof(T_AREA), "PID=" + id);
//for市
foreach(Object obj in list)
{
T_AREA areaCity = (T_AREA)obj;
//根据市ID删除之前,先删除市下面的所有的县
RecursionDeleteCity(areaCity.ID);
myORM_BLL.DeleteModelById(typeof(T_AREA), areaCity.ID);
}
} /// <summary>
/// 双击事件 进行编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_DoubleClick(object sender, EventArgs e)
{
//是否选中
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请先选中要编辑的节点");
return;
}
//获得选中的节点
TreeNode node = treeView1.SelectedNode;
//设置节点为可编辑状态
node.BeginEdit();
//获得改变的文本
T_AREA area=new T_AREA();
area.NAME = node.Text.Trim();
area.ID = (Int32)node.Tag;
area.PID = node.Parent==null?:(Int32)node.Parent.Tag; //父节点pid
//根据节点ID更新
bool flag = myORM_BLL.UpdateModel(area);
string msg = flag ? "更新成功" : "更新失败";
MessageBox.Show(msg);
//刷新
LoadAlllNodes();
} private event EventHandler _evt;
private MyEventArg mea = new MyEventArg(); /// <summary>
/// 新增节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAddNew_Click(object sender, EventArgs e)
{
//是否选中父节点,如果不选中,默认父节点为0
TreeNode node = treeView1.SelectedNode;
int id = node == null ? : (Int32)node.Tag;
//获得父节点ID,即PID
mea.area = new T_AREA();
mea.area.PID = id; //新增传PID
AddNewAndEdit();
} /// <summary>
/// 编辑节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEdit_Click(object sender, EventArgs e)
{
//是否选中
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请先选中要编辑的节点");
return;
}
//获得节点ID
int id = (Int32)treeView1.SelectedNode.Tag;
mea.area = new T_AREA();
mea.area.ID = id; //编辑传ID
mea.area.PID = treeView1.SelectedNode.Parent == null ? : (Int32)treeView1.SelectedNode.Parent.Tag;
AddNewAndEdit();
} /// <summary>
/// 编辑和新增
/// </summary>
/// <param name="p"></param>
private void AddNewAndEdit(int p)
{
mea.Tp = p;
Form2 f2 = new Form2();
//注册事件
this._evt += new EventHandler(f2.SetText);
if(this._evt!=null)
{
this._evt(this, mea);
}
if (f2.ShowDialog() == DialogResult.OK)
{
f2.Close();
LoadAlllNodes();
}
else if (f2.ShowDialog() == DialogResult.Cancel)
{
f2.Close();
}
} /// <summary>
/// 加载 制定目录中的所有文件及目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLoadAllDirectories_Click(object sender, EventArgs e)
{
//加载根目录,创建节点
DirectoryInfo dir = new DirectoryInfo(@"G:\TianYu");
TreeNode node = new TreeNode(dir.Name);
//加入节点集合之前 需要先获得子目录,并都加入根节点 递归
RecursionAddDirectory(node, dir);
//加入treeview
treeviewDirectories.Nodes.Add(node);
} /// <summary>
/// 递归 目录dir下的子目录和文件加入节点node的节点集合中
/// </summary>
/// <param name="node"></param>
/// <param name="dir"></param>
private void RecursionAddDirectory(TreeNode node, DirectoryInfo dir)
{
//获得目录dir下的所有字目录,加入node的节点集合,在加入节点node之前需要递归获取当前的子目录和文件
DirectoryInfo[] dirs = dir.GetDirectories();
foreach(DirectoryInfo direct in dirs)
{
TreeNode nodeDir = new TreeNode(direct.Name);
RecursionAddDirectory(nodeDir, direct); //递归 direct目录下的子目录和文件 加入nodeDir下的节点集合
node.Nodes.Add(nodeDir);
}
//获得目录dir下的所有子文件,加入node的节点集合
FileInfo[] files = dir.GetFiles();
foreach(FileInfo file in files)
{
TreeNode nodeFile = new TreeNode(file.Name);
node.Nodes.Add(nodeFile);
}
}
}
public class MyEventArg : EventArgs
{
/// <summary>
/// 1 新增,2 编辑
/// </summary>
public int Tp { get; set; }
/// <summary>
/// 节点实例
/// </summary>
public T_AREA area { get; set; }
}
}
树节点的处理