treeview - 根下的第一个节点 appears/displayed
treeview - first node appears/displayed under root
我有一个表单、按钮和树视图。我首先向我的树视图添加一个根节点。按下按钮时,它会向根节点添加一个节点,但显示如下...
我怎样才能避免这种情况?
注意,选择根节点可以解决问题。
来自 class.cs ...
Explorer explorer = new Explorer();
public Form1()
{
InitializeComponent();
explorer.init(this.tree);
}
从Explorer.cs ...
private TreeView tree;
private RootNode root;
public Explorer()
{
root = new RootNode();
}
public void init(TreeView tree)
{
this.tree = tree;
tree.LabelEdit = true;
tree.Nodes.Add(root);
tree.AfterLabelEdit += this.AfterLabelEdit;
}
以及根节点...
public class RootNode : TreeNode
{
public RootNode()
{
this.Name = "main";
this.Text = "Main";
}
// This method called by the button click handler
public void AddTestCase()
{
var newNode = new TestCaseNode();
newNode.Text = "New testcase";
this.Nodes.Add(newNode);
this.TreeView.Select();
newNode.BeginEdit();
}
}
我快速浏览了这些方法并添加了 newNode.EnsureVisible()
。这解决了我的问题。
我有一个表单、按钮和树视图。我首先向我的树视图添加一个根节点。按下按钮时,它会向根节点添加一个节点,但显示如下...
我怎样才能避免这种情况?
注意,选择根节点可以解决问题。
来自 class.cs ...
Explorer explorer = new Explorer();
public Form1()
{
InitializeComponent();
explorer.init(this.tree);
}
从Explorer.cs ...
private TreeView tree;
private RootNode root;
public Explorer()
{
root = new RootNode();
}
public void init(TreeView tree)
{
this.tree = tree;
tree.LabelEdit = true;
tree.Nodes.Add(root);
tree.AfterLabelEdit += this.AfterLabelEdit;
}
以及根节点...
public class RootNode : TreeNode
{
public RootNode()
{
this.Name = "main";
this.Text = "Main";
}
// This method called by the button click handler
public void AddTestCase()
{
var newNode = new TestCaseNode();
newNode.Text = "New testcase";
this.Nodes.Add(newNode);
this.TreeView.Select();
newNode.BeginEdit();
}
}
我快速浏览了这些方法并添加了 newNode.EnsureVisible()
。这解决了我的问题。