Hide and Close is not working in all my form - c#

I have a label with a text "X" and I use this to close my forms. I have this code in my login:
private void btnLogIn_Click(object sender, EventArgs e)
{
if (userLevel.IndexOf("ADMIN") + 1 > 0)
{
Save_Data();
using (AdminGateOpt adminOptions = new AdminGateOpt())
{
adminOptions.gUsers = User;
adminOptions.windowNumber = windowNumber;
adminOptions.userDetails = userLevel.Split('|');
adminOptions.ShowDialog();
Close();
}
}
}
..and if the userlevel is an admin, then it will go to another form:
private void btnInGate_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Do you want to go in In-Gate Form?", String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
ReadBarCodeInGate gateForm = new ReadBarCodeInGate();
gateForm.gUsers = gUsers;
gateForm.windowNumber = windowNumber;
gateForm.userDetails = userDetails;
gateForm.ShowDialog();
Hide();
}
}
private void buttonExit_Click(object sender, EventArgs e)
{
login Login = new login();
Login.ShowDialog();
Hide();
}
And it's also the same to other forms that suddenly it became like this, that once I hit the X label button it will go to another form but it wont close nor hide the previous form/s.

Related

Close button is not closing the form properly

My form contains a button named as close. I just put the below code in the close button click... Then the 2nd code set is for the form closing. But if I execute this when I click the close button a MessageBox will appear. When I click the "Yes" button, the form is not closing but if I click the "Yes" button second time the form will be closed. What is the reason? Can you, please, help me?
private void btnClose_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are You Sure You Want To Close This Form?",
"Close Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// MessageBox.Show("The application has been closed successfully.",
// "Application Closed!",
// MessageBoxButtons.OK);
System.Windows.Forms.Application.Exit();
}
else
{
this.Activate();
}
}
-------------------------------------
private void frminventory_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are You Sure You Want To Close This Form?",
"Close Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
System.Windows.Forms.Application.Exit();
}
else
{
this.Activate();
}
}
Do not close/exit Application, but Form:
private void btnClose_Click(object sender, EventArgs e) {
// On btnClose button click all we should do is to Close the form
Close();
}
private void frminventory_FormClosing(object sender, FormClosingEventArgs e) {
// If it's a user who is closing the form...
if (e.CloseReason == CloseReason.UserClosing) {
// ...warn him/her and cancel form closing if necessary
e.Cancel = MessageBox.Show("Are You Sure You Want To Close This Form?",
"Close Application",
MessageBoxButtons.YesNo) != DialogResult.Yes;
}
}
Edit: Usually we ask direct questions to user (it's unclear what wrong things can arise if I just "Close This Form"), e.g.
private void frminventory_FormClosing(object sender, FormClosingEventArgs e) {
// Data has not been changed, just close without pesky questions
if (!isEdited)
return;
// If it's a user who is closing the form...
if (e.CloseReason == CloseReason.UserClosing) {
var action = MessageBox.Show(
"You've edited the data, do you want to save it?"
Text, // Let user know which form asks her/him
MessageBoxButtons.YesNoCancel);
if (DialogResult.Yes == action)
Save(); // "Yes" - save the data edited and close the form
else if (DialogResult.No == action)
; // "No" - discard the edit and close the form
else
e.Cancel = true; // "Cancel" - do not close the form (keep on editing)
}
}
So, because i can't comment, i would do something like this.
//Create this variable
private bool _saved = true;
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
DoSomething();
_saved = true;
}
//Raised when the text is changed. I just put this for demonstration purpose.
private void textBox1_TextChanged(object sender, EventArgs e)
{
_saved = false;
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_saved == false)
{
var result = MessageBox.Show("Are you sure you want to close?\nYou may have unsaved information", "Information", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
_saved = true;
Application.Exit();
}
else
e.Cancel = true;
}
}
Hope this can solve yor problem

Form Closing dialog result appears in all forms

I have this Code in the Form1 closing event:
private void MemberForm_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult del = MessageBox.Show("Save changes?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (del == DialogResult.Yes)
{
// do something
}
if (del == DialogResult.No)
{
// do something
}
if (del == DialogResult.Cancel)
{
e.Cancel = true;
}
...
}
If I open form2 from button in my form1, in form2 closing event it will show me dialog result again. I want dialog result to be showed only in form1.
Why does this happen?
So my new question is: Can i do the same with save button? Preventing write the code second time?
Example
base.OnClosing(e);
if (e.CloseReason == CloseReason.UserClosing)
{
DialogResult del = MessageBox.Show("Save change?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (del == DialogResult.Yes)
{
??????????????
}
if (del == DialogResult.No)
{
Form3 ss = new Form2();
Hide();
ss.ShowDialog();
}
if (del == DialogResult.Cancel)
{
e.Cancel = true;
}
public bool saveToolStripMenuItemClicked { get; set; }
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 ss = new Form3();
Hide();
ss.ShowDialog();
}
Now i want when dialogresult (Yes) do the same with saveToolStripMenuItem_Click
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (e.CloseReason == CloseReason.UserClosing)
{
//Some Code
}
}
Try this, this should only run when you close the whole application, make sure you put it on the main form.
The reason why yours is running on Form2 closing is because you have a memberclosing which from my understanding runs on all forms closing
UPDATE
I have edited the code above, if you put the code in that if statement then it will only run then the user clicks the close button
UPDATE 2
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if(BackButtonClicked)
{
//code used for DialogResult.No answer
BackButtonClicked = false;
}
if (e.CloseReason == CloseReason.UserClosing)
{
//Some Code
}
}
public bool BackButtonClicked { get; set; }
private void backButton_Click(object sender, EventArgs e)
{
//Some code
BackButtonClicked = true;
//Some code, close form
}

Deleteing rows from DataGridView with custom DialogBox

What i have done is a user rightclicks on a row. A menu appears, they choose the delete option. But there is no confirmation dialog. How can I use a custom form that i have created. that has delete,cancel buttons.
My Code that works;
private void Delete_Click(object sender, EventArgs e)
{
if (this.dgvTable.SelectedRows.Count > 0)
{
dgvTable.Rows.RemoveAt(this.dgvTable.SelectedRows[0].Index);
}
//Call FrmDelete??
}
How can I use the new form, to confirm the delete. I have tried using a MessageBox
DialogResult dialogResult = MessageBox.Show("ARE YOU SURE?", "DELETE Title", MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
if (this.dgvTable.SelectedRows.Count > 0)
{
dgvTable.Rows.RemoveAt(this.dgvTable.SelectedRows[0].Index);
}
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
I know thats probably the easiest option. but I would like to use the new Delete Form.
Thanks
You have to use the DialogResult property of your new form.
In your new form:
void ButtonDelete_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
void ButtonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
Then in the main form:
private void Delete_Click(object sender, EventArgs e)
{
YourDialog dlg = new YourDialog();
DialogResult dialogResult = dlg.ShowDialog();
if(dialogResult == DialogResult.OK)
{
if (this.dgvTable.SelectedRows.Count > 0)
{
dgvTable.Rows.RemoveAt(this.dgvTable.SelectedRows[0].Index);
}
}
else if (dialogResult == DialogResult.Cancel)
{
return;
}
}
You just have to change a few lines.
The first to show the form:
DialogResult dialogResult = new DeleteForm().ShowDialog();
And then check the result is "OK" rather than "Yes":
if(dialogResult == DialogResult.OK)
Finally, DialogResult.No will never be returned, so your else if should just be else.

Forms opens two times

In the code below, I open a form with frmContact.ShowDialog(); and then when I close the form by clicking on the OK button in the form it closes, but then it opens again because I have the frmContact.ShowDialog() in the if statement. Could this be done in some oterh way?
// Button add new customer
private void btnAdd_Click(object sender, EventArgs e)
{
ContactForm frmContact = new ContactForm();
frmContact.ShowDialog(); // Show the contact form window
if (frmContact.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("OK", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
Simply remove the first call:
ContactForm frmContact = new ContactForm();
if (frmContact.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("OK", "Test", ...);
}
Another option (especially useful if the code that shows the form is not next to the code that checks the return value) is to use Form.DialogResult:
ContactForm frmContact = new ContactForm();
frmContact.ShowDialog();
if (frmContact.DialogResult == DialogResult.OK)
{
MessageBox.Show("OK", "Test", ...);
}
Just get rid of the first ShowDialog.
Just leave the second if, like this:
private void btnAdd_Click(object sender, EventArgs e)
{
ContactForm frmContact = new ContactForm();
if (frmContact.ShowDialog() == DialogResult.OK) //just one call
{
MessageBox.Show("OK", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
ContactForm frmContact = new ContactForm();
frmContact.ShowDialog();
}

Validation in modal form

I have a simple modal form in which I have to check user entered data. But after validation the form gets closed. It behaves like this because of not empty DialogResult property but I need this value for other purposes (in a parent form)
Any ideas?
A little code to clear things up
//This method creates and calls a modal form.
public static Definition edit(Definition w)
{
EditForm ed = new EditForm();
DialogResult dr = ed.ShowDialog();
if (dr == DialogResult.OK)
{
//update some fields of passed object
}
//other code
}
private void btnSave_Click(object sender, EventArgs e)
{
if (validateForm())
{
DialogResult = DialogResult.Yes;
Close();
}
}
I would do it this way:
private void btnSave_Click(object sender, EventArgs e)
{
if (validateForm())
{
DialogResult = DialogResult.Yes;
Close();
}
else
{
DialogResult = DialogResult.None;
}
}
I.e. as you said, clear the DialogResult.
Add a FormClosing event handler and then if the validation fails set e.Cancel = true:
private void EditForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.DialogResult == DialogResult.OK)
{
e.Cancel = !ValidateInput();
}
}
This will leave you sub form open and let the user correct the mistakes. You can check whether the "OK" or "Cancel"/Window Close button has been clicked by checking the DialogResult and only performing the validation if it's OK.

Categories