本文介绍了如何使用C#和Visual Studio 2013从Treeview控件一次选择多个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想从树视图中选择多个节点,并希望其文本出现在msgbox或控制台中。但我无法这样做。请有人帮帮我。? 从下面的答案添加 // 从数据库创建树 private void CreateTree(TreeNode n, int hdrID) { System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection( @ Data Source =(LocalDB)\ v11.0; AttachDbFilename = D:\Builder.mdf; MultipleActiveResultSets = True; Integrated Security = True; Connect Timeout = 30) ; con.Open(); SqlCommand cmd = new SqlCommand( SELECT Id,Title FROM Presentation WHERE Pid = + hdrID,con); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { TreeNode t = new TreeNode(rdr [ Title]。ToString()); CreateTree(t,Convert.ToInt16(rdr [ Id]。ToString() )); if (n == null ) { treeView1。 Nodes.Add(T); // t.ImageIndex = 0; } else { n.Nodes.Add(t); } } rdr.Close(); } 解决方案 如果您不希望每个项目旁边都有复选框,这可能对您有用。 br $> b $ b http://www.codeproject.com/Articles/8006/Multi-select-Treeview-control-v private void treeView1_AfterCheck( object sender,TreeViewEventArgs e) { if (e.Action!= TreeViewAction.Unknown) { 如果(忙) return ; busy = true ; 尝试 { TreeNode _node = e.Node; checkNodes(e.Node,e.Node.Checked); if (e.Node.Checked) { MessageBox.Show(e.Node.Text); } } 最后 { busy = 假; } } } private void checkNodes(TreeNode节点, bool check) { foreach (TreeNode child in node.Nodes) { if (child.Checked == true ) { MessageBox.Show(child 。文本); } // MessageBox.Show(child.Text); checkNodes(child,check); } } i want to select the multiple nodes from a treeview and want its text to be present in msgbox or console . but i am not able to do so. can anybody please help me.?Added from answer below//Create the tree from the database private void CreateTree(TreeNode n, int hdrID) { System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Builder.mdf;MultipleActiveResultSets = True;Integrated Security=True;Connect Timeout=30"); con.Open(); SqlCommand cmd = new SqlCommand("SELECT Id,Title FROM Presentation WHERE Pid=" + hdrID, con); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { TreeNode t = new TreeNode(rdr["Title"].ToString()); CreateTree(t, Convert.ToInt16(rdr["Id"].ToString())); if (n == null) { treeView1.Nodes.Add(t); //t.ImageIndex = 0; } else { n.Nodes.Add(t); } } rdr.Close(); } 解决方案 If you don't want checkboxes next to each item, this may be of use to you. http://www.codeproject.com/Articles/8006/Multi-select-Treeview-control-vprivate void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action != TreeViewAction.Unknown) { if (busy) return; busy = true; try { TreeNode _node = e.Node; checkNodes(e.Node, e.Node.Checked); if (e.Node.Checked) { MessageBox.Show(e.Node.Text); } } finally { busy = false; } } }private void checkNodes(TreeNode node, bool check) { foreach (TreeNode child in node.Nodes) { if (child.Checked == true) { MessageBox.Show(child.Text); } //MessageBox.Show(child.Text); checkNodes(child, check); } } 这篇关于如何使用C#和Visual Studio 2013从Treeview控件一次选择多个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 18:55