Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

winforms - Why is DragDrop not working under VS2010?

I have a winforms app that uses a UserControl. The user control's job is to collect a file that the user drops on it from Windows Explorer, Open the file, determine the type and handle it accordingly.

This control worked PERFECTLY under Visual Studio 2008 Pro. I upgraded to VS 2010 Pro, and now, it doesn't work. Is there a flag or a property that has changed that I should be aware of??

I made a quick demo to test. This demo works perfectly under 2008, but doesn't work at all under 2010.

The setup: Create a new winform project. Add a user control. Set the following code in the user control's code section. (compile to get the user control to appear in the toolbox) Add the user control to the form. Run the program, and drag ANY file from windows onto the form. If it works, the user control area should change colors.

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        this.AllowDrop = true;
        this.DragDrop += new DragEventHandler(UserControl1_DragDrop);
        this.DragEnter += new DragEventHandler(UserControl1_DragEnter);
        this.DragLeave += new EventHandler(UserControl1_DragLeave);
    }

    void UserControl1_DragLeave(object sender, EventArgs e)
    {
        this.BackColor = Color.FromName("Control");
    }

    void UserControl1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
            this.BackColor = Color.Blue;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    void UserControl1_DragDrop(object sender, DragEventArgs e)
    {
        this.BackColor = Color.Yellow;
    }
}

I'm open to any explanation or fix that you guys may think up!

UPDATE:

I tested using the comments listed below. STILL doesn't work. However, I have noted that it only fails while in the development environment. When I go to the bin directory and launch the program manually, it works fine. It just doesn't work when I am in the development environment, which makes debugging a bit difficult. Still looking for the big-picture fix.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A likely failure cause here is UIPI, the user interface component of UAC. You cannot drag from a non-elevated process and drop to a window owned by an elevated process. You'll trigger this when you started Visual Studio from a shortcut that has the "Run this program as an administrator" option in the Compatibility tab turned on. The only workaround is to turn that option off. Or to run it directly from the .exe file, as you discovered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...