0
我對樹視圖知之甚少。我希望能夠將樹節點移動到頂部並向上移動樹節點。將TreeNode移動到頂部,向上,向下和向下C#
-Fruits -Apples -Oranges -bananas -Watermelon
下面是代碼我已經嘗試實施。
首先,我把西瓜移到頂部,它工作正常。接下來,當我嘗試向上移動香蕉時,香蕉的指數將顯示-1。還有什麼我錯了嗎?
private void moveTopToolStripMenuItem_Click(object sender, EventArgs e)
{
TreeNode tv = new TreeNode();
TreeNode selectedNodeToMove = new TreeNode();
try
{
TreeNode parent = treeView.SelectedNode.Parent;
selectedNodeToMove = treeView.SelectedNode;
if (parent != null)
{
parent.Nodes.Remove(treeView.SelectedNode);
parent.Nodes[0] = selectedNodeToMove;
}
}
catch (Exception ex)
{
}
}
private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
{
TreeNode tv = new TreeNode();
TreeNode selectedNodeToMove = new TreeNode();
try
{
TreeNode parent = treeView.SelectedNode.Parent;
selectedNodeToMove = treeView.SelectedNode;
if (parent != null)
{
int index = parent.Nodes.IndexOf(treeView.SelectedNode);
if (index > 0)
{
tv = parent.Nodes[index - 1];
parent.Nodes.Remove(treeView.SelectedNode);
parent.Nodes.Remove(tv);
parent.Nodes.Insert(index - 1, selectedNodeToMove);
parent.Nodes.Insert(index, tv);
}
}
}
catch (Exception ex)
{
}
}