How to avoid an exit confirmation? - c#

what I want to do is simple but I do not know how to do it. I'm doing a basic window login, and I wrote a code to ask for confirmation before exit, like this: (I have the names in spanish, the "Contador" is the counter if you do not understand)
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialogo = MessageBox.Show("¿Desea cerrar la aplicación?",
"Aviso!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (dialogo == DialogResult.No)
{
e.Cancel = true;
}
}
What I've done after that, is a counter that if I entry the incorrect information three times, the application is going to close, here's the code:
private int Contador;
private void Form1_Load(object sender, EventArgs e)
{
Contador = 0;
aceptar.Enabled = false;
usuario.MaxLength = 40;
contraseña.MaxLength = 10;
}
private void aceptar_MouseClick(object sender, MouseEventArgs e)
{
if(Contador == 2)
{
DialogoCerrar();
Close();
}
if (usuario.Text == ("Demo") && (contraseña.Text == ("ABC123")))
{
Contador = 0;
DialogResult dialogo = MessageBox.Show(
"Ingreso exitoso!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
DialogResult dialogo = MessageBox.Show(
"Datos incorrectos", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
Contador++;
}
}
So, this works, but when after three times I put the incorrect information, before to close the program ask me if I want to do it (I know that is for the Form1_FormClosing), and I want that the program doesn't ask it in that situation.

You just need to set a flag:
private bool _noConfirmExit;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_noConfirmExit)
{
return;
}
DialogResult dialogo = MessageBox.Show("¿Desea cerrar la aplicación?", "Aviso!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (dialogo == DialogResult.No)
{
e.Cancel = true;
}
}
private void aceptar_MouseClick(object sender, MouseEventArgs e)
{
if(Contador == 2)
{
_noConfirmExit = true;
DialogoCerrar();
Close();
}
if (usuario.Text == ("Demo") && (contraseña.Text == ("ABC123")))
{
Contador = 0;
DialogResult dialogo = MessageBox.Show("Ingreso exitoso!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
DialogResult dialogo = MessageBox.Show("Datos incorrectos", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
Contador++;
}
}
That way, your FormClosing event handler can tell the difference between closing for other reasons and closing because the counter reached its limit.

Here's code that tracks whether you should show the warning dialog box. It's basically a flag that you set when you don't want to show the dialog box.
public partial class Form1 : Form
{
private bool SkipWarning = false;
public Form1()
{
InitializeComponent();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(!SkipWarning)
{
DialogResult dialogo = MessageBox.Show("¿Desea cerrar la aplicación?", "Aviso!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (dialogo == DialogResult.No)
{
e.Cancel = true;
}
}
}
private void aceptar_MouseClick(object sender, MouseEventArgs e)
{
if(Contador == 2)
{
SkipWarning = true;
DialogoCerrar();
Close();
}
if (usuario.Text == ("Demo") && (contraseña.Text == ("ABC123")))
{
Contador = 0;
DialogResult dialogo = MessageBox.Show("Ingreso exitoso!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
DialogResult dialogo = MessageBox.Show("Datos incorrectos", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
Contador++;
}
}
}

Related

label.Text = ComboBox.SelectedText(); always null?

I am making an application that has some options in drop-down menus that get populated from the App.Config file. I was testing a reset function when the program stopped doing the reset. My code for Form1 is below:
public Form1()
{
InitializeComponent();
InitializeDropDownMenu();
}
private void InitializeDropDownMenu()
{
//Populate all the menus from app.config
foreach (string s in Properties.Settings.Default.Box1Contents)
{
comboBox1.Items.Add(s);
}
foreach (string s in Properties.Settings.Default.Box2Contents)
{
comboBox2.Items.Add(s);
}
foreach (string s in Properties.Settings.Default.Box3Contents)
{
comboBox3.Items.Add(s);
}
//Controls for drop down menus
this.Controls.Add(comboBox1);
comboBox1.SelectedIndexChanged +=
new System.EventHandler(comboBox1_SelectedIndexChanged);
this.Controls.Add(comboBox2);
comboBox2.SelectedIndexChanged +=
new System.EventHandler(comboBox2_SelectedIndexChanged);
this.Controls.Add(comboBox3);
comboBox3.SelectedIndexChanged +=
new System.EventHandler(comboBox3_SelectedIndexChanged);
//Begin Program with all dDMenus enabled.
comboBox1.Enabled = true;
comboBox2.Enabled = true;
comboBox3.Enabled = true;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DialogResult result = MessageBox.Show(
"Change Viewer to: \r\n" + comboBox1.Text + "\r\n\r\n" + "Confirm?",
"Menu",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
label3.Text = comboBox1.SelectedText;
}
else if( result == DialogResult.No)
{
comboBox1.ResetText();
}
}
private void comboBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
DialogResult result = MessageBox.Show(
"Change Viewer to: \r\n" + comboBox2.Text + "\r\n\r\n" + "Confirm?",
"Menu",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
label3.Text = comboBox2.SelectedText;
}
else if (result == DialogResult.No)
{
comboBox2.ResetText();
}
}
private void comboBox3_SelectedIndexChanged(object sender, System.EventArgs e)
{
DialogResult result = MessageBox.Show(
"Change Viewer to: \r\n" + comboBox3.Text + "\r\n\r\n" + "Confirm?",
"Menu",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
label3.Text = comboBox3.SelectedText;
}
else if (result == DialogResult.No)
{
comboBox3.ResetText();
}
}
private void ResetApp()
{
comboBox1.ResetText();
comboBox2.ResetText();
comboBox3.ResetText();
}
private void button1_Click(object sender, EventArgs e)
{
ResetApp();
label3.Text = "ResetApp Ran";
}
Any ideas as to why label3 is always set to null, and why when reset is clicked the ComboBoxes aren't being reset to blanks anymore?
Thank you for your help,
-Arthur
EDIT* I will use Items.Clear(); and then just call InitializeDropDownMenu() in the reset function. Should work for my intended use. Thank you all.
I think the problem is in use of SelectedText. The SelectedTextproperty "Gets or sets the text that is selected in the editable portion of a System.Windows.Forms.ComboBox".
Instead try to use the SelectedItem property.
label1.Text = comboBox1.SelectedItem.ToString();

Back button affected by FormClosing event

I want to go back to the form1 but everytime I click the back button, it triggers the FormClosing event so it prompts me if I want to quit the application. How can I solve this problem in order to exclude the other buttons from the FormClosing event?
private void Form2_FormClosing_1(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to quit?", "Exit",
MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
private void back_button_Click(object sender, EventArgs e)
{
this.Hide();
}
Best thing to do would be to set global variable that will tell closing event to show/not show that dialog.
Try something along these lines.
private bool CalledFromButton = false;
private void Form2_FormClosing_1(object sender, FormClosingEventArgs e)
{
if(CalledFromButton)
return;
DialogResult dialog = MessageBox.Show("Do you really want to quit?", "Exit",
MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
private void back_button_Click(object sender, EventArgs e)
{
CalledFromButton = true;
this.Hide();
}

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.

How to prevent SelectedIndexChanged event from firing in ListBox after removing an item from it

I am working on selecting an item which will cause deletion of selected item. The problem is that when I delete an item I am selecting additional item which cause another deletion...
How do I unselest/deselect after deleting selected item?
This causes my problem:
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?", "Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
lbMessage.Items.Remove(lbMessage.SelectedItem);
lbMessage.SelectedIndex = -1;
}
else
{
}
}
private void btnAddMessage_Click(object sender, EventArgs e)
{
lbMessage.Items.Add(txtMessage.Text);
txtMessage.Text = string.Empty;
}
Try removing the SelectedIndexChanged event before removing the item, then add it back in:
private void lbMessage_SelectedIndexChanged(object sender, EventArgs e) {
if (MessageBox.Show("Are you sure you want to remove this item?",
"Remove Confirmation",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes) {
lbMessage.SelectedIndexChanged -= lbMessage_SelectedIndexChanged;
lbMessage.Items.Remove(lbMessage.SelectedItem);
lbMessage.SelectedIndexChanged += lbMessage_SelectedIndexChanged;
}
}
Setting SelectedIndex = -1 is raising the SelectedIndexChanged event. Check if there is nothing selected at the start of the event.
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbMessage.SelectedIndex == -1) return;
...
}
If you like it that way without a delete button then just do this:
bool isAfterDelete = false;
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (isAfterDelete)
{
isAfterDelete = false;
return;
}
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?",
"Removal Confirmation",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
lbMessage.Items.Remove(lbMessage.SelectedItem);
isAfterDelete = true;
lbMessage.SelectedIndex = -1;
}
else
{
}
}
What if you just either wrap your code an in if() that checks for lbMessage.SelectedIndex == -1, or write a check for it at the beginning and return if it's true:
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if(lbMessage.SelectedIndex == -1)
return;
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?", "Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
lbMessage.Items.Remove(lbMessage.SelectedItem);
lbMessage.SelectedIndex = -1;
}
}
The shear act of removing the item from the Items collection causes the items to change their index. This thus calls the "SelectedIndexChanged" event again on the list box. To prevent this, you would need to set a flag before the remove function to indicate that the SelectedIndexChange is being called after the
remove and prevent the event from occuring. You can do that by the following.
private bool afterRemove = false;
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (afterRemove)
{
afterRemove = false;
return;
}
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?", "Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
afterRemove = true;
lbMessage.Items.Remove(lbMessage.SelectedItem);
// Another call to "lbMessage_SelectedIndexChanged" is made right here.
}
}
private void btnAddMessage_Click(object sender, EventArgs e)
{
lbMessage.Items.Add(txtMessage.Text);
txtMessage.Text = string.Empty;
}
An alternative that might be easier to understand could be the following.
private bool afterRemove = false;
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (afterRemove)
return;
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?", "Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
afterRemove = true;
lbMessage.Items.Remove(lbMessage.SelectedItem);
// Another call to "lbMessage_SelectedIndexChanged" is made right here.
afterRemove = false;
}
}

Categories