简体   繁体   中英

Drag and drop not working

I have an upload/download web service that I created with WCF. Im using c sharp as the language.

I have allow drop enabled on my textbox that accepts the items to be dragged into it, but it does not allow me to do it, I still get that no sign hovering over it.

Is there something that Im missing? FYI I made another program using the exact same code and I was able to drag and drop items no problem.

    private void FileTextBox_DragEnter(object sender, DragEventArgs e)
    {
        //Makes sure that the user is dropping a file, not text
        if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            //Allows them to continue
            e.Effect = DragDropEffects.Copy;
        else
            e.Effect = DragDropEffects.None;
    }





    private void FileTextBox_DragDrop(object sender, DragEventArgs e)
    {
        String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);

        foreach (string file in files)
        {
            FileTextBox.Text = file.ToString();
        }
    } 

The 'normal' DragEnter , DragOver , Drop , ... events won't work for a TextBox! Use PreviewDragEnter , PreviewDragOver , PreviewDrop instead!

Also make sure that an DragDropEffects is set in the PreviewDragOver, and/or PreviewDragEnter delegate!

Small example: Drop a Folder to the textbox

XAML part:

             <TextBox
                Text=""
                Margin="12"
                Name="textBox1"
                AllowDrop="True"
                PreviewDragOver="textBox1_DragOver"
                PreviewDragEnter="textBox1_DragOver"
                PreviewDrop="textBox1_Drop"/> 

CodeBehind part:

    private void textBox1_DragOver(object sender, DragEventArgs e)
    {
        if(e.Data.GetDataPresent(DataFormats.FileDrop, true))
        {
            string[] filenames = e.Data.GetData(DataFormats.FileDrop, true) as string[];

            if(filenames.Count() > 1 || !System.IO.Directory.Exists(filenames.First()))
            {
                e.Effects = DragDropEffects.None;
                e.Handled = true;
            }
            else
            {
                e.Handled = true;
                e.Effects = DragDropEffects.Move;
            }
        }
    }

    private void textBox1_Drop(object sender, DragEventArgs e)
    {
        var buffer = e.Data.GetData(DataFormats.FileDrop, false) as string[];
        textBox1.Text = buffer.First();
    }

these aren't the only code that you need. you will need:

FileTextBox.AllowDrop = true;
FileTextBox.DragEnter += new DragEventHandler (FileTextBox_DragEnter);
FileTextBox.DragDrop += new DragEventHandler (FileTextBox_DragDrop);

Of course, if you're using an IDE, you can achieve this by assigning the handlers in the form designer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM