WinFrom中怎么通过点击Button获取TreeView控件的值到另一个TreeView上,带checkBook、可多选

 我来答
路露686
2012-04-26
知道答主
回答量:11
采纳率:0%
帮助的人:9.6万
展开全部
treeView1有个属性是CheckBoxes
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
private TreeView treeView1;
private Button showCheckedNodesButton;
private TreeViewCancelEventHandler checkForCheckedChildren;

public Form1()
{
treeView1 = new TreeView();
showCheckedNodesButton = new Button();
checkForCheckedChildren =
new TreeViewCancelEventHandler(CheckForCheckedChildrenHandler);

this.SuspendLayout();

// Initialize treeView1.
treeView1.Location = new Point(0, 25);
treeView1.Size = new Size(292, 248);
treeView1.Anchor = AnchorStyles.Top | AnchorStyles.Left |
AnchorStyles.Bottom | AnchorStyles.Right;
treeView1.CheckBoxes = true;

// Add nodes to treeView1.
TreeNode node;
for (int x = 0; x < 3; ++x)
{
// Add a root node.
node = treeView1.Nodes.Add(String.Format("Node{0}", x*4));
for (int y = 1; y < 4; ++y)
{
// Add a node as a child of the previously added node.
node = node.Nodes.Add(String.Format("Node{0}", x*4 + y));
}
}

// Set the checked state of one of the nodes to
// demonstrate the showCheckedNodesButton button behavior.
treeView1.Nodes[1].Nodes[0].Nodes[0].Checked = true;

// Initialize showCheckedNodesButton.
showCheckedNodesButton.Size = new Size(144, 24);
showCheckedNodesButton.Text = "Show Checked Nodes";
showCheckedNodesButton.Click +=
new EventHandler(showCheckedNodesButton_Click);

// Initialize the form.
this.ClientSize = new Size(292, 273);
this.Controls.AddRange(new Control[]
{ showCheckedNodesButton, treeView1 } );

this.ResumeLayout(false);
}

[STAThreadAttribute()]
static void Main()
{
Application.Run(new Form1());
}

private void showCheckedNodesButton_Click(object sender, EventArgs e)
{
// Disable redrawing of treeView1 to prevent flickering
// while changes are made.
treeView1.BeginUpdate();

// Collapse all nodes of treeView1.
treeView1.CollapseAll();

// Add the checkForCheckedChildren event handler to the BeforeExpand event.
treeView1.BeforeExpand += checkForCheckedChildren;

// Expand all nodes of treeView1. Nodes without checked children are
// prevented from expanding by the checkForCheckedChildren event handler.
treeView1.ExpandAll();

// Remove the checkForCheckedChildren event handler from the BeforeExpand
// event so manual node expansion will work correctly.
treeView1.BeforeExpand -= checkForCheckedChildren;

// Enable redrawing of treeView1.
treeView1.EndUpdate();
}

// Prevent expansion of a node that does not have any checked child nodes.
private void CheckForCheckedChildrenHandler(object sender,
TreeViewCancelEventArgs e)
{
if (!HasCheckedChildNodes(e.Node)) e.Cancel = true;
}

// Returns a value indicating whether the specified
// TreeNode has checked child nodes.
private bool HasCheckedChildNodes(TreeNode node)
{
if (node.Nodes.Count == 0) return false;
foreach (TreeNode childNode in node.Nodes)
{
if (childNode.Checked) return true;
// Recursively check the children of the current child node.
if (HasCheckedChildNodes(childNode)) return true;
}
return false;
}

}
陈韩泰
2012-03-23 · TA获得超过107个赞
知道小有建树答主
回答量:128
采纳率:0%
帮助的人:102万
展开全部
多选什么啊,是添加源TreeView的SelectedItem值到另一个目标TreeView吗?如果是的话,先添加treeview的selected事件,然后在这个事件里面写:(新建一个TreeNode实例,然后把selectedItem赋值给TreeNode,然后再用目标Treeview.Items.add()函数添加这个TreeNode)
如果不是的话,就直接把Treeview对象赋给新的Treeview对象就好了
追问
能给个源码吗?
追答
很简单的,自己做吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式