2012-03-22 4 views
0

이 트리 뷰가 있는데 부모가 선택되어 있으면 모든 자식을 선택하도록했습니다. 부양 자녀를 확인하는 또 다른 규칙이 있습니다. 문제는 다음과 같습니다. 어떤 자식이 검사되면 부모를 확인하려고하지만, 규칙 없이는 할 수없는 다른 규칙이 충돌합니다. 그래서 여기 내가 지금까지 코드했습니다되는 것 : 사전에자식 체크시 부모 검사

private void tvMorgan_AfterCheck(object sender, TreeViewEventArgs e) 
    { 
     //Check Children if parent checked 
     if (e.Node.Nodes.Count > 0) 
     { 
      TreeNode tnParent = e.Node; 
      if (tnParent.Checked) 
      { 
       foreach (TreeNode tnChild in tnParent.Nodes) 
       { 
        tnChild.Checked = true; 
       } 
      } 
      //Unchecked children if parent unchecked 
      else 
      { 
       foreach (TreeNode tnChild in tnParent.Nodes) 
       { 
        tnChild.Checked = false; 
       } 
      } 
     } 
     //If dependent node is selected, check the other two 
     else if (((e.Node.Text.Contains("BRL/EUR")) && (e.Node.Checked)) && (e.Node.Parent.Text.Contains("FWD"))) 
     { 
      TreeNode tnParent = e.Node.Parent; 

      foreach (TreeNode tn in tnParent.Nodes) 
      { 
       if (tn.Text.Contains("BRL/USD") || tn.Text.Contains("EUR/USD")) 
        tn.Checked = true; 
      } 
     } 
     //If one of the two necessary nodes are uncheked, then uncheck the dependent one 
     else if ((((e.Node.Text.Contains("BRL/USD")) || (e.Node.Text.Contains("EUR/USD"))) && (!e.Node.Checked)) && (e.Node.Parent.Text.Contains("FWD"))) 
     { 
      TreeNode tnParent = e.Node.Parent; 

      foreach (TreeNode tn in tnParent.Nodes) 
      { 
       if (tn.Text.Contains("BRL/EUR")) 
        tn.Checked = false; 
      } 
     } 
    } 

감사를

+1

찾고있는 것을 수행하는 방법을 정확하게 보여줍니다 문제를 격리하는 코드의 축소 버전을 제공 할 수 있습니다. 대신에 메서드 시작 부분에서 이벤트 처리기 AfterCheck를 비활성화하고 끝에 다시 활성화하려고한다고 언급 할 것입니다. 그러면 현재 메소드의 재귀 적 측면이 비활성화됩니다. – Slugart

+0

또한, 부모 - 자식 자동 선택 (질문 당)의 논리를 텍스트가 포함 된보다 하드 코딩 된 규칙에서 분리합니다. – payo

답변