在实际工作中用到的一些零碎的知识记录下来
1.拼接路径
有时需要调用路径,把某一个文件放在某处,拼接路径生成一个路径
放在执行目录下
string Path = Path.Combine(Application.StartupPath, @"XXX.XXX");Application.StartupPath就是DEBUG目录下
2.TreeView添加树节点
TreeView是比较常用的控件
TreeNode Rootnode = new TreeNode();//声明一个根节点 treeView.Nodes.Add(Rootnode); //TreeView加载根节点 TreeNode[] Chlidnodes = new TreeNode[n]; //声明n的子节点 Chlidnodes[n] = new TreeNode(); 生成一个新节点 Rootnode.Nodes.Add(Chlidnodes[n]); //加载
下面的代码只有一个TreeView控件的操作,其余的什么也没有,可以新建一个新的C#空工程,仅拖入一个TreeView控件测试与学习
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TreeViewAgain { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //初始化节点,根节点为Root,子节点为Chlid1,Chlid2 private void Form1_Load(object sender, EventArgs e) { TreeNode Root = new TreeNode(); Root.Text = "Root"; treeView1.Nodes.Add(Root); TreeNode[] ChlidNode = new TreeNode[2]; ChlidNode[0] = new TreeNode(); ChlidNode[0].Text = "Chlid1"; Root.Nodes.Add(ChlidNode[0]); ChlidNode[1] = new TreeNode(); ChlidNode[1].Text = "Chlid2"; Root.Nodes.Add(ChlidNode[1]); treeView1.ExpandAll();//展开 Root.Checked = true; SetChlid(Root); } //子节点逻辑判断,点击父节点,子节点的逻辑判断很简单,子节点的勾选状态与父节点一致即可 private void SetChlid(TreeNode Node) { foreach (TreeNode child in Node.Nodes) { child.Checked = Node.Checked; <span style="white-space:pre"> </span>//子节点状态等于父节点状态,父节点勾选,子节点全部勾选,父节点取消勾选,子节点全部取消勾选 SetChlid(child); //递归遍历子节点 } } //父节点逻辑判断,父节点逻辑判断略麻烦,先遍历所有子节点,如果至少有一个子节点勾选,则父节点勾选,子节点全部取消勾选,父节点取消勾选 private void SetParent(TreeNode Node) { if (Node.Parent != null) { if (Node.Checked) { Node.Parent.Checked = true; return; } else { foreach (TreeNode Brother in Node.Parent.Nodes) { if (Brother.Checked == false) continue; else return; } Node.Parent.Checked = false; } } } //CheckBox节点 private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action != TreeViewAction.Unknown) { SetChlid(e.Node); SetParent(e.Node); } } List<string> m_SubNode = new List<string>(); //存储所有子节点 List<string> m_SubCheckNode = new List<string>(); //存储所有勾选的子节点 List<string> m_SubUnCheckNode = new List<string>(); //<span style="font-family: Arial, Helvetica, sans-serif;">存储所有未勾选的子节点</span> //存储所有子节点 private List<string> Savenode(TreeNode Node) { foreach (TreeNode Chlid in Node.Nodes) { m_SubNode.Add(Chlid.Text); Savenode(Chlid); } return m_SubNode; } //存储未勾选子节点 private List<string> SaveUnchecknode(TreeNode Node) { foreach (TreeNode Chlid in Node.Nodes) { if (Chlid.Checked == false) { m_SubNode.Add(Chlid.Text); Savenode(Chlid); } } return m_SubUnCheckNode; } //存储勾选子节点 private List<string> Savechecknode(TreeNode Node) { foreach (TreeNode Chlid in Node.Nodes) { if (Chlid.Checked == true) { m_SubNode.Add(Chlid.Text); Savenode(Chlid); } } return m_SubCheckNode; } //点击节点 private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { m_SubNode.Clear(); m_SubCheckNode.Clear(); m_SubUnCheckNode.Clear(); Savenode(e.Node); Savechecknode(e.Node); SaveUnchecknode(e.Node); } } }