【问题标题】:How to check whether the selected tree nodes are of same order?如何检查选定的树节点是否具有相同的顺序?
【发布时间】:2014-05-29 10:20:35
【问题描述】:

我正在设计一个树视图,它允许用户通过按 SHIFT、CTRL + 开始节点然后结束节点来选择节点范围。我的要求是仅当范围在树节点下时才选择节点。 (范围不应落在两个父节点之下)。如果用户从两个不同的父节点中选择 Node2,我可以检查 if(selected_node_1->Parent == selected_node_2->Parent)。但是如果用户选择 Node_A 和 Node_B,我如何检查所选的树节点是否在同一级别?

(请注意 Node_A 和 Node_B 没有父节点)。

【问题讨论】:

  • 如果其中一个节点没有父节点,那么它们永远无法匹配您的条件。轻松愉快。

标签: c# visual-c++ user-interface treeview


【解决方案1】:

试试这个扩展方法(C#):

  public static class TreeNodeExtensions {
    public static int Level(this TreeNode value) {
      if (Object.ReferenceEquals(null, value))
        throw new ArgumentNullException("value"); // <- or return 0 

      int result = 0;

      for (TreeNode node = value; node != null; node = node.Parent)
        result += 1;

      return result;
    }
  }

  ... 

  TreeNode node1 = ...
  TreeNode node2 = ...

  if (node1.Level() != node2.Level()) {
    ...
  } 

【讨论】:

    【解决方案2】:

    他们没有称为 root 的父级吗? 如果没有,您可以检查两者是否都有parent == null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 2012-06-10
      相关资源
      最近更新 更多