Hi all,
I'm new to csharp... learning it by muddling my way though examples.
In my current example, I'm trying to make a form with a bunch of
listboxes that have drag and drop enabled amongst them...
I found an example online in the form of a DragNDrop.cs file that I've
included in my project (attached below).
I also have a form with a listbox (one for now) that is defined like
this...
private System.Windows. Forms.ListBox listBox1;
I'm not sure how I go about making the new methods from the DragNDrop.cs
file part of the 'standard' listbox.
In my research, it seems like I should be extending
System.Windows. Forms.ListBox and classing from that, but the example
doesn't really seem to be going that way...
Should I call ListBoxDragNDro p() with each standard listbox instance? If
so, what would the syntax for that be & where should I place it? I've
tried various different tacts, but they've all resulted in build
failures...
Thanks!!!
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
namespace SchedulingTool
{
public class ListBoxDragNDro p : ListBox
{
private int lastMouseUpItem Index = -1;
private bool isDropSource = false;
public ListBoxDragNDro p()
{
this.AllowDrop = true; //allow D&D
this.SelectionM ode = SelectionMode.O ne; //single
selection
DragDrop += new System.Windows. Forms.DragEvent Handler
(OnDragDrop);
DragEnter += new System.Windows. Forms.DragEvent Handler
(OnDragEnter);
MouseDown += new System.Windows. Forms.MouseEven tHandler
(OnMouseDown);
SelectedIndexCh anged += new System.EventHan dler
(OnSelectedInde xChanged);
}
private void OnDragDrop(obje ct sender, DragEventArgs e)
{
if (e.Effect == DragDropEffects .Copy)
{
Point point = this.PointToCli ent(new Point(e.X, e.Y));
int index = this.IndexFromP oint(point);
if (index > -1 && index < this.Items.Coun t)
Items.Insert(in dex, e.Data.GetData
(DataFormats.Te xt));
else
Items.Add(e.Dat a.GetData(DataF ormats.Text));
}
}
private void OnDragEnter(obj ect sender,
System.Windows. Forms.DragEvent Args e)
{
if (e.Data.GetData Present(DataFor mats.Text) && !isDropSource)
e.Effect = DragDropEffects .Copy;
else
e.Effect = DragDropEffects .None;
}
private void OnMouseDown(obj ect sender,
System.Windows. Forms.MouseEven tArgs e)
{
if (MouseButtons == MouseButtons.Le ft && SelectedIndex ==
lastMouseUpItem Index)
{
isDropSource = true;
DoDragDrop(Sele ctedItem, DragDropEffects .Copy);
isDropSource = false;
lastMouseUpItem Index = this.SelectedIn dex;
}
}
private void OnSelectedIndex Changed(object sender,
System.EventArg s e)
{
lastMouseUpItem Index = this.SelectedIn dex;
}
}
}