How to make a user survey when closing a form, only if he changed something on it?
2 answers
To a form class or somewhere in an accessible place:
public bool UpdateInForm = false; In handlers:
private void textBox1_TextChanged(object sender, EventArgs e) { UpdateInForm = true; } And on the form closing handler:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (UpdateInForm) { DialogResult result = MessageBox.Show("Сохранить изменения?", "Внимание", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == System.Windows.Forms.DialogResult.Yes) { // Сохраняем } else if (result == System.Windows.Forms.DialogResult.No) { //Не сохраняем } else e.Cancel = true;//Отменяем действие } } - And how does it check for changes? - zerpico
- We add change events to textboxes and other controls: private void textBox1_TextChanged (object sender, EventArgs e) {UpdateInForm = true; } or private void checkBox1_CheckedChanged (object sender, EventArgs e) {UpdateInForm = true; } - GenkaOk
- This is of course all wonderful, but what if I have a lot of controls on the form - s017.radikal.ru/i429/1308/27/23df36367ab0.png I will get tired of broadcasting an event for each. Is there a more radical method? - zerpico
- 3Something like this .. private void Form1_Load (object sender, EventArgs e) {foreach (Control a in this.Controls) {a.TextChanged + = a_TextChanged; ((CheckBox) a) .CheckedChanged + = Form1_CheckedChanged; }} void Form1_CheckedChanged (object sender, EventArgs e) {UpdateInForm = true; } void a_TextChanged (object sender, EventArgs e) {UpdateInForm = true; } - Wooden solution, you need to modify. - GenkaOk
|
Form form = new Form(); if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) { //действия } else { //действия } - Yes, not so. How do I know if something has changed on the form or not. And only if something was introduced or changed only then to interrogate - zerpico
|