Women in Technology

Hear us Roar



Article:
  Implementing Drag and Drop in Windows Forms
Subject:   Drag and Drop from Listbox to Richtextbox
Date:   2008-09-23 05:36:04
From:   samdoss
//Add two lines in Constructor
InitializeComponent();
this.richtextbox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.richtextbox1_DragEnter);


this.richtextbox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.richtextbox1_DragDrop);



Then


Event



private void Listbox1_MouseDown(object sender, MouseEventArgs e)
{
Listbox1.Refresh();
Listbox1.DoDragDrop(Listbox1.SelectedItem, DragDropEffects.Copy);
richtextbox1.DoDragDrop(Listbox1.SelectedItem, DragDropEffects.Copy);
}


private void richtextbox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
int i;
String s;


// Get start position to drop the text.
i = richtextbox1.SelectionStart;
s = richtextbox1.Text.Substring(i);
//richtextbox1.Text = richtextbox1.Text.Substring(0, i);
//// Drop the text on to the RichTextBox.
//richtextbox1.Text = richtextbox1.Text + e.Data.GetData(DataFormats.Text).ToString();
//richtextbox1.Text = richtextbox1.Text + s;
}


private void richtextbox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}


private void lstItemsFC_MouseDown(object sender, MouseEventArgs e)
{
lstItemsFC.Refresh();
lstItemsFC.DoDragDrop(lstItemsFC.SelectedItem, DragDropEffects.Copy);
richtextbox1.DoDragDrop(lstItemsFC.SelectedItem, DragDropEffects.Copy);
}



By,


Samdoss (Software Developer)