vb中的treeview控件如何使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb中的treeview控件如何使用相关的知识,希望对你有一定的参考价值。
在treeview控件中已添加完父节点和子节点 我想完成的操作是 选择父节点或子节点时 组合框中会显示我选择的节点的标题 而且在文本框中会显示我选择节点相对应的值
使用控件需要知道,要想获得树状结构的每个层次中的子文件夹,必须使用递归过程来完成,下面所示代码所执行的工作是:先把一个已经路径的文件夹添加到树状结构中,然后再遍历它的子文件夹:
'添加文件夹
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim i As Integer
Dim node As New TreeNode
'先添加本目录,从文件夹路径分析出文件夹名称
node.Text = Strings.Replace(strPath, strFatherPath & "\", "", , 1)
'为单个节点指定节点未被选中时显示的图标
node.ImageIndex = 1
'为单个节点指定节点被选中时显示的图标
node.SelectedImageIndex = 2
nodeFather.Nodes.Add(node)
Application.DoEvents()
Try
Dim str() As String = iDir.GetDirectories(strPath)
'遍历该目录的子文件夹
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), node)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
node = Nothing
End Sub
然后在按钮的Click事件中添加代码,用于添加树状结构的顶层以及调用遍历子文件的递归过程,详细代码如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim node As New TreeNode
'先把C盘添加到树中
TreeView1.Nodes.Clear()
node.ImageIndex = 0
node.Text = "本地磁盘 C:"
node.SelectedImageIndex = -1
TreeView1.Nodes.Add(node)
Dim i As Integer
'获取C:盘根目录下的文件夹
Dim str() As String = iDir.GetDirectories("C:\")
For i = 0 To str.GetUpperBound(0)
'调用遍历过程
AddDirectory("C:", str(i), node)
Next
node = Nothing
End Sub 参考技术A Private Sub Form_Load()
TreeView1.Nodes.Add , , "aa", "aa"
TreeView1.Nodes.Add "aa", tvwChild, "bb", "bb"
TreeView1.Nodes.Add "bb", tvwChild, "cc", "cc"
TreeView1.Nodes.Add "bb", tvwChild, "dd", "dd"
TreeView1.Nodes.Add "aa", tvwChild, "ee", "ee"
TreeView1.Nodes.Add "ee", tvwChild, "ff", "ff"
TreeView1.Nodes.Add , tvwChild, "gg", "gg"
Dim i As Integer
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes.Item(i).Expanded = True
TreeView1.Nodes.Item(i).Tag = String(100, i & "")
Combo1.AddItem TreeView1.Nodes.Item(i).Text
Next
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Combo1.ListIndex = Node.Index - 1
Text1.Text = Node.Tag
End Sub 参考技术B Private Sub Form_Load()
TreeView1.Nodes.Add , , "aa", "aa"
TreeView1.Nodes.Add "aa", tvwChild, "bb", "bb"
TreeView1.Nodes.Add "bb", tvwChild, "cc", "cc"
TreeView1.Nodes.Add "bb", tvwChild, "dd", "dd"
TreeView1.Nodes.Add "aa", tvwChild, "ee", "ee"
TreeView1.Nodes.Add "ee", tvwChild, "ff", "ff"
TreeView1.Nodes.Add , tvwChild, "gg", "gg"
Dim i As Integer
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes.Item(i).Expanded = True
TreeView1.Nodes.Item(i).Tag = String(100, i & "")
Combo1.AddItem TreeView1.Nodes.Item(i).Text
Next
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Combo1.ListIndex = Node.Index - 1
Text1.Text = Node.Tag
End Sub本回答被提问者和网友采纳
Winform中TreeView的常用属性和方法?
详细。有事例更好。
TreeView控件是APS.NET中的常用控件,现对其用法做一简单小结,具体内容如下:1.public TreeNode FindNode(string valuePath) ... 其中valuePath指节点的值路径 示例代码如下:
/// <summary>
/// 填充左侧的模块树
/// </summary>
/// <param name="tv_modules">树形控件</param>
/// <param name="selectedMID">选中的模块组号</param>
public void BuildLeftTree(TreeView tv_modules, int selectedMID)
//添加根节点
TreeNode rootTN = new TreeNode();
rootTN.Text = SysConfig.LEFTTREE_ROOT_TEXT;
rootTN.Value = SysConfig.LEFTTREE_ROOT_VALUE;
rootTN.ImageUrl = SysConfig.LEFTTREE_ROOT_IMAGEURL;
rootTN.NavigateUrl = SysConfig.LEFTTREE_ROOT_NAVIGATEURL;
rootTN.Target = SysConfig.LEFTTREE_ROOT_TARGET;
rootTN.PopulateOnDemand = false;
rootTN.SelectAction = TreeNodeSelectAction.None;
rootTN.Expand();
tv_modules.Nodes.Add(rootTN);
PlatformAccessor pa = new PlatformAccessor();
DataSet ds = pa.getCodeRow(6, 1);
DataTable dt = ds.Tables[0];
DataRow dr = dt.Rows[0];
string content = dr["CONTENTS"].ToString();
XmlNodeList nodes = _xmldoc.SelectNodes("/Root/MyModules/Item");
if (content == "1")
//添加“我的模块”节点
TreeNode mymodulesTN = new TreeNode();
mymodulesTN.Text = SysConfig.LEFTTREE_MYMODULES_TEXT;
mymodulesTN.Value = SysConfig.LEFTTREE_MYMODULES_VALUE;
mymodulesTN.ImageUrl = SysConfig.LEFTTREE_MYMODULES_IMAGEURL;
mymodulesTN.NavigateUrl = SysConfig.LEFTTREE_MYMODULES_NAVIGATEURL;
mymodulesTN.Target = SysConfig.LEFTTREE_MYMODULES_TARGET;
mymodulesTN.PopulateOnDemand = false;
mymodulesTN.SelectAction = TreeNodeSelectAction.None;
mymodulesTN.Expand();
rootTN.ChildNodes.Add(mymodulesTN);
//添加“我的模块”
foreach (XmlNode xmlnode in nodes)
_addOneTreeNode(mymodulesTN, xmlnode, false);
PlatformAccessor pfa = new PlatformAccessor();
DataRow row = pfa.getModuleData(selectedMID);
int mlevel = int.Parse(row["MLEVEL"].ToString());
int mtopid = int.Parse(row["MTOPID"].ToString());
if (mlevel == 1 && mtopid == -1)
XmlNodeList rootList = _xmldoc.SelectNodes("/Root/Item");
foreach (XmlNode xn in rootList)
_addOneTreeNode(rootTN, xn, false, selectedMID);
return;
else
int mid = selectedMID;
int[] mids = new int[mlevel];
for (int i = 0; i < mlevel; i++)
DataRow dr_Temp = pfa.getModuleData(mid);
mids[i] = int.Parse(dr_Temp["MID"].ToString());
mid = int.Parse(dr_Temp["MTOPID"].ToString());
int[] sort_mids = new int[mlevel];
for (int i = 0; i < mlevel; i++)
sort_mids[i] = mids[mlevel - 1 - i];
for (int i = 0; i < mlevel; i++)
if (i == 0)
XmlNodeList rootList = _xmldoc.SelectNodes("/Root/Item");
foreach (XmlNode xn in rootList)
_addOneTreeNode(rootTN, xn, false, sort_mids[i]);
else
string temp = "";
for (int k = 0; k < (i + 1); k++)
temp += "/" + sort_mids[k].ToString();
//string valuePath = "ROOT/1031/1036";
string valuePath = SysConfig.LEFTTREE_ROOT_VALUE + temp;
TreeNode treeNode = tv_modules.FindNode(valuePath);
treeNode.Expand();
参考技术A 请参见 仔细看对你以后有用
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.treeview_members.aspx
以上是关于vb中的treeview控件如何使用的主要内容,如果未能解决你的问题,请参考以下文章
delphi中的treeview控件如何取到选择节点的索引号?