Inheritance for csharp listboxes for drag drop?


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;
}
}
}
 
Ad

Advertisements

P

Paul Cheetham

Hi,
private System.Windows.Forms.ListBox listBox1;

Change this line to this:

private SchedulingTool.ListBoxDragNDrop listBox1;


You will also need to change the new statement to:

listBox1 = new SchedulingTool.ListBoxDragNDrop();


The ListBoxDragNDrop class is a descendant of the standard Listbox.


I would suggest reading up on Inheritance and PolyMorphism so you get a
better understanding of what is going on. (They may be about C++, but
it's good enough. You should be able to understand the syntax, and the
principles are the same.)


Paul Cheetham

P.S. It's polite to use a name when you post :)
 
A

Alex

Thanks Paul!

That did it... I guess the ListBoxDragNDrop was the new class - I didn't
understand it that way initially.

Alex
 
A

Alex

....I have another question on the same topic now.

What I'd like to do is remove the item from the source listbox once it is
dropped into the target listbox.

Is there a way I can tell the result of the drag operation from the source
listbox in the OnMouseDown method, or do I need to message back to the
source listbox in some way?

If so, how would I do that across windows? Right now I have one window
with the listbox (now classed as ListBoxDragNDrop) and I start two
instances of the window from my control window. I'm not sure how to have
the target listbox invoke a method on the source listbox dynamically (ie,
be able to do this regardless of the number of listboxes)...?

Thanks again!
 
Ad

Advertisements

A

Alex

....I was trying to message back via the "sender" object in the the
OnDragDrop method, but it didn't seem like it has the methods of the source
listbox exposed though it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top