TreeNode拖拽功能
// 2009-08-25 Jacky
#region TreeNode拖拽功能
private void tvStorage_ItemDrag(object sender, ItemDragEventArgs e)
{
if (!LoginUser.HasPermission(Privilege.BAS_StorageEdit)) return;
TreeNode node = (TreeNode)e.Item;
if (Function.GetStringValue(node.Tag) == "000") return;
if (e.Button == MouseButtons.Left)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}
}
private void tvStorage_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void tvStorage_DragDrop(object sender, DragEventArgs e)
{
try
{
//获得拖放中的节点
TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
//根据鼠标坐标确定要移动到的目标节点
Point pt;
TreeNode targeNode;
pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
targeNode = this.tvStorage.GetNodeAt(pt);
// 目标节点为空时,停止操作
if (targeNode == null) return;
// 去除本身拖放的情况
if (targeNode == moveNode) return;
// 防止拖放到自己的子级当中
if (ChildJudge(targeNode, moveNode)) return;
int index = moveNode.Tag.ToString().IndexOf(",");
String StorageID = Function.GetStringValue(moveNode.Tag.ToString().Substring(0,index));
String SuperStorageID = string.Empty;
if (targeNode.Tag.ToString() == "000")
{
SuperStorageID = Function.GetStringValue(targeNode.Tag);
}
else
{
index = targeNode.Tag.ToString().IndexOf(",");
SuperStorageID = Function.GetStringValue(targeNode.Tag.ToString().Substring(0, index));
}
//更新“上级仓库编码”
int iReturn = Services.BASService.UpdateSuperStorageByID(SuperStorageID, StorageID);
if (iReturn == 1)
{
LoadData();
}
}
catch (Exception ex)
{
log.Error(Definition.MESSAGE_QUERY_ERROR + ":" + ex.Message);
Tool.ShowMessage(Definition.MESSAGE_QUERY_ERROR + ":" + ex.Message,MessageBoxIcon.Error);
}
}
#endregion
// 判断拖放项的子级中是否存有目标项
/// <summary>
/// 判断拖放项的子级中是否存有目标项
/// </summary>
/// <param name="targeNode">目标项</param>
/// <param name="modeNode">拖放项</param>
/// <returns>true:有->停止操作,反之继续操作</returns>
private bool ChildJudge(TreeNode targeNode, TreeNode modeNode)
{
foreach (TreeNode node in modeNode.Nodes)
{
if (node == targeNode)
return true;
if (node.Nodes.Count > 0)
if (ChildJudge(targeNode, node))
return true;
}
return false;
}