下面是我的递归方法,我想要在方法找到满足的条件后就跳出循环,但是试了几种方法还是没有成功。请大家帮帮忙,出出主意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);
                    }
                }
            }
       
        }

解决方案 »

  1.   

    把思路反过来,如果符合条件则继续,否则跳出
    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); 
                        }                     
                    }             } 
          
            }
      

  2.   

    mdq001
    你好,我的意思是在这个递归循环中满足我条件的节点有很多,但是我只想在找到第一个节点后就退出,但是我用break和return都无法实现这个目的
      

  3.   

    如果这样,设置一个bool变量,符合条件则设置bool为true,再else里判断bool是否为false,如果为false则递归方法,如果为true则不进入else语句块,自然不会递归方法了
      

  4.   

    int i=0;while(true)
    {
       //dosomething
       if(i++ == 100)
          break;
    }
      

  5.   


     //你把方法体改成有返回值的 在调用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);
                        }
                    }
                }        }
      

  6.   

    加一个静态的布尔变量:static bool bState = true;
    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;
                         }
                    }
                }
         
            }
      

  7.   

    看来只能是设置全局变量来控制了,return和break诗无法在符合第一次后就退出循环了的
      

  8.   

    如果break不行的话
    设置个bool变量可能也不行,可以调试一下,当执行到break时的情况,可能跳出条件没有设好
      

  9.   


    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); 
                        } 
                    } 
                } 
          
            }
      

  10.   

    利用全局变量来标记是否改结束然后
    return;
      

  11.   

    看起来lz的代码是遍历所有结点,找出所有的匹配。(当然,那就还需要传一个List进去,把找到的结点都加到这个列表里面)可是为什么lz要求中途退出呢?那你的代码逻辑肯定就不对了。那么建议添加一个新的参数这样应该就不需要全局参数了。毕竟全局参数是OO的敌人。        private void FocusHasPicNode(TreeListNodes nodes, TreeListNode found) 
            { 
                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); 
                        } 
                    } 
                } 
          
            }