No focus on other form after FolderBrowserDialog.ShowDialog() - c#

I am trying to make a folder selection which would show an error if there isn't enough space on the selected drive. I have created a custom designed error and dialog form, but there is a problem with using the FolderBrowserDialog.
Here is my actual code:
frmDialog dialog = new frmDialog("Install software", "The software cannot be found. Please select the path of the executable or let the launcher install it for you.");
dialog.SetYesButtonText("Install software");
dialog.SetNoButtonText("Browse for executable...");
if (dialog.ShowDialog() == DialogResult.Yes)
{
fbd = new FolderBrowserDialog();
fbd.Description = "Please select where do you want to install the software!";
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK) // + space checking, but I deleted it for debugging now.
{
frmError error = new frmError("Not enough space", "Please select a folder with at lease 22 MB of free space.");
error.ShowDialog();
}
}
I will actually make a loop afterwards which will run until the user selects a folder with enough space or cancels the selection.
The problem is that the error dialog does not get any focus. So when the user selects the folder, the FolderBrowserDialog disappears, and the error dialog shows up in a new window, but the Visual Studio's window gets the focus instead of the error dialog. As I experienced, this issue is not existing with my own forms, so if I changed the fdb to frmDialog, all the three dialogs would appear with focus after each other.

Set the owner of the dialogs like this:
fbd.ShowDialog( dialog );
error.ShowDialog( dialog );
I recommend to set the owners of the other dialogs to set a parent child relationship. So when you close the parent form, the child forms closes to. And also put a using block around your forms if you're using the ShowDialog calls.

Related

Why does my function to only open one Window not work?

so I want in my Project to only open 1 instance of my Window. So I gave the Window a Title and tried to track every opening with that:
foreach (Window window in Application.Current.Windows)
{
if (window.Title == "QUALI-NET")
{
temp++;
}
}
and then i wanted to call my Function when this if statement is true:
if (temp == 1)
I have build this 2 in an extra Class and have an Switch Case around this. Above the Switch Case I initiliaze this:
QualiWindow WPFQuali = new QualiWindow(Mandant, Data.GetValue<string>("Artikelnummer"));
But The problem when I Open one Window and then open and another Window then it wont open but when I close the first started Window, I cant open the Window ever Again? I just want to allow one Instance of this Window to open. What am Im doing wrong?
I already tried the solutions from here:
How can I make sure only one WPF Window is open at a time?
But none of that is working. Is there a Way to get every opened Window from taskbar or something and just allow one Window with that name XY. to open
If you're keeping a count of the open windows with that title you also need to decrement the count whenever you close the window.

How to simulate pressing save/enter in SaveFileDialog

I'm creating an application to automate some processes. One of them is creating docx file from the dotx template.
Steps are quite easy: app opens MS Word with test.dotx file and SaveAs it to c:\temp as a test.docx. It should be as close to user's actions as possible. When the file is opened (from dotx so it is docx already) all I need is to open SaveAs dialog and push "save" (or just "enter", because focus is set on "save" button).
The problem is how to "hit" the save/enter. I tried SendKeys but I am in ShowDialog() which is waiting for the result and cannot perform SendKeys at the moment. Of course if I press enter from keyboard or cklick on "Save" all works perfectly, but this one "press" I'd like to do from the code. Could you please point me how to solve this (if it is possible at all)? Thank you.
Here is the part of the code I'm strugglig with:
SaveFileDialog saveFileDialog1 = new SaveFileDialog
{
InitialDirectory = #"C:\Temp\",
DefaultExt = "docx",
FileName = "test"
};
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
object FileName = saveFileDialog1.FileName;
doc.SaveAs(ref FileName);
}
If hit the save/enter is your requirement, the recommendation is use Spy++ (this Tool can be installed by Visual Studio Installer) to capture both: "the dialog with save button" and "save button" alias/class; then, programming with C# PInvoke (for example, using FindWindow and FindWindowEx) to send/post message to simulate this "save" button.

Select File and Continue Program In WPF

I've been trying to follow tutorials and various Stack Overflow posts, etc. to implement an OpenFileDialog to select a file. The problem is, it seems I can't get my program to continue with the rest of the logic. Not entirely sure if it has something to do with the fact I'm trying to open the file dialog inside my main window or what. Consider the following snippet:
public MainWindow()
{
InitializeComponent();
string file = "";
// Displays an OpenFileDialog so the user can select a file.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Files|*.txt;*.out";
openFileDialog1.Title = "Select a File";
openFileDialog1.ShowHelp = true;
// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//file = openFileDialog1.FileName;
//file = openFileDialog1.OpenFile().ToString();
//openFileDialog1.Dispose();
}
openFileDialog1 = null;
Console.WriteLine("File path is: " + file);
As you can see, I've even tried setting the "Help" value to true before the dialog finishes. I've tried to select both the file name for the file string, etc. but to no avail - the program seems to simply wait after the file is selected from the dialog. Would anyone here have a suggestion for a solution?
Previously I had the same problem with WPF. When you are working with WPF, System.Windows.Form Namespace is not included to your Project references;
and in the other hand actually, there are two OpenFileDialog, the first is System.Windows.Forms.OpenFileDialog (this is what you have) and the second is Microsoft.Win32.OpenFileDialog. if you want to get your code to work you must add System.Windows.Forms into your references:
Solution Explorer -> YourProject -> References (Right Click and Add Reference...) -> Assembly -> Framework -> Find And Select System.Windows.Forms -> OK
and the Next Solution is to use Microsoft.Win32, it's pretty easy. just add this Namespace into your code file and Change your code like this:
string file = "";
// Displays an OpenFileDialog so the user can select a file.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Files|*.txt;*.out";
openFileDialog1.Title = "Select a File";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog1.ShowDialog() == true)
{
file = openFileDialog1.FileName;
//file = openFileDialog1.OpenFile().ToString();
//openFileDialog1.Dispose();
}
openFileDialog1 = null;
Console.WriteLine("File path is: " + file);
OpenFileDialog.ShowDialog() is a modal method:
FileDialog is a modal dialog box; therefore, when shown, it blocks the
rest of the application until the user has chosen a file. When a
dialog box is displayed modally, no input (keyboard or mouse click)
can occur except to objects on the dialog box. The program must hide
or close the dialog box (usually in response to some user action)
before input to the calling program can occur.
This means that invoking this method will block your main thread until the dialog box is closed. A few of the options you have are:
Invoke the FileDialog after the main window is initialized.
Invoke the FileDialog from a thread. In this case, be careful to synchronize.

How to close a windows form after message box OK is clicked?

Program has two forms: Login and the main form after login.
Upon launching of the program, it connects to the database and checks if there is a new version available, if so, it instantly displays a MessageBox letting the user know to download the new version.
When the user clicks OK, the application needs to be closed so the user can no longer use it until the new version is downloaded. The problem is, after clicking okay, the login form is still shown. My code for the class is below:
DialogResult dialog = MessageBox.Show("FleetTrack™ update required.\n\nA new version of FleetTrack™ is available on your Driver Hub. You must download"
+ " the latest update to use FleetTrack™.", "FleetTrack™ Update Required", MessageBoxButtons.OK);
if (dialog == DialogResult.OK)
{
Application.ExitThread();
}
Not too sure what I need to do. The application successfully shows the pop-up if the version running is different than what is shown in the database, but after clicking OK it just loads up the login form like normal.
Use Application.Exit() and not Application.ExitThread()
And if you are displaying the Dialog box before Application.Run(), then all you need to ensure is that you do not call the Application.Run() if a version update is required.
if (updateRequired)
{
DialogResult dialog = MessageBox.Show("FleetTrack™ update required.\n\nA new version of FleetTrack™ is available on your Driver Hub. You must download"
+ " the latest update to use FleetTrack™.", "FleetTrack™ Update Required", MessageBoxButtons.OK);
if (dialog == DialogResult.OK)
{
Application.Exit();
}
} else
Application.Run(new Login());
updateRequired is a boolean you maintain to check if an app update is required.

Save Dialog on top of another custom dialog is behaving strangely!

I have a save as image feature for charts in my application. The chart control is a custom user control with custom logic in them. It also has some scaling based on size, zoom etc. However, while saving them as an image I would like to give the user the option to set the size of the image (eg: 800x600 px # 300 DPI).
To do this I have created a Form with textboxes/checkboxes etc for various settings for image. One of these TextBoxes is for the file name. The file name textbox is readonly and is accompanied with a browse button which shows a SaveFileDialog when clicked.
The user clicks "Save As Image" in the main form's menu. I show the ImageExportDialog using the code below:
using(ImageExportDialog dlg = new ImageExportDialog())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
//get the settings selected by the user and generate the image
}
}
In the ImageExportDialog, the user clicks on the browse button and the SaveFileDialog is shown as follows:
using(SaveFileDialog dlg = new SaveFileDialog())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = dlg.FileName;
}
}
Now the problem is, when the user clicks on "Save" button in the SaveFileDialog, as expected the txtFileName.Text is set, but the parent custom dialog also seems to return from the ShowDialog method and the DialogResult is the same as the one for SaveFileDialog! The control then goes on to the "get the settings selected by the user and generate the image" part of the code above.
Not really sure what I am doing wrong here!
Arghhh!!!
Found out the issue myself. I had copy-pasted the OK button of the ImageExportDialog to create the Browse button for the SaveFileDialog.
Guess what, the Browse button had it's DialogResult property set to "OK"! Changing it to "None" solved the issue.

Categories