C# winform 自定义TreeView 双模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# winform 自定义TreeView 双模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2804字,纯文字阅读大概需要5分钟。
内容图文
![C# winform 自定义TreeView 双模式](/upload/InfoBanner/zyjiaocheng/854/8efd80b76f7b49a7a47de2f33c210f03.jpg)
NewTreeView.cs:
public class NewTreeView:TreeView
{
private bool isToggleIcon;//是否是切换图标模式
protected Font STFont;//二级和三级节点的字体
public bool IsToggleIcon
{
get
{
return isToggleIcon;
}
set
{
isToggleIcon = value;
}
}
public ZHTreeView()
{
this.isToggleIcon = true;
this.ShowLines = false;
this.FullRowSelect = true;
this.ShowPlusMinus = false;
this.ItemHeight = 32;
this.Font = new Font("宋体", 16, FontStyle.Bold);
this.STFont = new Font("宋体", 14, FontStyle.Regular);
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
//节点选中后触发
if (false == isToggleIcon)//图标分级效果
{
e.Node.SelectedImageIndex = e.Node.Level;//当前节点
TreeNodeCollection nodes = e.Node.Nodes;
foreach (TreeNode node in nodes)
{
node.ImageIndex = node.Level; //子节点
}
}
}
protected override void OnAfterExpand(TreeViewEventArgs e)
{
//节点展开后触发
if (true == isToggleIcon)//图标切换效果
{
//e.Node为展开后下一级的父节点
//切换模式下image0为展开前的图标,image1为展开后的图标
e.Node.SelectedImageIndex = 1;//展开后的图标
//SelectedImageIndex为选中期间的image索引,ImageIndex为不选状态下的image索引
e.Node.ImageIndex = 1;
//展开后的下一级节点
TreeNodeCollection nodes = e.Node.Nodes;
foreach (TreeNode node in nodes)
{
node.ImageIndex = 0;//展开前的图标
if (0 == node.Nodes.Count)//展开后的下一级节点是叶子节点
{
node.SelectedImageIndex = 2;//叶子节点图标
node.ImageIndex = 2;
}
}
e.Node.BackColor = this.BackColor;
}
//展开后设置下一级字体
TreeNodeCollection newNodes = e.Node.Nodes;
foreach (TreeNode newNode in newNodes)
{
newNode.NodeFont = STFont;
}
}
protected override void OnAfterCollapse(TreeViewEventArgs e)
{
//节点折叠后触发
if (true == isToggleIcon)//图标切换效果
{
//e.Node为被折叠一级节点的父节点
//切换模式下image0为折叠后的图标,image1为折叠前的图标
e.Node.SelectedImageIndex = 0;//折叠后的图标
e.Node.ImageIndex = 0;
}
}
}
1、添加节点:
?for (int i = 0; i < 4; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? TreeNode node1 = treeView1.Nodes.Add("父亲" + i.ToString());
? ? ? ? ? ? ? ? for (int j = 0; j < 3; j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? TreeNode node2 = new TreeNode("儿子" + j.ToString());
? ? ? ? ? ? ? ? ? ? node1.Nodes.Add(node2);
? ? ? ? ? ? ? ? ? ? for (int p = 0; p < 2; p++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? TreeNode node3 = new TreeNode("孙子" + p.ToString());
? ? ? ? ? ? ? ? ? ? ? ? node2.Nodes.Add(node3);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
2、添加图标:
try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (Image.FromFile(@"..\..\Resources\tree_NodeCollaps.png") != null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? imageList.Images.Add(Image.FromFile(@"..\..\Resources\tree_NodeCollaps.png"));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? }?? ??? ?
?? ??? ??? ?treeView1.ImageList = imageList;?? ??
内容总结
以上是互联网集市为您收集整理的C# winform 自定义TreeView 双模式全部内容,希望文章能够帮你解决C# winform 自定义TreeView 双模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。