DragDrop on Buttons

S

sunnyz

Hi all
I wanna drag and drop from one button to another.I am not actually
dropping any item on other button but wanna know how can i do it
between two buttons.

i tried reading msdn ..it shows me one lengthy code using listboxes

waiting for response eagerly...

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
J

Justin Rogers

Can't get it any more basic than below.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

using System;
using System.Drawing;
using System.Windows.Forms;

public class ButtonDragDrop : Form {
[STAThread()]
private static void Main(string[] args) {
Application.Run(new ButtonDragDrop());
}

public ButtonDragDrop() {
this.b1.Location = new Point(5, 5);
this.b1.Text = "Button1";
this.b1.MouseDown += new MouseEventHandler(Button_MouseDown);

this.b2.Location = new Point(5, 28);
this.b2.Text = "Button2";
this.b2.AllowDrop = true;
this.b2.DragOver += new DragEventHandler(Button_DragOver);
this.b2.DragDrop += new DragEventHandler(Button_DragDrop);
this.Controls.Add(b1);
this.Controls.Add(b2);
}

private void Button_MouseDown(object sender, MouseEventArgs e) {
DataObject dataObject = new DataObject();
dataObject.SetData(DataFormats.Text, b1.Text);
this.DoDragDrop(dataObject, DragDropEffects.Copy);
}

private void Button_DragOver(object sender, DragEventArgs e) {
if ( e.Data.GetDataPresent(DataFormats.Text) ) {
e.Effect = DragDropEffects.Copy;
}
}

private void Button_DragDrop(object sender, DragEventArgs e) {
if ( e.Data.GetDataPresent(DataFormats.Text) ) {
string data = e.Data.GetData(DataFormats.Text) as string;
b2.Text = data;
}
}

private Button b1 = new Button();
private Button b2 = new Button();
}
 

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