我有一个树视图和2个按钮 我想点击UP_button,如果我点击DOWN_button选择节点上面那么选择应该下去
答案 0 :(得分:0)
private void SelectNode(TreeNode node)
{
if(node.IsSelected)
{
// Determine which TreeNode to select.
switch(myComboBox.Text)
{
case "Previous":
node.TreeView.SelectedNode = node.PrevNode;
break;
case "PreviousVisible":
node.TreeView.SelectedNode = node.PrevVisibleNode;
break;
case "Next":
node.TreeView.SelectedNode = node.NextNode;
break;
case "NextVisible":
node.TreeView.SelectedNode = node.NextVisibleNode;
break;
case "First":
node.TreeView.SelectedNode = node.FirstNode;
break;
case "Last":
node.TreeView.SelectedNode = node.LastNode;
break;
}
}
node.TreeView.Focus();
}
修改强>
您当然会在按钮单击处理程序中使用“Previous”和“Next”或“PreviousVisible”和“NextVisible”的情况。此代码假定您从组合框中选择“操作”并按下按钮。
编辑2
如果您尝试上下移动节点(而不是选择),您可以使用以下内容:
TreeNode sourceNode = treeView.SelectedNode;
if (sourceNode.Parent == null)
{
return;
}
treeView.Nodes.Remove(sourceNode);
treeView.Nodes.Insert(sourceNode.Index + 1, sourceNode);
这会将当前节点向下移动一个。请注意,您必须编写更多代码来处理一些特殊情况(例如,第一个节点会发生什么?它是否适用于树的所有级别?),但基本上我会尝试这样做。
答案 1 :(得分:0)
您可以使用节点的一些不错的内置属性,因此,您可以使用:
TreeView.SelectedNode = TreeView.SelectedNode.PrevNode;
和下来:
TreeView.SelectedNode = TreeView.SelectedNode.NextNode;
答案 2 :(得分:0)
尝试UP:
private void btnUP_Click(object sender, EventArgs e)
{
var tn = tv.SelectedNode;
if(tn==null) return;
var idx = tn.Index;
txt.Text = "Node: " + idx;
var par = tn.Parent;
if(par==null) return;
par.Nodes.Remove(tn);
if (idx > 0)
{
par.Nodes.Insert(tn.Index - 1, tn);
return;
}
//if you want to move int its parent's parent [grand parent :) ]
if (par.Parent!=null)
par.Parent.Nodes.Add(tn);
}
答案 3 :(得分:0)
我的问题解决了。
thanx答案
private void button1_Click(object sender, EventArgs e)
{
TreeNode node = new TreeNode();
node = treeView1.SelectedNode;
treeView1.SelectedNode = node.NextVisibleNode;
node.TreeView.Focus();
}
答案 4 :(得分:0)
对于ASP.NET Treeview:
/// <summary>
/// MoveComponentUpLinkButton_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void MoveComponentUpLinkButton_Click(object sender, EventArgs e)
{
// Get the selected node
TreeNode sourceNode = this.MyTreeview.SelectedNode;
if (sourceNode != null)
{
// Get the selected node's parent
TreeNode parentNode = this.MyTreeview.SelectedNode.Parent;
if (parentNode != null)
{
int index = -1;
// For each node in selected nodes parent
for (int j = 0; j < parentNode.ChildNodes.Count; j++)
{
// If we found the selected node
if (sourceNode == parentNode.ChildNodes[j])
{
// save index
index = j;
break;
}
}
// If node is not already at top of list
if (index > 0)
{
// Move it up
parentNode.ChildNodes.RemoveAt(index);
parentNode.ChildNodes.AddAt(index - 1, sourceNode);
sourceNode.Selected = true;
}
}
}
}
/// <summary>
/// MoveComponentDownLinkButton_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void MoveComponentDownLinkButton_Click(object sender, EventArgs e)
{
// Get the selected node
TreeNode sourceNode = this.MyTreeview.SelectedNode;
if (sourceNode != null)
{
// Get the selected node's parent
TreeNode parentNode = this.MyTreeview.SelectedNode.Parent;
if (parentNode != null)
{
int index = -1;
// For each node in selected nodes parent
for (int j = 0; j < parentNode.ChildNodes.Count; j++)
{
// If we found the selected node
if (sourceNode == parentNode.ChildNodes[j])
{
// save index
index = j;
break;
}
}
// If node is not already at botton of list
if (index < parentNode.ChildNodes.Count - 1)
{
// Move it down
parentNode.ChildNodes.RemoveAt(index);
parentNode.ChildNodes.AddAt(index + 1, sourceNode);
sourceNode.Selected = true;
}
}
}
}