Drag and drop from TreeView to Textbox

1

I am pasting an element of a Treeview to a Textbox , but I would like to paste the element in the position indicated by the mouse as well as show the bar that indicates where the element to be pasted will go, as shown in the next image or just like in the expression creator of the integration services

Here is my Code

private void tvOperador_ItemDrag(object sender, ItemDragEventArgs e)
{
    var node = (TreeNode)e.Item;
    if (node.Level > 0)
    {
        DoDragDrop(node.Text, DragDropEffects.Copy);
    }
}

private void txtExpresion_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(string))) e.Effect = DragDropEffects.Copy;
}

private void txtExpresion_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(System.String)))
    {
        string Item = (System.String)e.Data.GetData(typeof(System.String));
        string[] split = Item.Split(':');

        txtExpresion.Text += split[1];
    }
}
    
asked by EGSL 12.08.2016 в 00:45
source

1 answer

1

You can use the caret of txtExpresion :

private void txtExpresion_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(string)))
    {
        e.Effect = DragDropEffects.Copy;
        this.txtExpresion.Focus();       
    }            
}

private void txtExpresion_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(System.String)))
    {
        string Item = (System.String)e.Data.GetData(typeof(System.String));
        string[] split = Item.Split(':');

        string texto = this.txtExpresion.Text.Trim();    
        int i = this.txtExpresion.GetCharIndexFromPosition(this.txtExpresion.PointToClient(Cursor.Position));

        // El espacio agregado al final es para obtener, en la fórmula de arriba, la longitud de txtExpresion.Text (Length), cuando se intente anexar al final.            
        // En caso contrario se obtendría Length - 1, lo que dejaría escapar la última letra.
        // La función GetCharIndexFromPosition() devuelve un valor cuyo rango es [0..Length - 1].
        this.txtExpresion.Text = texto.Substring(0, i) + split[1] + texto.Substring(i) + " ";
        this.txtExpresion.SelectionStart = i + split[1].Length;
    }
}

private void txtExpresion_DragOver(object sender, DragEventArgs e)
{            
    int i = this.txtExpresion.GetCharIndexFromPosition(this.txtExpresion.PointToClient(Cursor.Position));
    this.txtExpresion.SelectionStart = i;
}
    
answered by 12.08.2016 в 09:16