private void button1_Click(object sender, EventArgs e) { Model.GroupID = Model.GroupID == Guid.Empty ? Guid.NewGuid() : Model.GroupID; Model.GroupName = txtGroupName.Text; if (string.IsNullOrWhiteSpace(Model.GroupName)) { MessageBox.Show("请首先填写组名称!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } RDSDataManager dataManager = new RDSDataManager(); dataManager.AddGroup(Model); this.DialogResult = DialogResult.OK; }
/// <summary> /// 拖动释放 /// 参考:http://www.cnblogs.com/jiewei915/archive/2012/01/11/2318951.html /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void serverTree_DragDrop(object sender, DragEventArgs e) { // 得到拖放中的节点 var moveDataNode = (RDSDataNode)e.Data.GetData("RDManager.Model.RDSDataNode"); // 根据鼠标坐标确定要移动到的目标节点 TreeNode targetNode = new TreeNode(); Point pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y)); targetNode = this.serverTree.GetNodeAt(pt); var targetDataNode = (RDSDataNode)targetNode; // 如果目标为自己,返回 if (targetDataNode.Text == moveDataNode.Text) { return; } // 如果目标包含在自己的节点树中,返回 if (moveDataNode.Nodes.Contains(targetDataNode)) { return; } else if (targetDataNode.FullPath.Contains(moveDataNode.FullPath)) { return; } // 如果目标节点是Server,返回 if (targetDataNode.NodeType == RDSDataNodeType.Server) { return; } // 如果目标为子节点,返回 if (targetDataNode.Parent == moveDataNode) { return; } // 如果目标为父节点,返回 if (targetDataNode == moveDataNode.Parent) { return; } // 移除拖放的节点 RemoveServerTreeNode(moveDataNode, false); // 添加节点 var newMoveNode = (RDSDataNode)moveDataNode.Clone(); newMoveNode.RDSData = moveDataNode.RDSData; newMoveNode.NodeType = moveDataNode.NodeType; // 保存节点 if (newMoveNode.NodeType == RDSDataNodeType.Group) { var group = (RDSGroup)newMoveNode.RDSData; group.ParentGroupID = (Guid)targetDataNode.Tag; RDSDataManager dataManager = new RDSDataManager(); dataManager.AddGroup(group); } else { var server = (RDSServer)newMoveNode.RDSData; server.GroupID = (Guid)targetDataNode.Tag; RDSDataManager dataManager = new RDSDataManager(); dataManager.AddServer(server); } // 将节点插入到分组中 targetDataNode.Nodes.Insert(targetDataNode.Nodes.Count, newMoveNode); // 更新当前拖动的节点选择 serverTree.SelectedNode = newMoveNode; // 展开当前节点的父节点 targetDataNode.Expand(); }