public void LoadCommands() { //load all commands //AutomationCommands = TypeMethods.GenerateAutomationCommands(AContainer); var groupedCommands = AutomationCommands.Where(x => x.Command.CommandName != "BrokenCodeCommentCommand" && x.Command.CommandName != "SequenceCommand") .GroupBy(f => f.DisplayGroup); tvCommands.Nodes.Clear(); foreach (var cmd in groupedCommands) { TreeNode newGroup = new TreeNode(cmd.Key); foreach (var subcmd in cmd) { TreeNode subNode = new TreeNode(subcmd.ShortName); subNode.ToolTipText = subcmd.Description; newGroup.Nodes.Add(subNode); } tvCommands.Nodes.Add(newGroup); } tvCommands.Sort(); _tvCommandsCopy = new TreeView(); _tvCommandsCopy.ShowNodeToolTips = true; CopyTreeView(tvCommands, _tvCommandsCopy); txtCommandSearch.Text = _txtCommandWatermark; }
private void lstScriptActions_DragDrop(object sender, DragEventArgs e) { //returns the location of the mouse pointer in the ListView control Point cp = SelectedTabScriptActions.PointToClient(new Point(e.X, e.Y)); //obtain the item that is located at the specified location of the mouse pointer ListViewItem dragToItem = SelectedTabScriptActions.GetItemAt(cp.X, cp.Y); if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) { TreeNode commandNode = ((TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode")); if (commandNode.Nodes.Count != 0) { return; } var commandName = commandNode.Text; var commandGroupName = commandNode.Parent.Text; var newCommandName = AutomationCommands.Where(x => x.ShortName == commandName && x.DisplayGroup == commandGroupName) .Select(x => x.Command).FirstOrDefault().GetType(); dynamic newCommandInstance = TypeMethods.CreateTypeInstance(AContainer, newCommandName.Name); CreateUndoSnapshot(); if (dragToItem != null) { AddCommandToListView(newCommandInstance, dragToItem.Index); } else { AddCommandToListView(newCommandInstance, SelectedTabScriptActions.Items.Count); } } else { //return if the items are not selected in the ListView control if (SelectedTabScriptActions.SelectedItems.Count == 0) { return; } CreateUndoSnapshot(); if (dragToItem == null) { return; } List <ScriptCommand> commandsToMove = new List <ScriptCommand>(); for (int i = 0; i <= SelectedTabScriptActions.SelectedItems.Count - 1; i++) { var command = (ScriptCommand)SelectedTabScriptActions.SelectedItems[i].Tag; commandsToMove.Add(command); } //obtain the index of the item at the mouse pointer int dragIndex = dragToItem.Index; ListViewItem[] sel = new ListViewItem[SelectedTabScriptActions.SelectedItems.Count]; for (int i = 0; i <= SelectedTabScriptActions.SelectedItems.Count - 1; i++) { sel[i] = SelectedTabScriptActions.SelectedItems[i]; } for (int i = 0; i < sel.GetLength(0); i++) { //obtain the ListViewItem to be dragged to the target location ListViewItem dragItem = sel[i]; int itemIndex = dragIndex; if (itemIndex == dragItem.Index) { return; } if (dragItem.Index < itemIndex) { itemIndex++; } else { itemIndex = dragIndex + i; } //insert the item at the mouse pointer ListViewItem insertItem = (ListViewItem)dragItem.Clone(); SelectedTabScriptActions.Items.Insert(itemIndex, insertItem); //removes the item from the initial location while the item is moved to the new location SelectedTabScriptActions.Items.Remove(dragItem); SelectedTabScriptActions.Invalidate(); } } }