Miguel Oz
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(richTextB ox1_DragDr op);
richTextBox1.DragEnter += new DragEventHandler(richTextB ox1_DragEn ter);
// Add code here to populate the ListBox1
for (int i = 0; i < 10; i++)
{
listBox1.Items.Add("Cat" + i.ToString());
}
}
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(richTextB
richTextBox1.DragEnter += new DragEventHandler(richTextB
// Add code here to populate the ListBox1
for (int i = 0; i < 10; i++)
{
listBox1.Items.Add("Cat" + i.ToString());
}
}
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?
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(richTextB ox1_DragDr op);
richTextBox1.DragEnter += new DragEventHandler(richTextB ox1_DragEn ter);
// 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.Mouse EventArgs 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[ind ex].ToStri ng(), DragDropEffects.Link); //i tried DragDropEffects.Copy (no joy)
}
}
private void richTextBox1_DragEnter(obj ect 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(obje ct sender, DragEventArgs e)
{
// Loads the file into the control.
richTextBox1.LoadFile((Str ing)e.Data .GetData(" Text"), System.Windows.Forms.RichT extBoxStre amType.Ric hText);
}
"Do you have AllowDrop = True for the target?" Yes, In note (2), you can see:
richTextBox1.AllowDrop = true;
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(richTextB
richTextBox1.DragEnter += new DragEventHandler(richTextB
// 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.Mouse
{
// 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[ind
}
}
private void richTextBox1_DragEnter(obj
{
// If the data is text, copy the data to the RichTextBox control.
if(e.Data.GetDataPresent("
e.Effect = DragDropEffects.Copy;
}
private void richTextBox1_DragDrop(obje
{
// Loads the file into the control.
richTextBox1.LoadFile((Str
}
"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.
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.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works perfectly.
I agree with you that there is something wrong with M$ regarding this issue.
I agree with you that there is something wrong with M$ regarding this issue.