473,657 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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


Change this line to this:

private SchedulingTool. ListBoxDragNDro p listBox1;
You will also need to change the new statement to:

listBox1 = new SchedulingTool. ListBoxDragNDro p();
The ListBoxDragNDro p 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 ListBoxDragNDro p was the new class - I didn't
understand it that way initially.

Alex

Paul Cheetham <PA******@dsl.p ipex.com> wrote in news:#PHvtN6bGH A.1204
@TK2MSFTNGP02.p hx.gbl:

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


Change this line to this:

private SchedulingTool. ListBoxDragNDro p listBox1;
You will also need to change the new statement to:

listBox1 = new SchedulingTool. ListBoxDragNDro p();
The ListBoxDragNDro p 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 ListBoxDragNDro p) 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
459
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 to you reference the value of a column in a data table/view from the row filter string of a different data view. I'm using a strongly typed dataset. Below is more info.
11
2257
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 VB project. Create a subclass of a UI control, I used TextBox. Add no code to the subclass. Public Class Component1
7
2495
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 datasource, the items are properly displayed but the method private void OnDragEnter(object sender,
2
1605
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 the list boxes on the form and drag them into another empty list box that is not bound to any data when the form loads. I was wondering is it possible to use drag and drop with data binded list boxes ? If it is possible do you know of any place on...
3
10586
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
1
2448
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 apps. Is there a framework I can use to do this? John Dalberg
2
1317
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 field to hold source control name. 2. When mouse down, store name in this control. 3. Drag. 4. When drop, do postback from target control.
2
4341
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 and the refreshes are slow. (OT. I've often wondered why there is no .Sort or .Filter property for Combos and Listboxes.) My listboxes , and their rowsources, on my form were constantly being refreshed to filter and sort data. Ex:
0
1372
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 other listbox display the same text but still have the same value behind. so when i submit the lbTo data should read something like Smith John 7A with a value of 657576 for each person in the lbTo.
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.