/// <summary> /// Move a Vehicle Gear TreeNode after Drag and Drop. /// </summary> /// <param name="intNewIndex">Node's new index.</param> /// <param name="objDestination">Destination Node.</param> public void MoveVehicleGearParent(int intNewIndex, TreeNode objDestination, TreeView treVehicles, ContextMenuStrip cmsVehicleGear) { // The item cannot be dropped onto itself. if (objDestination == treVehicles.SelectedNode) { return; } // The item cannot be dropped onton one of its children. foreach (TreeNode objNode in treVehicles.SelectedNode.Nodes) { if (objNode == objDestination) { return; } } // Determine if this is a Location. TreeNode objVehicleNode = objDestination; do { objVehicleNode = objVehicleNode.Parent; } while (objVehicleNode.Level > 1); // Get a reference to the destination Vehicle. Vehicle objDestinationVehicle = new Vehicle(_objCharacter); foreach (Vehicle objCharacterVehicle in _objCharacter.Vehicles) { if (objCharacterVehicle.InternalId == objVehicleNode.Tag.ToString()) { objDestinationVehicle = objCharacterVehicle; break; } } // Make sure the destination is another piece of Gear or a Location. bool blnDestinationGear = true; bool blnDestinationLocation = false; Vehicle objTempVehicle = new Vehicle(_objCharacter); Gear objDestinationGear = _objFunctions.FindVehicleGear(objDestination.Tag.ToString(), _objCharacter.Vehicles, out objTempVehicle); if (objDestinationGear == null) { blnDestinationGear = false; } // Determine if this is a Location in the destination Vehicle. string strDestinationLocation = ""; foreach (string strLocation in objDestinationVehicle.Locations) { if (strLocation == objDestination.Tag.ToString()) { strDestinationLocation = strLocation; blnDestinationLocation = true; break; } } if (!blnDestinationLocation && !blnDestinationGear) { return; } // Locate the currently selected piece of Gear. Vehicle objVehicle = new Vehicle(_objCharacter); Gear objGear = _objFunctions.FindVehicleGear(treVehicles.SelectedNode.Tag.ToString(), _objCharacter.Vehicles, out objVehicle); // Gear cannot be moved to one of its children. bool blnAllowMove = true; TreeNode objFindNode = objDestination; if (objDestination.Level > 0) { do { objFindNode = objFindNode.Parent; if (objFindNode.Tag.ToString() == objGear.InternalId) { blnAllowMove = false; break; } } while (objFindNode.Level > 0); } if (!blnAllowMove) { return; } // Remove the Gear from the Vehicle. if (objGear.Parent == null) { objVehicle.Gear.Remove(objGear); } else { objGear.Parent.Children.Remove(objGear); } if (blnDestinationLocation) { // Add the Gear to the Vehicle and set its Location. objDestinationVehicle.Gear.Add(objGear); objGear.Location = strDestinationLocation; objGear.Parent = null; } else { // Add the Gear to its new parent. objDestinationGear.Children.Add(objGear); objGear.Location = ""; objGear.Parent = objDestinationGear; } TreeNode objClone = treVehicles.SelectedNode; objClone.ContextMenuStrip = cmsVehicleGear; // Remove the current Node. treVehicles.SelectedNode.Remove(); // Add the new Node to its parent. objDestination.Nodes.Add(objClone); objDestination.Expand(); }