下面是我的递归方法,我想要在方法找到满足的条件后就跳出循环,但是试了几种方法还是没有成功。请大家帮帮忙,出出主意private void FocusHasPicNode(TreeListNodes nodes)
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
this.treeList1.FocusedNode = node;
}
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}
}
}
}
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
this.treeList1.FocusedNode = node;
}
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}
}
}
}
private void FocusHasPicNode(TreeListNodes nodes)
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if (!(!string.IsNullOrEmpty(sIsRead)) && !(sIsRead.Equals("1")) && !(!string.IsNullOrEmpty(sPicKind)) && !(!sPicKind.StartsWith("a")) && !(!sPicKind.StartsWith("b")) && !(node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
//this.treeList1.FocusedNode = node; if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}
} }
}
你好,我的意思是在这个递归循环中满足我条件的节点有很多,但是我只想在找到第一个节点后就退出,但是我用break和return都无法实现这个目的
{
//dosomething
if(i++ == 100)
break;
}
//你把方法体改成有返回值的 在调用FocusHasPicNode()这方法那里为treeList1.FocusedNode赋值
//如treeList1.FocusedNode=FocusHasPicNode(node);
private DevExpress.XtraTreeList.Nodes.TreeListNode FocusHasPicNode(TreeListNodes nodes)
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
//this.treeList1.FocusedNode = node;
return node; }
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}
}
} }
private void FocusHasPicNode(TreeListNodes nodes)
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
this.treeList1.FocusedNode = node;
bState = false; //设置状态
}
else
{
if( bState )
{
if (node.Nodes.Count > 0 )
{
FocusHasPicNode(node.Nodes);
}
}
else
{
break;
}
}
}
}
设置个bool变量可能也不行,可以调试一下,当执行到break时的情况,可能跳出条件没有设好
bool bStatus = true;
private void FocusHasPicNode(TreeListNodes nodes)
{
if(!bStatus)
return;
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
this.treeList1.FocusedNode = node;
bStatus = false;
}
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}
}
}
}
return;
{
if(found != null)
return;
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
found = node;
return;
}
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes, found);
}
}
}
}