private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e) {
treeView1.Height = GetExpandNodeCount() * treeView1.ItemHeight;
}
private void treeView1_AfterExpand(object sender, TreeViewEventArgs e) {
treeView1.Height = GetExpandNodeCount() * treeView1.ItemHeight;
}
private int GetExpandNodeCount() {
int count = 0;
foreach(TreeNode each in treeView1.Nodes) {
count += GetSingleNodeCount(each);
}
return count;
}
private int GetSingleNodeCount(TreeNode node) {
int count = 0;
++count;
if(node.IsExpanded) {
foreach(TreeNode each in node.Nodes) {
count += GetSingleNodeCount(each);
}
}
return count;
}
要自动缩放控件,只要将控件的Dock设为Top,并通过改变其Visible属性来缩放
转载于:https://www.cnblogs.com/cjfwu/archive/2008/03/18/1111827.html