Why can I not close a form in C#? - c#

I cannot close one of my forms programmatically. Can someone help me?
Here's the code:
private void WriteCheck_Load(object sender, EventArgs e) {
SelectBankAccountDialog sbad = new SelectBankAccountDialog();
DialogResult result = sbad.ShowDialog();
if (result == DialogResult.Cancel) {
this.Close();
} else {
MessageBox.Show(result.ToString());
}
MessageBox.Show(sbad.bankaccountID.ToString());
}

As configurator mentioned (in comments), the form must be shown before it can be closed, so, instead of the Load event, you should be doing this in the Shown event instead.
If you don't want the form visible for the Dialog box, I guess you can wrap the event code in a Visible = false;
In summary, the basic code would be
private void WriteCheck_Shown(object sender, EventArgs e)
{
Visible = false;
SelectBankAccountDialog sbad = new SelectBankAccountDialog();
DialogResult result = sbad.ShowDialog();
if (result == DialogResult.Cancel) {
this.Close();
} else {
MessageBox.Show(result.ToString());
}
MessageBox.Show(sbad.bankaccountID.ToString());
Visible = true;
}

By calling Form.Close(), the form should close, but not until all waiting events have been processed. You also still have a chance to cancel the form closing in the FormClosing event.
First, you'll probably want to return after your call to this.Close(). If it still doesn't close, step through your code and see what is happening. You may have to set and check a "forciblyClose" flag and return from any other processing methods before it'll actually close.

The actual problem is that windows won't let a form close on it's load method. I have to show the dialog on construction, and then if the dialog result is cancel I have to throw an exception and catch the exception on form creation.
This place talks more about it

Related

C# Dialog keeps pop up when click yes in Form_FormClosing

Adding an event in my C# winForms the FormClosing but the dialog keeps pop-up after adding the code Close()
private void AdminPanel_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = (MessageBox.Show(this,
"Are you sure you want to close the Application?",
"Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question));
if (result == DialogResult.Yes)
{
//dialog keeps poping up when i try to close the form
Close();
}
else {
e.Cancel = true;
}
}
Have you tried removing the Close(); call? You're already in a Closing event handler – unless you cancel the close, it's going to happen anyway.
You don't need to add a call to Close method inside FormClosing because you are already in the form close call pattern of Application.
You need to write:
private void AdminPanel_FormClosing(object sender, FormClosingEventArgs e)
{
var result = MessageBox.Show(this,
"Are you sure you want to close the Application?",
"Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) );
if ( result != DialogResult.Yes )
{
e.Cancel = true;
}
}
If DialogResult.Yes then it does nothing to e.Cancel and the form is closed and the FormClosed event is called.
If e.Cancel is set to true then the Close call is cancelled by the Application manager and FormClosed is not called.
If you add a call to Close in FormClosing you will get a stack overflow : the box is shown and shown and shown and shown and shown and so on without end as long as you click on Yes, unless you click on No...
If you want to force the application ending you can use in the FormClosed event:
Environment.Exit(0);
But perhaps you have something somewhere that prevents the close.
Do you have code in FormClosed ? Does it throw an exception ? Else you need to catch it because it stops the close.

Receiving password input using DialogShow() not working

I am writing a WinForm application in C#. There is a Form A that opens Form C on Button click. Now, I want to add a password input screen Form B before opening Form C. Only if the password entered is correct will Form C open, and otherwise, show an error message. Form B just has a TextBox control and a Verify Button control.
/*Form A*/
FormB fb;
private void BtnClick(object sender, EventArgs e) {
DialogResult result;
//fb can open once at a time
if(fb == null){
fb = new FormB();
fb.FormClosed += new FormClosedEventHandler(FormB_FormClosed);
//This seems to be not working
result = fb.ShowDialog();
}
else //FormB already open. Do nothing
return;
//Only if password entered is correct, proceed
if(result == DialogResult.Yes){ //result == cancel
//Code to open Form C //Program never reaches here
}
}
//Upon closing FormB, reset it to null
private void FormB_FormClosed(object sender, FormClosedEventArgs e){
if(fb != null)
fb = null;
}
/* Form B */
private const string password = "xxxyyyzzz";
private void BtnPW_Click(object sender, EventArgs e){
bool result = Verify();
if(result){
BtnPW.DialogResult = DialogResult.Yes;
}
else{
MessageBox.Show("Error: Incorrect password");
BtnPW.DialogResult = DialogResult.No;
}
this.Close(); //Added
}
private bool Verify(){
if(TxtBox.Text == password)
return true;
else
return false;
}
Can someone tell me what is wrong with this code? It never reaches the second if statement in Form A.
Edit: Even if I enter the correct password and hit the button on Form B, result in Form A gets "DialogResult.Cancel`.
If you call the Form.Close method, then the DialogResult property of that form is set to DialogResult.Cancel even if you have set it to something else. To HIDE a form opened modally you just need to set the form's DialogResult property to anything but DialogResult.None.
Said that your code seems to be not the one usually used to handle a modal dialog.
ShowDialog is blocking your code, you don't exit from this call until the called form is closed or hidden, so you don't need to keep a global variable of FormB around and handle the FormClosed event handler of FormB in FormA.
private void BtnClick(object sender, EventArgs e)
{
using(FormB fb = new FormB())
{
// Here the code returns only when the fb instance is hidden
result = fb.ShowDialog();
if(result == DialogResult.Yes)
{
//open Form C
}
}
}
Now you should remove the call to Form.Close in the FormB code and set directly the DialogResult property of the FormB, do not try to change at this point the DialogResult property of the buttons, this will not work and you need a second click to hide the form, instead set directly the form's DialogResult property.
private const string password = "xxxyyyzzz";
private void BtnPW_Click(object sender, EventArgs e)
{
if(Verify())
this.DialogResult = DialogResult.Yes;
else
{
MessageBox.Show("Error: Incorrect password");
this.DialogResult = DialogResult.No;
}
}
In this way the form is hidden (not closed) and your code exits from the ShowDialog call in the FormA. Inside the using block you can still use the FormB instance to read its properties and take the appropriate paths. When your code exits from the using block the fb instance will be automatically closed and disposed out of existence.

Keeping the parent DialogButton open after the child DialogButton is closed

I'm having a problem in keeping the Parent Form to be opened till I close after using ShowDialog() .
I've been trying regarding this but couldn't get. I think there is something simple that I might have been missing. Can you please help me reg this?
The problem is,
I have Form 1, on pressing one button, Form 2 opens.
I do some validations in Form 2, and check for the validations. If the validation doesn't pass, I open a DialogBox form, with Retry and Cancel.
If I press Retry, The control should go back to the Form 2 and form 2 should not close.
If the press Cancel, both the DialogBox form and the Form 2 should close. Right now, regardless of what I press, both the forms close.
I have looked online and couldn't find any solution. Went through this solution, but both the forms are still closing for me.
Why does closing a nested child dialog also close the parent dialog?
My code:(Sample example scenario)
Form 1:
private void button1_Click(object sender, EventArgs e)
{
Form2 testForm = new Form2();
DialogResult dialogResult = new DialogResult();
dialogResult = testForm.ShowDialog(this);
if(dialogResult == DialogResult.OK)
{
//Do something
}
}
Form 2:
private void button1_Click(object sender, EventArgs e)
{
DialogResult validDataResult = MessageBox.Show("Invalid Data Entered. Please provide the correct data."
, "Data Management"
, MessageBoxButtons.RetryCancel);
if (validDataResult == DialogResult.Cancel)
{
this.Close();
}
}
in Form2.cs do your validation and then
(assuming validationOK is the true/false result of your checks)
if(validationOK == false)
{
// Ask retry or cancel to the user
if(DialogResult.Cancel == MessageBox.Show("Validation Fail", "Validation failed, press retry to do it againg", MessageBoxButtons.RetryCancel))
this.DialogResult.Cancel; // Set the dialog result on form2. This will close the form.
// if you have the validation done in a button_click event and that button has its
// property DialogResult set to something different than DialogResult.None, we need
// to block the form2 from closing itself.
// uncomment this code if the above comment is true
// else
// this.DialogResult = DialogResult.None;
}
You have to set the DialogResult of Form2 before you can call this.Close(). Otherwise, it remains the default. (The below code is only a guess of the actual double close logic since you did not specify that)
Form2.cs:
if (validDataResult == DialogResult.Cancel)
DialogResult = DialogResult.Cancel;
else
DialogResult = DialogResult.OK;
Close();
Form1.cs:
if(dialogResult == DialogResult.OK)
{
Close();
}
else
{
//Do something
}

How can I fire an event so my other form reacts to it?

I have this form called MainForm.cs. When I click Enter The Progam button I display another form using the .ShowDialog() method.
In this new form called LoginBox.cs, I check if the entered credentials are valid and if they are I want my MainForm.cs to react with either a positive responde (the actual software form opens) or negative response a MessageBox alerting him of the failure.
It's a very simple use case, but I don't know how to solve this correctly and efficiently. Thank you.
private void button1_Click(object sender, EventArgs e)
{
LoginBox login = new LoginBox();
login.ShowDialog();
}
//And in the LoginBox.cs file:
private void button1_Click(object sender, EventArgs e)
{
if ((txtUser.Text == "admin") && (txtPassword.Text == "123"))
{
}
}
If you open the form with ShowDialog it returns a DialogResult which you can check in your main form.
LoginBox login = new LoginBox();
DialogResult dialogResult = login.ShowDialog();
if (dialogResult == DialogResult.OK)
{
// etc...
}
You can set the value of DialogResult in your LoginBox form:
DialogResult = DialogResult.OK;
Others have mentioned using DialogResult, which can work--but might be abused a bit in this use-case. Its intended purpose is to let the parent form know what the user did on a child form--did they click OK or Cancel? Did they click Retry or Abort? It's not intuitive that it should be used for authentication purposes.
So--what's better? Probably a combination...
Your LoginBox class is a Dialog, so returning a DialogResult should be expected--but should also only be used to indicate what the user did on the Form, not the outcome of the authentication.
I would suggest looking into the usage of some other dialogs, such as OpenFileDialog. It returns a DialogResult to specify whether to go ahead with the file opening, but it doesn't actually open the file until being explicitly told to do so. This means the consuming code has to both check the result and instruct the dialog to perform it's function, so it's not perfectly simple--but it's fairly conventional.
Here's an example of how I would suggest you use LoginBox:
private void button1_Click(object sender, EventArgs e)
{
LoginBox login = new LoginBox();
if (login.ShowDialog() == DialogResult.OK) // Let the user input their credentials and click OK or Cancel
{
if (!login.ValidateCredentials) // Perform the authentication with the collected credentials
{
MessageBox.Show("The specified Credentials were invalid!");
}
}
}
Add an Event to the LoginBox. Then have the MainForm handle that event. In the event handler proceed with the additional logic that you want to perform.

Form closing save changes before close

I have a Windows forms application in C# and I have a form that when the user closes it I ask, "do you want to save the changes"? How can I get the changes in my form? Here is some code:
public partial class DepartEdit : Form
{
string _nameDep; //This variavel get value textbox when form load
{
InitializeComponent();
}
private void DepartamentEdit_FormClosing(object sender, FormClosingEventArgs e)
{
if (txtNameDepart.Text != _nameDep && codDepartament > 0)//Here i compare
{
DialogResult dlg = MessageBox.Show("Save changes?", "Question", MessageBoxButtons.YesNo);
if (dlg == DialogResult.Yes)
{
saveDepart(); // Metod save depart
e.Cancel = false;
}
if(dlg ==DialogResult.No)
{
e.Cancel = false;
}
}
}
There are a lot of textboxs and combo boxes? Is there any other way to get the changes in the form?
A lot will depending on where the information is held.
It you are using DataBinding you should be just monitoring the listChanged event or calling dataTable.GetChanges() if you are using a DataTable.
If the information comes from a class the implements ICloneable and IComparable, then you can take just take a backup copy when intialising the form (and after saving) and when closing you prepare you class for saving and compare it with the original.
Otherwise you can do something like
Declare a private variable
private bool requiresSaving =false;
Declare an event
private void SomethingChanged(object sender, EventArgs e)
{
requiresSaving = true;
}
Hook up this event to the various changed events, eg
this.txtNameDepart.TextChanged += new System.EventHandler(this.SomethingChanged);
(The actual event is sometimes called something different , eg ValueChanged, SelectedIndexChanged , but they can all point to SomethingChanged unless you need a particular event to do something else.)
Check this variable when you are closing the form
private void DepartamentEdit_FormClosing(object sender, FormClosingEventArgs e)
{
if (requiresSaving)
{
....
You also need to set requiresSaving false in the saveDepart method.
Alternatively I have seem code where, when the control is being intialised, the tag value is also set, and the formclosing event loops through each control and compares the original values (in the tag object) with the current values.
Create a string (or string[] I guess) within the Form_Load event and initialise them with the values present when the form first opens.
eg
string oName = nameTextBox.Text;
string oCompany = companyComboBox.Text;
Then during the Form_Closing() event you can check these against the current values
private void Contact_FormClosing(object sender, FormClosingEventArgs e)
{
if (oName!=nameTextBox.Text||oCompany!=companyComboBox.Text)
{
DialogResult result = MessageBox.Show("Would you like to save your changes",
"Save?",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Stop);
if (result == DialogResult.Yes)
{
SaveFormValues();
}
else if (result == DialogResult.Cancel)
{
// Stop the closing and return to the form
e.Cancel = true;
}
else
{
this.Close();
}
}
}
Loop the forms controls, and add your event watchers. These events will call a function in the form that will keep a Hashtable or some other various collection up to date w/ the status of any changes to a particular control.
like I have, Hashtable changes;
then each time my event is called, i say, Add Control.Name, and then a change status, whatever you want. Then you have a list of controls that have been updated.
I can go into more detail if need be, hopefully this will get you started.
Well I think the theory is good. There are some problems with the implementation.
if (dlg == DialogResult.Yes)
{
saveDepart(); // Metod save depart
e.Cancel = false;
}
if(dlg ==DialogResult.No)
{
e.Cancel = false;
}
I think it would be a lot similier to write this:
if(dlg == DialogResult.Yes)
{
saveDepart();
}
// You don't need to change e.Cancel to false here unless you set it to true previously.
It's simple
Declare a global variable count=0
Now on your form u might already have a Save button so just increment count on save button click event
Now on form close event just check for the value of count if it's 0 ask user to save info either by reminding by message box
If it's 1 or greater than1 just close the form
I hope u get it

Categories