F
Full NameEntry
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 ListBoxDragNDrop() 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SchedulingTool
{
public class ListBoxDragNDrop : ListBox
{
private int lastMouseUpItemIndex = -1;
private bool isDropSource = false;
public ListBoxDragNDrop()
{
this.AllowDrop = true; //allow D&D
this.SelectionMode = SelectionMode.One; //single
selection
DragDrop += new System.Windows.Forms.DragEventHandler
(OnDragDrop);
DragEnter += new System.Windows.Forms.DragEventHandler
(OnDragEnter);
MouseDown += new System.Windows.Forms.MouseEventHandler
(OnMouseDown);
SelectedIndexChanged += new System.EventHandler
(OnSelectedIndexChanged);
}
private void OnDragDrop(object sender, DragEventArgs e)
{
if (e.Effect == DragDropEffects.Copy)
{
Point point = this.PointToClient(new Point(e.X, e.Y));
int index = this.IndexFromPoint(point);
if (index > -1 && index < this.Items.Count)
Items.Insert(index, e.Data.GetData
(DataFormats.Text));
else
Items.Add(e.Data.GetData(DataFormats.Text));
}
}
private void OnDragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text) && !isDropSource)
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void OnMouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left && SelectedIndex ==
lastMouseUpItemIndex)
{
isDropSource = true;
DoDragDrop(SelectedItem, DragDropEffects.Copy);
isDropSource = false;
lastMouseUpItemIndex = this.SelectedIndex;
}
}
private void OnSelectedIndexChanged(object sender,
System.EventArgs e)
{
lastMouseUpItemIndex = this.SelectedIndex;
}
}
}
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 ListBoxDragNDrop() 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SchedulingTool
{
public class ListBoxDragNDrop : ListBox
{
private int lastMouseUpItemIndex = -1;
private bool isDropSource = false;
public ListBoxDragNDrop()
{
this.AllowDrop = true; //allow D&D
this.SelectionMode = SelectionMode.One; //single
selection
DragDrop += new System.Windows.Forms.DragEventHandler
(OnDragDrop);
DragEnter += new System.Windows.Forms.DragEventHandler
(OnDragEnter);
MouseDown += new System.Windows.Forms.MouseEventHandler
(OnMouseDown);
SelectedIndexChanged += new System.EventHandler
(OnSelectedIndexChanged);
}
private void OnDragDrop(object sender, DragEventArgs e)
{
if (e.Effect == DragDropEffects.Copy)
{
Point point = this.PointToClient(new Point(e.X, e.Y));
int index = this.IndexFromPoint(point);
if (index > -1 && index < this.Items.Count)
Items.Insert(index, e.Data.GetData
(DataFormats.Text));
else
Items.Add(e.Data.GetData(DataFormats.Text));
}
}
private void OnDragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text) && !isDropSource)
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void OnMouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left && SelectedIndex ==
lastMouseUpItemIndex)
{
isDropSource = true;
DoDragDrop(SelectedItem, DragDropEffects.Copy);
isDropSource = false;
lastMouseUpItemIndex = this.SelectedIndex;
}
}
private void OnSelectedIndexChanged(object sender,
System.EventArgs e)
{
lastMouseUpItemIndex = this.SelectedIndex;
}
}
}