6

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.

Jerry
  • 4,507
  • 9
  • 50
  • 79
  • Goes from grey, to blue on hover, and yellow on drop in VS 2010 pro for me. (Just FYI). – KreepN Nov 14 '11 at 15:39
  • That's what it SHOULD do. Very odd!!! I wonder if my VS2010 is toast or something. Everything else works perfectly. Thank you for testing for me. Did you do ANYTHING different than the setup above? Any other enabled switches/flags/properties? – Jerry Nov 14 '11 at 16:11
  • I pasted the code verbatim into a user control, added it to a panel that resides on a form on form_load, and then tested it. No properties were touched. It could be that you adding it from the toolbox causes it to act funny. As I mentioned I added it programmatically: UserControl1 uc1 = new UserControl1(); this.panel1.Controls.Add(uc1); – KreepN Nov 14 '11 at 16:29

1 Answers1

13

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Holy crap!! That was it. I had to make it "administrator" due to another control I'd added recently that needed to write to the registry. Thank you so much. It was driving me nuts. – Jerry Nov 14 '11 at 20:11
  • @Hans Do you know of a way to launch an application with standard privileges from Visual Studio run under admin privileges? – d11 Oct 11 '12 at 06:45
  • Use Debugger.Launch() in Main() so you can start the program from Explorer and get a debugger attached after it started. – Hans Passant Oct 11 '12 at 08:13