English 中文(简体)
• 如何只检查一个选项
原标题:How to make Treeview check only one option

我有一个树木概览和检查箱。 我想的是,在整个树木调查中只选定一个核对箱。 我如何能够这样做?

FYI:树图分三个深度。

最佳回答

这样做的最简单方式是给你们的树木在Check活动打上甚至手。 在这一手里,除刚刚检查的节点外,你可以检查所有节点:

void node_AfterCheck(object sender, TreeViewEventArgs e) {
    // only do it if the node became checked:
    if (e.Node.Checked) {
        // for all the nodes in the tree...
        foreach (TreeNode cur_node in e.Node.TreeView.Nodes) {
            // ... which are not the freshly checked one...
            if (cur_node != e.Node) {
                // ... uncheck them
                cur_node.Checked = false;
            }
        }
    }
}

Should work (didn t try)

问题回答

这是一个老的职位,尽管在我的案件中没有任何解决办法。

So I did as follows,

private TreeNode uncheck_treeview(TreeView treeView, TreeNode treeNode, TreeViewEventH以及ler e)
{
    treeView.AfterCheck -= e;

    foreach (TreeNode node in treeView.Nodes)
    {
        uncheck_treenode_tree(node);
    }

    if (treeNode != null)
    {
        treeNode.Checked = true;
    }

    treeView.AfterCheck += e;

    return treeNode;
}

以及

private void uncheck_treenode(TreeNode treeNode)
{
    treeNode.Checked = false;
    foreach (TreeNode node in treeNode.Nodes)
    {
        uncheck_treenode_tree(node);
    }
}

以及

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    var checkedNode = uncheck_treeview_tree((TreeView) sender, e.Node, treeView1_AfterCheck);
    // further processing ...
}

请注意,这种方法防止<代码>StackOverflowException!

对他人有益的希望

www.un.org/Depts/DGACM/index_spanish.htm 我在审判中肯定会奏效。

       bool manualcheck = false;
      protected override void OnAfterCheck(TreeViewEventArgs e)
      {
                if (manualcheck) return;
                if (e.Node.Checked)
                {
                   if (Nodes.Count>0) UnCheckAll(Nodes[0]);
                   manualcheck = true;
                e.Node.Checked = true;
                manualcheck = false;
                }


        }


        void UnCheckAll(TreeNode node)
            {


                if (node != null)
                {

                    node.Checked = false;
                    foreach (TreeNode item in node.Nodes)
                    {
                        manualcheck = true;
                        item.Checked = false;
                        if (item.Nodes.Count > 0) UnCheckAll(item.Nodes[0]);
                    }


                    if (node.NextNode != null)
                        UnCheckAll(node.NextNode);

                }
                manualcheck = false;
            }

我也存在同样的问题,但 lo笑一切都给业绩带来昂贵的代价。

Here is my solution without looping trough all Nodes, but using a class property:

public partial class GuiDefault // not my whole class, just an example
{ 

TreeNode checkedNode = null;

    private void Tree_AfterCheck(object sender, TreeViewEventArgs e)
    {
        TreeNode checkedNodeVar = e.Node;

        if (checkedNodeVar.Checked)
                    {

                        if (checkedNode != null)
                        {
                            checkedNode.Checked = false; // set "checked" property of the last checked box to false
                        }

                        checkedNode = checkedNodeVar; // set the class property "checkedNode" to the currently checked Node
                    }
        else
                    {
                        checkedNode = null; // if current checked box gets unchecked again, also reset the class property "checkedNode"
                    }
    }

}

找到这一解决办法的唯一问题是,如果用户把同样的检查箱拖到快步,那么功能就会被正确地称作,它会用1点击的“检查”或“检查”盒子。

也许其他人可以增加一个固定点,以点击同一检查箱,在运行时间迅速。

// Upon node checked, uncheck all other nodes
private void tvAccountHierarchy_AfterCheck(object sender, TreeViewEventArgs e)
    {
        // only do it if the node became checked:
        if (e.Node.Checked)
        {   
            Uncheck_Treenodes(e.Node.TreeView.Nodes, e.Node);                
        }
    }

//Recursively iterate through all child nodes and uncheck all except the checkedNode
 private void Uncheck_Treenodes(TreeNodeCollection treeNodes, TreeNode checkedNode)
    {            
        // iterate through child nodes & uncheck
        foreach (TreeNode cur_node in treeNodes)
        {
            if (cur_node != checkedNode)
            {
                // ... uncheck them
                cur_node.Checked = false;
            }
            Uncheck_Treenodes(cur_node.Nodes, checkedNode);
        }
    }

我使用这一解决办法:here

页: 1

function client_OnTreeNodeChecked(event)
{
 var treeNode = event.srcElement || event.target ;
 if (treeNode.tagName == "INPUT" && treeNode.type == "checkbox")
  {
   if(treeNode.checked)
    {
     uncheckOthers(treeNode.id);
    }
  }
}

function uncheckOthers(id)
 {
  var elements = document.getElementsByTagName( input );
  // loop through all input elements in form
  for(var i = 0; i < elements.length; i++)
   {
    if(elements.item(i).type == "checkbox")
    {
     if(elements.item(i).id!=id)
     {
      elements.item(i).checked=false;
     }
    }
   }
  }

添加客户的代码 OnTreeNode to树园活动的检查功能

private void Page_PreRender(object sender, EventArgs e)
 {
  TreeView1.Attributes.Add("OnClick", "client_OnTreeNodeChecked(event)");
 }

NOTE: Read my post 如果树木在增订版内出现,如何与Ajax UpdatePanel 一道做javascript 工作

This solution which I shamelessly stole allows you to iterate over all nodes at all levels in one go.

这使得确保只选择一个节点容易:

    foreach (var node in resultsTree.GetAllNodes())
    {
        if (node != e.Node) node.Checked = false;
    }




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签