Winform中TreeView的常用属性和方法?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于Winform中TreeView的常用属性和方法?的主要内容,如果未能解决你的问题,请参考以下文章

C# winform treeview 节点展开状态的图标设置

C# winform treeview 节点展开状态的图标设置

c#的winform TreeView控件中,展开不同的节点,右边显示不同的内容,该怎么做啊,请帮帮我吧,谢谢~

Winform中Treeview控件失去焦点,如何将选择的节点还设置为高亮显示?

Winform 进程线程treeview

WinForm控件TreeView 只部分节点显示 CheckBox