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).
Related
As the title says, I have a WinForms window that is sometimes setting its DialogResult property to Cancel immediately after ShowDialog is called.
The window is displayed like this:
bool isSuccess = (ShowDialog() == System.Windows.Forms.DialogResult.OK);
Now most of the time, the DialogResult is set to None when the window is first instantiated, as it should be. In these cases, the window will remain open/active until the DialogResult is set to something other than None.
I have even changed this function to look like:
this.DialogResult = System.Windows.Forms.DialogResult.None;
bool isSuccess = (ShowDialog() == System.Windows.Forms.DialogResult.OK);
And still, sometimes, the DialogResult will immediately be Cancel.
There is no where in my code being hit that sets or interacts with the DialogResult property, anywhere, at any time, when it is automaticall set to Cancel.
Also if I break on the line immediately after ShowDialog is called, the entire stack trace leading up to the ShowDialog call is identical - line for line.
Given that nothing is actually interacting with the DialogResult property, I have absolutely no idea how to stop this.
How are you closing the window? If you close it using the X, the result is automatically set to DialogResult.Cancel (see here)
When a form is displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application.
EDIT:
If I'm understanding correctly, you also mention that it works correctly "when it's first instantiated". Does that mean you're re-using the same form after closing it? If so, this isn't supported. Once it is closed, you have to create a new instance of the form to show it again.
I have a form "Main" and in Main I am creating another form which I use .ShowDialog() to display it. I do this because I don't want the rest of the code to in Main to execute until after the new form is completed. I am allowing a user to minimize the new form to system tray.
The problem: When the form gets minimized it is returning DialogResult.Cancel to the Main form that called it, causing the next line to run early.
Code to create form from Main form:
for(int i = 0; i < lvAll.SelectedItems.Count; i++)
{
this.Hide();
this.run = new RunProfile(this.profiles[lvAll.SelectedItems[i].Text]);
DialogResult result = run.ShowDialog();
MessageBox.Show(result.ToString());
}
In the new form a user will get a list of files that copy... now the user can continue and copy those files and I would expect to return a result of OK and if not I assume they are going to cancel and return Cancel...
Is my only recourse to return Abort from the new form if a user specifies a cancel and assume that a cancel is someone trying to minimize? This just seems odd
You could handle this in a number of ways, one way is like what follows:
MainTestForm mainTestForm = new MainTestForm();
if (mainTestForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
You must set the DialogResult state when you close the form like
this.DialogResult = System.Windows.Forms.DialogResult.OK;
I hope this helps.
This is entirely by design. When you hide a modal dialog, the user doesn't have any way to get back to the program. Dialogs don't have a taskbar button and the rest of the windows in the app are disabled so cannot be activated. The only recourse the user would have is to kill your program with Task Manager.
So Winforms does the logical thing, it automatically closes the dialog to avoid this UI trap. And of course you'll get DialogResult.Cancel.
Use proper UI design, a dialog should always have its MinimizeBox property set to False.
I'm not sure what you are trying to achieve with that. However, to get the Dialog to return anything else, you have to set it in the form's (Dialog's) DialogResult property before the form is closed.
You may also use the Form_Closing event of the dialog to set the DialogResult property to what you want. This is done before the form closes.
Here is a quote from MSDN regarding ShowDialog:
When this method is called, the code following it is not executed
until after the dialog box is closed.
After further reading it does state the X does not close the form but hides it and so you must dispose of it:
When a form is displayed as a modal dialog box, clicking the Close
button (the button with an X in the top-right corner of the form)
causes the form to be hidden and the DialogResult property to be set
to DialogResult.Cancel. The Close method is not automatically called
when the user clicks the Close button of a dialog box or sets the
value of the DialogResult property. Instead, the form is hidden and
can be shown again without creating a new instance of the dialog box.
Because of this behavior, you must call the Dispose method of the form
when the form is no longer needed by your application.
It seems this same issues applies to minimizing a userform as well.
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.
I have a windows form with multiple controls and OK and Cancel buttons. In the Click event handler of OK button, it checks to see if there are errors on the form. I need the form to stay open if there are errors. But the code below is not working. If there are errors, it is closing the form and returning to the caller. And I have the below two lines to invoke and show the form.
PtForm paymentForm = new PtForm();
ptForm.ShowDialog();
private void btnOk_Click(object sender, EventArgs e)
{
this.ValidateChildren(ValidationConstraints.Visible);
string ErrorString = GetValidationErrors();
MessageBox.Show(ErrorString, "Errors");
if (!string.IsNullOrEmpty(ErrorString))
{
return;
}
//Processing
}
Thanks for any help.
There's nothing in this code that will close the form. Therefore, the culprit must be outside this code.
Did you set your OK button's DialogResult property to DialogResult.OK? That would explain why the form is closing. AFAIK, if you set DialogResult on the button, that's what happens -- there's no way to veto it in code.
So in that case, you would need to go back to the designer and set the button's DialogResult back to None. Then at the end of your btnOk_Click method, once you've validated all the input and decided that it's safe to close the dialog, add a line that sets your Form's DialogResult property to OK.
Remove DialogResult property of a button-i.e. set it to None.
If I call Close() in my WinForm, it seems that even though DialogResult is None at the time, right after I call Close() I see that it is set to Cancel.
Does this sound normal?
Or even easier, you can set DialogResult just after Close. For example, assuming ValidateSettings will show the user any problems with the form or return true otherwise:
private void btnOK_Click(object sender, EventArgs e)
{
if (ValidateSettings())
{
SaveSettings();
Close();
DialogResult = DialogResult.OK;
}
}
That is completely normal, as it is the intended behavior. However, it is not equivalent to clicking the red "X" in the top right corner of the Form if you are using an MDI or ShowDialog().
When a form is displayed as a modal dialog box, clicking the Close
button (the button with an X in the top-right corner of the form)
causes the form to be hidden and the DialogResult property to be set
to DialogResult.Cancel. The Close method is not automatically called
when the user clicks the Close button of a dialog box or sets the
value of the DialogResult property. Instead, the form is hidden and
can be shown again without creating a new instance of the dialog box.
Because of this behavior, you must call the Dispose method of the form
when the form is no longer needed by your application.
The DialogResult value can be overridden though:
You can override the value assigned to the DialogResult property when
the user clicks the Close button by setting the DialogResult property
in an event handler for the Closing event of the form.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult(v=VS.100).aspx