关于treeview 查找的问题 在treeview中进行关键字查找,输入关键字,然后点击“下一个”或者“上一个”按钮进行查找, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 up ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp 就是一个遍历吗,找到后break,然后下一个时,从当前节点继续遍历 递归算法给你一段“下一个”的代码,“上一个”你参考这个写 private TreeNode m_currNode = null; private void btnFindNext_Click(object sender, EventArgs e) { if (treeView1.Nodes.Count > 0) { if (m_currNode == null) m_currNode = treeView1.Nodes[0]; m_currNode = findNext("关键字", m_currNode); if (m_currNode != null) treeView1.SelectedNode = m_currNode; } } private TreeNode findNext(string strKeyword, TreeNode fromNode) { if (fromNode.Nodes.Count > 0) { foreach (TreeNode node in fromNode.Nodes) { if (node.Text == strKeyword) return node; return findNext(strKeyword, node); } } else { if (fromNode.NextNode != null) { if (fromNode.NextNode.Text == strKeyword) return fromNode.NextNode; return findNext(strKeyword, fromNode.NextNode); } else if (fromNode.Parent != null && fromNode.Parent.NextNode != null) { if (fromNode.Parent.NextNode.Text == strKeyword) return fromNode.Parent.NextNode; return findNext(strKeyword, fromNode.Parent.NextNode); } } return null; } 上一步,把上面代码的NextNode改成PreNode应该就可以,你试一下 就是一个遍历,找到后break,记录下当前节点,然后下一个时,从当前节点继续遍历 a b cj b ch b b你的代码,查找b只能找到第一个b a b cj a b ch b b 可以用treview.nodes.find方法试试,注意,里面的第一个参数是node的name,你可以第归,找到后,将其checked属性设置为true 我自己写的,已经在项目里面用过了,不过不是一个一个查,是自动查找所有 checkbox被勾选的项。给你参考。ps:其中FunctionInfo是我项目中用到的类,绑定在节点的Tag上。//获取tvw_Function中所有选中的节点信息 //没有树生成则返回null,树中无选中节点返回空IList private IList<FunctionInfo> GetCheckedNodes() { //检验tvw_Function是否有内容 if (0 == tvw_Function.Nodes.Count) { IList<String> logLines = new List<String>(); StackTrace st = new StackTrace(new StackFrame(true)); StackFrame sf = st.GetFrame(0); logLines.Add(sf.GetFileName()); logLines.Add(sf.GetMethod() + " | " + sf.GetFileLineNumber()); logLines.Add(" : tvw_Function has no content!"); Common.Common.WriteLog(logLines,false); return null; } IList<FunctionInfo> functionList = new List<FunctionInfo>(); TreeNode tnRoot , tnCurrent , tnCurrentPar; tnRoot = tvw_Function.Nodes["root"]; tnCurrentPar = tnRoot; tnCurrent = tnCurrentPar.FirstNode; //遍历tvw_Function获取被勾选的节点信息 while (tnCurrent != null && tnCurrent != tnRoot) { while (tnCurrent != null) { //若当前节点被勾选,且当前节点为有效功能节点(tag不为空),则将tag代表的功能加入返回功能列表 if ((true == tnCurrent.Checked) && (tnCurrent.Tag != null)) functionList.Add((FunctionInfo) tnCurrent.Tag); if (tnCurrent.Nodes.Count > 0) { //移动到下一层节点 tnCurrentPar = tnCurrent; tnCurrent = tnCurrent.FirstNode; } else if (tnCurrent != tnCurrentPar.LastNode) { //移动到本层下一个节点 tnCurrent = tnCurrent.NextNode; } else break; } //逐层回溯父节点 while (tnCurrent != tnRoot && tnCurrent == tnCurrentPar.LastNode) { tnCurrent = tnCurrentPar; tnCurrentPar = tnCurrentPar.Parent; } //回溯停止后移动到本层下一个节点 if (tnCurrent != tnRoot) tnCurrent = tnCurrent.NextNode; } return functionList; } a b n cj a b ch b b这样就不可以,查b a b n cj a b ch b b treeView1.Nodes.Find("关键字",true)[0].下一个(上一个) 不需要遍历整个树,TreeNode的Name属性其实就是关键字,创建TreeView的时候指定一下这个属性,然后就可以使用Nodes.Find方法搜索符合关键字的节点,得到的是一个TreeNode的数组,然后,遍历这个数组就可以了。 WPF与WinForm的抉择[转贴] 求正则表达式 GPS/GIS软件开发 c#做winform程序,操纵EXCEL模板,怎么让EXCEL成为当前活动程序,并打印预览? 怎么修改按钮的边框色,急急急急 有效性检查的问题 列名无效 请各位大虾帮帮忙! 请教,在线等候,马上给分! Devexpress中WinForm如何统一设置字体样式? WEB SERVICE 请教一个字符串问题!
给你一段“下一个”的代码,“上一个”你参考这个写
private TreeNode m_currNode = null;
private void btnFindNext_Click(object sender, EventArgs e)
{
if (treeView1.Nodes.Count > 0)
{
if (m_currNode == null)
m_currNode = treeView1.Nodes[0]; m_currNode = findNext("关键字", m_currNode);
if (m_currNode != null)
treeView1.SelectedNode = m_currNode;
}
}
private TreeNode findNext(string strKeyword, TreeNode fromNode)
{
if (fromNode.Nodes.Count > 0)
{
foreach (TreeNode node in fromNode.Nodes)
{
if (node.Text == strKeyword)
return node; return findNext(strKeyword, node);
}
}
else
{ if (fromNode.NextNode != null)
{
if (fromNode.NextNode.Text == strKeyword)
return fromNode.NextNode; return findNext(strKeyword, fromNode.NextNode);
}
else if (fromNode.Parent != null && fromNode.Parent.NextNode != null)
{
if (fromNode.Parent.NextNode.Text == strKeyword)
return fromNode.Parent.NextNode; return findNext(strKeyword, fromNode.Parent.NextNode);
}
} return null;
}
b
c
j
b
c
h
b
b
你的代码,查找b只能找到第一个b
a
b
c
j
a
b
c
h
b
b
//获取tvw_Function中所有选中的节点信息
//没有树生成则返回null,树中无选中节点返回空IList
private IList<FunctionInfo> GetCheckedNodes()
{
//检验tvw_Function是否有内容
if (0 == tvw_Function.Nodes.Count)
{
IList<String> logLines = new List<String>();
StackTrace st = new StackTrace(new StackFrame(true));
StackFrame sf = st.GetFrame(0);
logLines.Add(sf.GetFileName());
logLines.Add(sf.GetMethod() + " | " + sf.GetFileLineNumber());
logLines.Add(" : tvw_Function has no content!");
Common.Common.WriteLog(logLines,false);
return null;
} IList<FunctionInfo> functionList = new List<FunctionInfo>();
TreeNode tnRoot , tnCurrent , tnCurrentPar;
tnRoot = tvw_Function.Nodes["root"];
tnCurrentPar = tnRoot;
tnCurrent = tnCurrentPar.FirstNode; //遍历tvw_Function获取被勾选的节点信息
while (tnCurrent != null && tnCurrent != tnRoot)
{
while (tnCurrent != null)
{
//若当前节点被勾选,且当前节点为有效功能节点(tag不为空),则将tag代表的功能加入返回功能列表
if ((true == tnCurrent.Checked) && (tnCurrent.Tag != null))
functionList.Add((FunctionInfo) tnCurrent.Tag); if (tnCurrent.Nodes.Count > 0)
{
//移动到下一层节点
tnCurrentPar = tnCurrent;
tnCurrent = tnCurrent.FirstNode;
}
else if (tnCurrent != tnCurrentPar.LastNode)
{
//移动到本层下一个节点
tnCurrent = tnCurrent.NextNode;
}
else
break;
} //逐层回溯父节点
while (tnCurrent != tnRoot && tnCurrent == tnCurrentPar.LastNode)
{
tnCurrent = tnCurrentPar;
tnCurrentPar = tnCurrentPar.Parent;
} //回溯停止后移动到本层下一个节点
if (tnCurrent != tnRoot)
tnCurrent = tnCurrent.NextNode;
} return functionList; }
b
n
c
j
a
b
c
h
b
b
这样就不可以,查b
a
b
n
c
j
a
b
c
h
b
b