private void OnFormLoad(object sender, EventArgs e) { lv.Columns.Add("Property"); lv.Columns.Add("Value"); lv.View = View.Details; SizeColumns(lv); treeLGUs.Nodes.Add("root", "Provinces and LGUs").Tag = "root"; foreach (var item in LGUs.GetLGUTree()) { var provNode = treeLGUs.Nodes["root"].Nodes.Add(item.Key.ToString(), item.Value.provinceName); _provinceCount++; provNode.Tag = "province"; foreach (var lgu in item.Value.lgus) { var munNode = provNode.Nodes.Add(lgu.Key.ToString(), lgu.Value); munNode.Tag = "municipality"; _lguCount++; } } global.LoadFormSettings(this); }
private void OnDragDrop(object sender, DragEventArgs e) { // Retrieve the client coordinates of the drop location. Point targetPoint = treeLGUs.PointToClient(new Point(e.X, e.Y)); // Retrieve the node at the drop location. TreeNode targetNode = treeLGUs.GetNodeAt(targetPoint); // Retrieve the node that was dragged. TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); if (targetNode.Tag.ToString() == "province" && draggedNode.Parent.Text != targetNode.Text) { // Sanity check if (draggedNode == null) { return; } // Did the user drop on a valid target node? if (targetNode == null) { // The user dropped the node on the treeview control instead // of another node so lets place the node at the bottom of the tree. draggedNode.Remove(); treeLGUs.Nodes.Add(draggedNode); draggedNode.Expand(); } else { TreeNode parentNode = targetNode; // Confirm that the node at the drop location is not // the dragged node and that target node isn't null // (for example if you drag outside the control) if (!draggedNode.Equals(targetNode) && targetNode != null) { bool canDrop = true; // Crawl our way up from the node we dropped on to find out if // if the target node is our parent. while (canDrop && (parentNode != null)) { canDrop = !Object.ReferenceEquals(draggedNode, parentNode); parentNode = parentNode.Parent; } // Is this a valid drop location? if (canDrop) { // Yes. Move the node, expand it, and select it. var result = MessageBox.Show($"Are you sure you want to transfer the LGU of {draggedNode.Text} to the Province of {targetNode.Text}?", "Confirmation needed", MessageBoxButtons.YesNo, MessageBoxIcon.Question); //if (result == DialogResult.Yes && Landingsite.MoveToLandingSite(sourceTag.Item1, destinationTag.Item1)) if (result == DialogResult.Yes && LGUs.MoveLGuToProvince(int.Parse(draggedNode.Name), int.Parse(targetNode.Name))) { targetNode.Nodes.Add(draggedNode); draggedNode.Nodes.Clear(); targetNode.Expand(); } targetNode.BackColor = Color.White; } } } // Optional: Select the dropped node and navigate (however you do it) treeLGUs.SelectedNode = draggedNode; } else { MessageBox.Show("Can only move an LGU to a different province", "Validation error", MessageBoxButtons.OK, MessageBoxIcon.Information); } }