I'm using Microsoft.Win32.SaveFileDialog class to save my files. When I saved file, and minimize my app, I can't restore it back. It happens only after when used Microsoft.Win32.SaveFileDialog. Here is code:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = String.Format("{0} {1} {2}", ev["b"], ev["a"], ev["c"]);
dlg.DefaultExt = ".csv";
dlg.Filter = "Supported format (.csv)|*.csv";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string s = dlg.FileName;
//other code
}
File saves successfully, but I don't know how to solve problem with minimizing. Does anybody knows what it could be?
WPF has all kinds of weird modality issues when you show dialogs without parent windows. I haven't seen this directly with the SaveFileDialog, but I have seen similar behavior with other dialogs. Try using the overload of .ShowDialog() where you pass in the parent window.
I encountered also a strange modality problem with WPF and the Win32 SaveFileDialog / OpenFileDialog.
What happens:
The modal state is violated / gets lost completely and the main window can be clicked while the OpenFileDialog is opened with ShowDialog()
When does it happen:
There was a task running before the OpenFileDialog opens
The debugger breaks into a breakpoint before running the task
Just create a simple WPF Application with a button click event:
private void Button_Click(object sender, RoutedEventArgs e)
{ // <-- Breakpoint sits here
Task.Run(() => {}).Wait();
new Microsoft.Win32.OpenFileDialog().ShowDialog();
}
Using the overloaded ShowDialog(Window owner) function solves this problem.
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'm a newbie with Visual Studio Express 2013 and C#. I've borrowed a simple C# Windows application which builds and runs fine, and now I want to add some objects to the main form.
I select an object from the Toolbox, then click in the Designer on my main form where I want it to appear. This works fine for some simple objects like Label, but when I select an OpenFileDialog object nothing appears on my form. An OpenFileDialog "box" appears in a bar below my form instead, and I can't drag it to my form (I get a slashed circle).
I'm surely missing something simple. Thanks for any help.
You cannot drag an OpenFileDialog to the form because it is a non visual control.
To add an OpenFileDialog,just double click the control in the toolbox.That will add it to the form.Now to show the dialog you have to use OpenFileDialog.ShowDialog() in the code behind.Here is an example which shows the dialog on the click of a button.:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
DialogResult result= openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
{
string s = reader.ReadLine();
}
}
I have a small application that needs to save to contents of a rich text box as a text file upon a button click. What I currently have is
//save button logic
private void saveBtn1_Click(object sender, EventArgs e)
{
//set up new SaveFileDialog
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Text Files (*.txt)|*.txt";
saveFileDialog1.Title = "Save a text File";
//prompt the user
DialogResult result = saveFileDialog1.ShowDialog();
//if they select save write the contents of the RTB to a text file
if (result == DialogResult.OK)
{
System.IO.File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
}
}
It seems like a pretty straight forward piece of code, but I can't figure out why the dialog opens twice.
Just expanding Servy's comment into an answer because I suspect his diagnosis is correct. I'm going to give some simple suggestions for diagnosing/finding the problem. Firstly, you can put a break point in the event handler, if you're taken into that event handler twice then that's obviously the problem. The other solution (without even running) is simply to right click on the method name and select "find all references" this will give you list of all the references to the this method within your code. I'm guessing you will find 3; the method itself, the place where you expect to be registering it, and the place you were you don't. Remove the one and your problem will go away.
Shortest answer: you called the save dialog() twice in your code. Remove the second one and your issue will clear up.
Because you are used twice.
Thisa is simple excample for you
SaveFileDialog save = new SaveFileDialog();
save.DefaultExt = "zip";
save.Filter = "zip faylı (*.zip)|*.7z|Hamısı(*.*)|*.*";
save.ShowDialog(); // this is first using
if (save.ShowDialog() == DialogResult.OK) // and second using save.ShowDialog()
{
// yours code here
}
I've run into an issue with the Microsoft.Win32.SaveFileDialog in our Wpf application.
If the user enters an enormous file path, above the allowed maximum (I think its 255 chars?), within the SaveFileDialog then it starts to become unusable (details of this are in the code example).
So as a work-around I want to close the dialogue and make them enter the file path again. However the problem is that the SaveFileDialog does not have a Close() routine or anything else that I can see to close it. How can I close the dialogue programmatically?
// error only seems to occur if a filter is specified.
var dialog = new Microsoft.Win32.SaveFileDialog
{
Filter = "My juicy file (*.mjf) | *.mjf"
};
try
{
dialog.ShowDialog();
}
catch (System.IO.PathTooLongException error) // handle
{
/*
* if I handle this exception (by displaying a message to the user)
* the dialog will remain open, but if the user attempts to use it
* and enter another filename a mixture of the following exceptions
* are raised:
*
* AccessViolationException
* FatalExecutionEngineError
* ExecutionEngineException
*/
MessageBox.Show(error.Message);
}
EDIT
Thanks for you answers/comments. I've just tested this on my Windows 7 box and it behaves as expected so this maybe an issue only on XP.
In WPF 4.0 on Windows 7 the SaveFileDialog is showing its own error dialog:
<long path?
The path is too long.
Try a shorter name.
with an OK button to dismiss the error dialog. That leads the user back to the original SaveFileDialog where they can change their value or Cancel.
For earlier versions where the behavior might be different, you can use the Windows UI Automation framework to programmatically click the 'Cancel' button on the SaveFileDialog.
if (dialog != null)
{
dialog.DialogResult = DialogResult.Cancel;
}
Try setting the dialog result to close the dialog.
Send the window a WM_SYSCOMMAND message with a wParam parameter of SC_CLOSE. This is the equivalent of clicking on the Close button in the upper-right corner of the dialog.
If the dialog click Make new folder, just start editing the name just create a folder and click OK, OK dialogrezalt returns, but in the property SelectedPath he will name the folder New folder, then there is the name of the default
This happens because when we create, just edit and click OK, this property is not updated and the method ShowDialog () returns.
How fix this problem?
Thank you!
I had the same problem - if you created a new Folder with the FolderBrowseDialog, the .SelectedPath showed "xxx\NewFolder" not whatever new name the user had given.
The problem went away once I explicitly gave the command, prior to displaying the dialog,
MyFolderBrowser.ShowNewFolderButton = True
I failed to simulate the problem you are describing, I have tested it:
Create a new Form Form1 add button1 to it and in the button1.Click handler copy this code:
private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
string path = dialog.SelectedPath;
Console.WriteLine(path);//will not print new folder if the file renamed.
}
}
}
It worked as expected either by creating a new folder and press enter two times. or by creating a new folder and click ok.
Are you using a third party UI Controls, theams...
Edit: You stated:
Yes, if this sample run at windows application, it work correct. But
my application is Excel add-in. And FolderBrowserDialog work that I
write at started post
So you are using a third party "Excel add-in", When using a third party with FolderBrowserDialog or OpenFileDialog.. you may notice a strange behavior depending on the third party..
The solution for the problem you described is either by disabling ShowNewFolderButton or implement your own custom OpenFileDialog.