I have a save file dialog set up to display but when I click off of it the dialog it disappears into the background without a tab or anything. My question is how do I make the dialog modal? If you don't know what I mean go into notepad hit save as and try to click off the dialog. You will see the window flashes and you get a nice sound informing you that you must do something in the dialog before doing anything else. I would like to achieve this effect with my dialog but I don't know how. I cannot simply use the Form.Modal property because that deals with forms and this isn't a Form. Could someone help me out here?
Thanks.
EDIT:
This is how I'm showing the dialog, it's running in XNA and when I click the save as button the EntrySelected() method is called:
private void EntrySelected(object sender, EventArgs e) {
if(sender == saveAsEntry) {
sfd = new SaveFileDialog();
thread = new Thread(ShowSaveDialog);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
...
}
private void ShowSaveDialog() {
if(sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
World.Save(sfd.FileName);
thread.Abort();
}
else {
thread.Abort();
}
}
Actualy your Dialog is a Form, As the above comment suggests there is not a Show method, you should be using ShowDialog() Command that opens it up as a Modal Dialog.
i.e.
SaveFileDialog1.ShowDialog();
Base on your edit, there is a version of ShowDialog where you assign the owner to the Dialog, maybe that will work for you.
SaveFileDialog1.ShowDialog(dialogOwner);
From above link:
This version of the ShowDialog method allows you to specify a specific form or control that will own the dialog box that is shown. If you use the version of this method that has no parameters, the dialog box being shown would be owned automatically by the currently active window of your application.
Related
I have a program that out of the blue started giving me errors (shown below). I think the problem is that the if statement below is never true. Could somebody explain what the if file is looking for? This occurs right as a screen is opened right in front of another. I thought this would check if the new screen is opened, but the if statement never comes true.
private void TeachSensor_Button_Click(object sender, EventArgs e)
{
TeachSensor_OPC();
UserProgram.Enabled = false;
User_Program_Teach_Senson TS = new User_Program_Teach_Senson(this);
if (TS.ShowDialog(this) == DialogResult.Yes)
{
OperationInitialize();
SetupMode_Button_Click(null, null);
SetupMode_Button.Focus();
PartCounterOPC.RunWorkerAsync();
}
}
ShowDialog() is a function of a form used to show the form as a dialog. The result of the function is returned when the dialog is closed. A dialog box with a message prompt would be set up to return a DialogResult value based on what the user did in the dialog box (e.g. clicked OK, returns DialogResult.OK; clicked Cancel or close button returns DialogResult.OK; same for Yes, No, etc.). In your case, the If statement would not be evaluated until the dialog box is closed.
Therefore, any code to run for initializing the dialog box must be inside the dialog form. After the user finishes the task in the dialog box, a result is returned by setting a value to Form.DialogResult in the dialog box (e.g. DialogResult = DialogResult.OK).
I have custom dialog box with a button and a link_label which i use as a button.
When the user clicks the label i want to open a certain folder at window explorer and keep the dialog open. I know which button the user clicks by determining what Dialog result to return.
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.DialogResult = DialogResult.No;
}
private void simpleButton1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
At another class i have a method which returns a bool value according to the dialogResult:
DialogResult res = dig.ShowDialog();
return res == DialogResult.No;
Then if this methods return true i need to open the win explorer and keep the dialog open until the button is clicked.
the problem is when im tring to prevent the dialog to closing with FormClosing event of MyDialog form, the explorer is not opened. And if i am not using FormClosing the explorer opens but the dialog is closed.
private void MyDialog_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.DialogResult == DialogResult.No)
{
e.Cancel = true;
}
}
EDIT: I cant use the Linkclicked event of the form because i want this dialog to be reusable and handle the result each time. (open different paths depends on where i use the dialog)
The reason why explorer is not opened is that when you reject the form close, the parent code doesnt continue to open explorer,
What you want to achieve is a bit unclear to me, but the way you do it is not possible, as a suggestion why not open the FolderBrowserDialog from inisde MyDialog based on label click and closing MyDialog on button click, rather than having the parent handle the decision.
I'm currently working on a WPF app which has multiple windows. From the "main" window, you should be able to close the entire app. Before the app will be closed, the client wants it to show a dialog box which basically asks "are you sure you want to close the app" and blocks every other window until the user answers.
I'm currently using MessageBox.Show() to create this dialog box, but for some reason it only blocks the main window.
Here's the simplest example of what I'm talking about; if you create a WPF window with two buttons:
private void openChildWindowButton_Click(object sender, RoutedEventArgs e)
{
var window = new ChildWindow();
window.Show();
}
private void openDialogButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this, "This should freeze all other windows");
}
Opening a dialog will completely freeze the first window. If you click on it or attempt any sort of interaction, the OS makes a "ding!" sound and flashes the border on the message box. But all of the other windows you've opened can be clicked, moved, resized, etc., and that's what I want to prevent.
As it turns out, there is a way to do this, but it's not pretty. It involves using the WinForms version of MessageBox and passing an undocumented option as the last property.
var result = System.Windows.Forms.MessageBox.Show("Are you sure you want to exit this app?", "Exit", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button2, (System.Windows.Forms.MessageBoxOptions)8192 /*MB_TASKMODAL*/);
Source: http://social.msdn.microsoft.com/Forums/vstudio/en-US/8d1bd4a2-455e-4e3f-8c88-7ed49aeabc09/messagebox-is-not-applicationmodal?forum=wpf
Hopefully this is helpful to somebody else in the future!
If this works in WPF like in Windows Forms you could just use:
MessageBox.ShowDialog()
chris
Above not working...
Edit:
But there is a workaround: style a Form like a MessageBox (use a fixed Border-Type) and then Show it using ShowDialog(). Then set the Forms Cacel and Ok Button in Properties to your Buttons and you can get a DialogResult just like in a MessageBox. Hope that Helps but it is also from Windows-Forms ;)
Ok, this is the main essence of what I am trying to achieve and the symptoms of what it is doing.
I have a main windows form. On this form the user can click on a button that will open up a new and seperate form. This form will have a button that is supposed to display a FolderBrowserDialog. Instead it simply locks up form2 and never displays anything.
Here is essentially the code I have dealing with the form etc.
This is in the first form that is loaded after i do some uninteresting things.
FORM1.cs
//do stuff
//In a button.click method I do the following
Application.Run(new Form2(myParameters1, 2, 3));
This is the second form that is called from the first form
FORM2.cs
//do more stuff with the parameters on load
//user clicks on a button
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.RootFolder = Environment.SpecialFolder.Desktop;
fbd.Description = "This is the browser dialog box";
if(fbd.ShowDialog() == DialogResult.OK)
{
//do stuff
}
}
After I click on the button the dialog box does not show up and the form2 gets locked from doing anything.
I have also attempted changing the
fbd.ShowDialog()
To
fbd.ShowDialog(Form2.ActiveForm)
with the same results.
Any help would be appreciated! If you need more info let me know and I can try to provide all that I can.
EDIT
I forgot to mention (and actually completely forgot) That the method that opens up the second form is a seperate thread.
So the first form starts a thread, which opens the second form, which is supposed to open a dialog which it is not. Now i think it has to do with the threading..
I have figured out my issue. It ended up being that the thread from Form1 that was opening Form2 was not able to open DialogBoxes because it was seperate from the UI thread entirely.
I ended up working around using that thread and just eliminated it completely which solved my problem. The dialog box opened up just as I wanted.
Thank you all for the responses though! They did help me figure out a few other things I failed to do correctly.
I had similar problem. Main GUI thread was creating a backgroundworker thread to write to database but when background thread is failed used to show a custom control dialog to save exception file. THis custom dialog was shown correctly however Browse button on it to open folderBrowserDialog to save the exception file wouldn't show. My custom control would show "Not Responding" in its title bar.
What I did was instead of calling Custom Control directly I made it call on UI thread itself
like this.
void DBThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
this.Invoke(new CrossThreadExceptionHandler(CatchInterThreadException), e.Error);
}
}
I have been dealing with strange problem. I am using KryptonForm in a project. I have a form (say form1) and I need to open another form on a button click from this form. Here is the code:
void btn_click(object sender, EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
var f = new Form2();
f.ShowDialog();
Visible = true;
ShowInTaskbar = true;
}
The problem is that when the Form2 closes it closes the Form1 also. I have tried setting DialogResult = DialogResult.None from Form2 but of no avail. Please help me.
I am always using this technique and this thing has never happened.
Yes, this code is troublesome. It goes wrong when the user closes the dialog. Windows must then find another window to give the focus to. There isn't any left in your app, your main window is invisible. It then picks a window of another app. Odds are good, for example, that this will be a window inside Visual Studio. A big one. Your main form now disappears behind it.
You need to make sure that your main window is visible again before the dialog closes. You can do so by subscribing to the dialog's FormClosing event handler. For example:
private void button1_Click(object sender, EventArgs e) {
using (var dlg = new Form2()) {
dlg.StartPosition = FormStartPosition.Manual;
dlg.Location = this.Location;
dlg.FormClosing += (s, ea) => this.Show(); // <=== Here
this.Hide();
if (dlg.ShowDialog() == DialogResult.OK) {
// etc...
}
}
}
Bugged me for days!! Found this: https://bytes.com/topic/net/answers/769433-c-showdialog-inside-showdialog-closing-both-return
The result was being passed down and I dont know why. But if after the .ShowDialog() I just put this.DialogResult = DialogResult.None, it will fix it. This shouldnt happen in the first place, but this fixes it, so I am not too bothered.
You can also try changing the dialogResult on the button itself to "None" or deleting the this.Btn1.DialogResult... from the designer which worked for some people.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/543093ad-1860-4428-bae1-b0d4f112e04b/showdialog-closes-parent?forum=csharpgeneral
I know this is an old post, but I ran into this, and in my case the accepted answer (at the time I am writing this) is not helpful at all. The answer by #blind Skwirl led me to the culprit.
After 20 years of .Net programming (since it was introduced), I never noticed that BUTTONS have a "dialogresult" property. I always just set the forms "cancelbutton" and "acceptbutton" properties. What I found in my case was that (because I was doing a lot of copy-pasting of buttons), I had a bunch of buttons (not forms) that themselves had their "dialogresult" property set to "cancel", which meant that I would click a button on a dialog that would open another dialog, the "ok" button on the dialog had its result set to "cancel", and the button on the parent form ALSO had its result set to "cancel", so the dialog would close (with a result of cancel) and then the PARENT form would close with a result of cancel, confusing the heck out of me... so...
Just make sure all of your buttons have their dialogresult property set to NONE (or whatever the actual proper setting is that you want).
Bottom line, if a BUTTON (not the form) has its dialogresult property set to anything other than NONE, the form will close with that result when it is clicked (after any click event code has completed).
I hope that helps someone out there.