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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With