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.
Related
Setting VisualStyles.VisualStyleState.NonClientAreaEnabled in code shows a completely different dialog for OpenFileDialog call than when done without a VisualStyleState. The drop down for "View Menu" shows a vertical bar without text and the left browser pane is gone.
Image showing problem with Visual Style State set
In our application we need to set Styles as we have developed custom ones.
Issue reproducible on Windows 10 Build 1709, .Net 4.6.1 and default C# forms application. Also reproduced on Windows 10 build 1809. Works fine with all earlier versions of Windows.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Application.VisualStyleState = System.Windows.Forms.VisualStyles.VisualStyleState.NonClientAreaEnabled;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = false;
fileDialog.CheckFileExists = true;
fileDialog.Filter = " (*.sql)|*.sql";
fileDialog.ShowDialog();
}
}
Without VisualStyleState set, the OpenFileDialog shows a completely different UI, with browser pane on the left side and all drop downs work as expected.
Image showing default behaviour of OpenFileDialog
Any pointers to fix this issue would be helpful.
In one situation a dialog is rendered as an old style dialog and the other one as so-called “Vista-style” dialog:
old style
Vistay style
Here's the code the drives this decision:
https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/FileDialog.cs,986
A type of a dialog depends on whether VisualStyleState.ClientAreaEnabled is set or not. Because you set the style to VisualStyleState.NonClientAreaEnabled the app falls back to the old-style.
There is AutoUpgradeEnabled property that allow rendering the dialog as Vista-style, you could try setting it to true and see if it helps.
I have a WPF application which opens a popup when the main window is loaded. The problem is when I select the .xaml file in the solution explorer in Visual Studio 2013, the popup "pops" even when the application is not running. I suppose it is an intended behavior since the visualizer needs to execute the code in order to render the page layout, but for now I need to close it every time I load the page... I cannot temporarily disable this popup since it has some start logic for the application (selection of a location,...).
Here is the code of the popup trigger
public GeneralProcess() //usercontrol
{
InitializeComponent();
Loaded += GeneralProcess_Loaded;
}
void GeneralProcess_Loaded(object sender, RoutedEventArgs e)
{
var popup = new StationSelect();
popup.Owner = Window.GetWindow(this);
popup.ShowDialog();
}
Is there a way to know if the application is running or if I am in the visualizer, or is there a way to disable the Loadedevent just for visual studio ? The goal is to still be able to see the page for easy editing.
EDIT : this question is a duplicate. However this answer worked for me.
void GeneralProcess_Loaded(object sender, RoutedEventArgs e)
{
if (DesignerProperties.GetIsInDesignMode(this))
return;
var popup = new StationSelect();
popup.Owner = Window.GetWindow(this);
popup.ShowDialog();
}
i created a little form in C# and registered some drag drop events to it. But i cant get it to work properly!
Goal: Dragging files from explorer.exe into the form, and handeling it, and photos dragged into the form. I test the code by dragging a .txt file from my desktop into the form. It shows the (/) cursor when doing so
Code:
public partial class Viewer : Form
{
public Viewer()
{
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(Viewer_DragEnter);
this.DragDrop += new DragEventHandler(Viewer_DragDrop);
}
public void Viewer_DragEnter(object sender, DragEventArgs e)
{
Console.WriteLine("Viewer_DragEnter");// Line has breakpoint
}
public void Viewer_DragDrop(object sender, DragEventArgs e)
{
Console.WriteLine("Viewer_DragDrop: "+e.Data.GetFormats());// Line has breakpoint
}
}
What i have tried:
Try-catch over the constructor => nothing
Running succesfull build outside of visualstudio(2010)
Running explorer.exe as administrator when dragging items into the form
Thanks for reading my question
EDIT:
when testing the files were on the desktop(%userprofile%\Desktop). but it shouldnt matter where they are stored. the images are supposed to be dragged from the browser or a word document into the form
ANOTHER EDIT:
When trying to run it without visual studio, i get an InvalidOperationException, that occurs on the AllowDrop = true; but i dont get a reaction when try-catching it
SOLUTION:
OMG... so.... i kinda moved the public static Main() into the viewer.cs file.... and i forgot to add the STAThread attribute.
I found this when executing the build outside of visual studio again, and in the exception form it showed that the thread needed to be a STAThread but that part of the message was hidden and hard to find
anyway: ALWAYS USE STATHREAD :P
I'm having a problem that I've been trying to solve for days now, but without luck!
On my Windows Forms Application I have a grid. One column contains an email address. When the user double clicks this column, I want to open a new E-Mail Window via Outlook automation. This window should have the focus and allow the user to type immediately.
Everything works fine, when:
I'm running my app from Visual Studio.
Or my app has the focus.
However, when I run my .exe and outlook has the focus when I double click the column, the following happens:
The new Mail window opens as expected
The cursor blinks in the new mail window (as expected)
when the user starts typing, the cursor still blinks in outlook but the typed text appears in the grid of my application, not in outlook.
I was able to reproduce the problem with a simple form that has a textbox on it.
I use the following code:
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
OpenOutlookMail(textBox1.Text);
}
private void OpenOutlookMail(string to)
{
MailItem item = OutlookApp.CreateItem(OlItemType.olMailItem) as MailItem;
item.To = to;
item.Subject = string.Empty;
item.Body = string.Empty;
item.Display();
}
protected Application OutlookApp
{
get
{
if (mOutlookApp == null)
{
mOutlookApp = new Application();
}
return mOutlookApp;
}
}
What i already tried was to
Activate my current form via this.Activate() before the call to OpenOutlookMail
Activate the MailItem Inspector Object
Activate the ActiveWindow and ActiveExplorer of Outlook via Automation
Using AutoIt as explained here Similar Problem with MS Word on the MSDN Forum
Any help would be appreciated!
I wrote about focusing a background window some time ago:
http://blog.sebastianbrand.com/2010/02/activate-form-in-background.html
private void label1_Click(object sender, EventArgs e)
{
// mainform.BringToFront(); // doesn't work
BeginInvoke(new VoidHandler(OtherFormToFront));
}
delegate void VoidHandler();
private void OtherFormToFront()
{
mainform.BringToFront(); // works
}
If you do have an handle of the bad window, give that a try.
You can try to use Dispatcher.BeginInvoke(...) with some low priority in your textBox1_MouseDoubleClick(...) method to call OpenOutlookMail(). It often helps to workaround focus management issues like this one.
I haven't been able to reproduce the problem with your code. I've used Microsoft.Office.Interop.Outlook version 14.0.0.0 and in every tests i've done the mail window get the focus.
As you state,
Everything works fine, when:
•I'm running my app from Visual Studio.
•Or my app has the focus.
Maybe trying to focus your form and/or making your application sleep before opening the mail window would work
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Focus();
System.Threading.Thread.Sleep(500);
OpenOutlookMail(textBox1.Text);
}
Interops often have weird behaviors. :s
I'm having an issue dragging a file from Windows Explorer on to a Windows Forms application.
It works fine when I drag text, but for some reason it is not recognizing the file. Here is my test code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
}
else if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}
AllowDrop is set to true on Form1, and as I mentioned, it works if I drag text on to the form, just not an actual file.
I'm using Vista 64-bit ... not sure if that is part of the problem.
The problem comes from Vista's UAC. DevStudio is running as administrator, but explorer is running as a regular user. When you drag a file from explorer and drop it on your DevStudio hosted application, that is the same as a non-privileged user trying to communicate with a privileged user. It's not allowed.
This will probably not show up when you run the app outside of the debugger. Unless you run it as an administrator there (or if Vista auto-detects that it's an installer/setup app).
You could also run explorer as an admin, at least for testing. Or disable UAC (which I would not recommend, since you really want to catch these issues during development, not during deployment!)
The code you posted should work.
Try putting this at the beginning of the DragEnter method
string formats = string.Join( "\n", e.Data.GetFormats(false) );
MessageBox.Show( formats );
which will dump data formats associated with the d'n'd operation. Might help us narrowing down where the problem lies.
I added the code that arul mentioned and things still didn't work, but it got me thinking.
I started thinking it might be a Vista issue so I sent it to a friend that had Windows XP and it worked great! I then tried running it outside of the Release folder in the bin directory and what do you know it worked!
The only time it does not work is when I am running it inside the Visual Studio 2008 IDE ... that's just weird.
Did you try to add the STAThread attribute to the main method?
[STAThread]
static void Main(string[] args)
{
}
I had the same problem as #mattruma meaning i got not Drag&Drop events.
After adding the STAThread attribute to the main method it worked as expected.