static void toolboxControl_DragDrop(object sender, DragEventArgs e) { string data = null; if (e.Data.GetDataPresent(System.Windows.Forms.DataFormats.UnicodeText)) { data = e.Data.GetData(System.Windows.Forms.DataFormats.UnicodeText) as string; } if (e.Data.GetDataPresent(typeof(ScriptEditor).FullName + ".toolbox")) { object obj = e.Data.GetData(typeof(ScriptEditor).FullName + ".toolbox"); if (obj == toolboxControl) { if (data != null) { if (toolboxControl.Items.Contains(data)) { int id = toolboxControl.IndexFromPoint(toolboxControl.PointToClient(new Point(e.X, e.Y))); if (id < 0 || id >= toolboxControl.Items.Count) { id = toolboxControl.Items.Count - 1; } if ((string)toolboxControl.Items[id] == data) { return; } toolboxControl.Items.Remove(data); toolboxControl.Items.Insert(id, data); toolboxControl.SelectedItem = data; } else { toolboxControl.Items.Add(data); toolboxControl.SelectedItem = data; } } } else { if (data != null && !toolboxControl.Items.Contains(data)) { toolboxControl.Items.Add(data); toolboxControl.SelectedItem = data; } } } else if (data != null && !toolboxControl.Items.Contains(data)) { toolboxControl.Items.Add(data); toolboxControl.SelectedItem = data; } }
private void Setline(ListBox target, bool visible, DragEventArgs e) { panel1.Visible = visible; if (target == null) { return; } Point p = new Point(e.X, e.Y); p = (target).PointToClient(p); int indexOfItem = EventBox.IndexFromPoint(p.X, p.Y); int y = target.PointToClient(p).Y; int x1 = target.Left; int x2 = target.Width; int ih = target.ItemHeight; int y1 = (int)Math.Min(Math.Round((double)(y / ih)),target.Items.Count) * ih + target.Top; panel1.Left = x1; panel1.Top = y1; panel1.Width = x2; panel1.Height = 2; }
private void ListDragTarget_DragOver(object sender, System.Windows.Forms.DragEventArgs e) { // Determine whether string data exists in the drop data. If not, then // the drop effect reflects that the drop cannot occur. if (!e.Data.GetDataPresent(typeof(System.String))) { e.Effect = DragDropEffects.None; DropLocationLabel.Text = "None - no string data."; return; } // Set the effect based upon the KeyState. if ((e.KeyState & (8 + 32)) == (8 + 32) && (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) { // KeyState 8 + 32 = CTL + ALT // Link drag-and-drop effect. e.Effect = DragDropEffects.Link; } else if ((e.KeyState & 32) == 32 && (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) { // ALT KeyState for link. e.Effect = DragDropEffects.Link; } else if ((e.KeyState & 4) == 4 && (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) { // SHIFT KeyState for move. e.Effect = DragDropEffects.Move; } else if ((e.KeyState & 8) == 8 && (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) { // CTL KeyState for copy. e.Effect = DragDropEffects.Copy; } else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) { // By default, the drop action should be move, if allowed. e.Effect = DragDropEffects.Move; } else { e.Effect = DragDropEffects.None; } // Get the index of the item the mouse is below. // The mouse locations are relative to the screen, so they must be // converted to client coordinates. indexOfItemUnderMouseToDrop = ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(new Point(e.X, e.Y))); // Updates the label text. if (indexOfItemUnderMouseToDrop != ListBox.NoMatches) { DropLocationLabel.Text = "Drops before item #" + (indexOfItemUnderMouseToDrop + 1); } else { DropLocationLabel.Text = "Drops at the end."; } }
private int getIndex(ListBox list, int x, int y) { Point point = list.PointToClient(new Point(x, y)); int index = list.IndexFromPoint(point); if (index < 0) { index = list.Items.Count - 1; } //off the end of the list return index; }