473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inheritance for csharp listboxes for drag drop?

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;
}
}
}
May 4 '06 #1
4 2549

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 :-)
May 4 '06 #2
Thanks Paul!

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

Alex

Paul Cheetham <PA******@dsl.pipex.com> wrote in news:#PHvtN6bGHA.1204
@TK2MSFTNGP02.phx.gbl:

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 :-)


May 4 '06 #3
....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!

May 4 '06 #4


....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...

May 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: softengine | last post by:
Can and how do you alter a data view to include a look up field from another data table? The data table of the dataview only has the key, the value I need is in another data table. Can and how...
11
by: PAul Maskens | last post by:
The form designer adds unnecessary code to the section when using a subclassed control. I've reproduced this in VS.NET 2002 and VS.NET 2003 so it's pretty fundamental. Outline steps: Create a...
7
by: Sam | last post by:
Hi, I've done the method described here : http://64.78.52.104/FAQ/WinForms/FAQ_c87c.asp#q565q It works well for items added at design time. However if my source list is filled with a...
2
by: Dwayne Gaddy | last post by:
Hey all, I have a windows form with a list boxes. I have data binded the list boxes with data from my sql database. I want to use drag and drop to allow users to choose different options from...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
1
by: John Dalberg | last post by:
I have a couple of lisboxes in a form. I want to drag items from the first list and drop in the second list and sequence in the second list by dragging up and down. The same way in regular Windows...
2
by: CNemo | last post by:
Hello friends! I need to do subj and VS03. No Atlas or something like this. Would be great if somebody may share a small sample how to do it. What i have in mind is like: 1. Create hidden...
2
by: salad | last post by:
This is a tip on how to speed up listboxes DRAMATICALLY. Persons that would benefit are those that are constantly updating the rowsource of a listbox/combobox in order to filter and sort the data...
0
by: piercy | last post by:
i have two listboxes, lbData and lbTo. i need to drag and drop between the two. lbData.datasource = tblfilter; i need to be able to drag and drop the data from one to the other then on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.