I have a TreeView and a Multiline Textbox together on the same form in a Windows form. I have drag and drop setup so that I can drag a node from the TreeView over to the textbox and insert text into the textbox (this is working).
I would like to enhance this so that as the mouse is dragged over the textbox some sort of indicator moves along through the text showing the user where the text will be inserted at, and when dropped it gets inserted at that position. Currently I just put the text at SelectionStart, but the drag operation does not update SelectionStart so its at whereever the user last had the cursor.
Here's my current code:
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
object item = e.Item;
treeView1.DoDragDrop(((TreeNode)item).Tag.ToString(), DragDropEffects.Copy | DragDropEffects.Scroll);
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
e.Effect = DragDropEffects.Copy | DragDropEffects.Scroll;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
textBox1.SelectionLength = 0;
textBox1.SelectedText = (string)e.Data.GetData(DataFormats.StringFormat);
}
}