/// <summary>
/// 上移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbMoveUp_Click(object sender, EventArgs e)
{
if (Tv_tree.SelectedNode == null)
{
MessageBox.Show("请选择需要移动的的节点!");
this.Focus();
return;
}
folder model = new folder();
TreeNode node = Tv_tree.SelectedNode; //获得选中节点的值
model.folderId = node.Tag.ToString();
model.TheOrder = node.Name.ToString();
if (node.PrevNode == null) //选中节点的上一个节点为Null则返回
{
return;
}
folder upModel = new folder();
TreeNode upNode = node.PrevNode; //获得选中节点的上一个节点
upModel.folderId = upNode.Tag.ToString();
upModel.TheOrder = upNode.Name;
bool flag = folderOperation.MoveUp(model, upModel); //将两个节点的排序编号互换
if (flag) //如果执行成功
{
string theOrder = node.Name;
node.Name = upNode.Name;
upNode.Name = theOrder;
TreeNode newNode = (TreeNode)node.Clone(); //克隆选中的节点
if (node.Parent == null)
{
Tv_tree.Nodes.Insert(upNode.Index, newNode); //在选中节点的上一个节点的地方插入本节点
}
else
{
node.Parent.Nodes.Insert(upNode.Index, newNode);
}
node.Remove();
Tv_tree.SelectedNode = newNode;
}
else
{
return;
}
}
/// <summary>
/// 下移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbMoveDown_Click(object sender, EventArgs e)
{
if (Tv_tree.SelectedNode == null)
{
MessageBox.Show("请选择需要移动的的节点!");
this.Focus();
return;
}
folder model = new folder();
TreeNode node = Tv_tree.SelectedNode; //选中的节点
model.folderId = node.Tag.ToString();
model.TheOrder = node.Name.ToString();
if (node.NextNode == null) //下一个节点为Null时返回
{
return;
}
folder downModel = new folder();
TreeNode dowNode = node.NextNode;
downModel.folderId = dowNode.Tag.ToString();
downModel.TheOrder = dowNode.Name;
bool flag = folderOperation.MoveUp(model, downModel);
if (flag)
{
string theOrder = node.Name;
node.Name = dowNode.Name;
dowNode.Name = theOrder;
TreeNode newNode = (TreeNode)dowNode.Clone(); //克隆下一个节点及其所有的子节点
if (node.Parent == null)
{
Tv_tree.Nodes.Insert(node.Index, newNode); //在原节点的位置插入下一个节点的值
}
else
{
node.Parent.Nodes.Insert(node.Index, newNode);
}
dowNode.Remove(); //删除掉选中节点的下一个节点
Tv_tree.SelectedNode = node; //重新选中
}
else
{
return;
}
}
string sql = string.Format("update folder set theorder = '{0}' where folderid = '{1}'", model.TheOrder, upModel.folderId);
string sql1 = string.Format("update folder set theorder = '{0}' where folderid = '{1}'", upModel.TheOrder, model.folderId);
/// <summary>
/// 节点的展开和折叠
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvDocInfo_AfterExpand(object sender, TreeViewEventArgs e)
{
if (e.Node.Parent == null)
{
TreeView tree = (TreeView)sender;
tree.AfterCheck -= new TreeViewEventHandler(this.tvDocInfo_AfterExpand);
//折叠其他同级节点
TreeNode parentNode = e.Node.Parent;
if (parentNode != null)
{
foreach (TreeNode tn in parentNode.Nodes)
{
if (tn != e.Node)
tn.Collapse();
}
}
else
{
foreach (TreeNode tn in tree.Nodes)
{
if (tn != e.Node)
tn.Collapse();
}
}
}
}
节点拖拽,必须设置TreeView的AllowDrop属性为True;
private Point Position = new Point(0, 0);
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}
private void treeView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(TreeNode)))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
TreeNode myNode = null;
if (e.Data.GetDataPresent(typeof(TreeNode)))
{
myNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
}
else
{
MessageBox.Show("error");
}
Position.X = e.X;
Position.Y = e.Y;
Position = tvType.PointToClient(Position);
TreeNode DropNode = this.tvType.GetNodeAt(Position);
if (FunctionsUI.CreateQuestionDialog("是否要将"+myNode.Text+"移动"+DropNode.Text+"类别下?") == DialogResult.OK)
{
// 1.目标节点不是空。2.目标节点不是被拖拽接点的字节点。3.目标节点不是被拖拽节点本身
if (DropNode != null && DropNode.Parent != myNode && DropNode != myNode)
{
TreeNode DragNode = myNode;
// 将被拖拽节点从原来位置删除。
myNode.Remove();
// 在目标节点下增加被拖拽节点
DropNode.Nodes.Add(DragNode);
}
// 如果目标节点不存在,即拖拽的位置不存在节点,那么就将被拖拽节点放在根节点之下
if (DropNode == null)
{
TreeNode DragNode = myNode;
myNode.Remove();
tvType.Nodes.Add(DragNode);
}
}
}