Link to home
Start Free TrialLog in
Avatar of Miguel Oz
Miguel OzFlag for Australia

asked on

Drag and drop Listview to RichTextBox sample does not work

I am using the code posted in this link:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop.aspx

Unfortunately it does not work even if I set EnableAutoDragDrop = true.

Any ideas why this code does not work as expected and how to fix it so that the code can drag and drop from the listview to RichTextBox

Note: 1) I am using VS2008 SP1, this is a winform project.
2) I added the following code to complement the sample:
        public Form2()
        {
            InitializeComponent();
            richTextBox1.AllowDrop = true;
            richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
            richTextBox1.DragEnter += new DragEventHandler(richTextBox1_DragEnter);

            // Add code here to populate the ListBox1
           for (int i = 0; i < 10; i++)
            {
                listBox1.Items.Add("Cat" + i.ToString());
            }
        }
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What happens with this code?  I see where that code is setting AllowEffect in the DragOver event handler (which is the usual culprit)?  Do you have AllowDrop = True for the target?
Avatar of Miguel Oz

ASKER

Check the last code sample on the link (this is the one that does not work), Posted the code from link below:

private void Form1_Load(object sender, EventArgs e)
{
   // Sets the AllowDrop property so that data can be dragged onto the control.
   richTextBox1.AllowDrop = true;
   richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
   richTextBox1.DragEnter += new DragEventHandler(richTextBox1_DragEnter);

            // Add code here to populate the ListBox1
           for (int i = 0; i < 10; i++)
            {
                listBox1.Items.Add("Cat" + i.ToString());
            }
}

private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
   // Determines which item was selected.
   ListBox lb =( (ListBox)sender);
   Point pt = new Point(e.X,e.Y);
   int index = lb.IndexFromPoint(pt);

   // Starts a drag-and-drop operation with that item.
   if(index>=0)
   {
      lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Link); //i tried DragDropEffects.Copy (no joy)
   }
}

private void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
   // If the data is text, copy the data to the RichTextBox control.
   if(e.Data.GetDataPresent("Text"))
      e.Effect = DragDropEffects.Copy;
}

private void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
   // Loads the file into the control.
   richTextBox1.LoadFile((String)e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText);
}
"Do you have AllowDrop = True for the target?" Yes, In note (2), you can see:
richTextBox1.AllowDrop = true;
I believe that you should have the following, but I can't see all of it.

1) DoDragDrop if mouse down on a valid ListBox item.

2) RichTextBox.AllowDrop = True

3) In ListBox.DragOver, set e.Effect.

I am still not quite sure what it happening when you run this code.  Those 3 steps are the minimum for drag and drop operations.
thanks for your time, I have done that and it does not work. keeo in min that if you replace the richtextbox for say a texbox, it works OK. It seems that richtextbox needs additonal code/settings than the typical net control.
What I mean that it does not work is that you can drag from listbox but the moment the mouse is over the richtextbox it can not drop even though dragenter is setting the effects to copy. but richTextBox1_DragDrop is never called.

By the way richTextBox does not have dragover (this is winforms - .net 2.0 component)
You can easily reporduce the code by creating a form and put a listbox and a richTextBox component then use code above.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works perfectly.
I agree with you that there is something wrong with M$ regarding this issue.