c# - Form still closing when I choose no -
i have following code
private void form1_formclosing(object sender, formclosingeventargs e) { if (cmd.cetaktanya("are sure want exit ?")) { cmd.cetaksukses("thank using " + cmd.title); starturl(); } } public bool cetaktanya(string message) { bool status = false; dialogresult dialogresult = messagebox.show(message, title, messageboxbuttons.yesno, messageboxicon.exclamation); if (dialogresult == dialogresult.yes) { status = true; } else if (dialogresult == dialogresult.no) { status = false; } return status; }
why form still closed though choose "no" @ confirmation dialog?
use cancel
property cancel event if "no" clicked.
private void form1_formclosing(object sender, formclosingeventargs e) { if (cmd.cetaktanya("are sure want exit ?")) { cmd.cetaksukses("thank using " + cmd.title); starturl(); } else e.cancel = true; }
additionally, method refactored bit more compact:
public bool cetaktanya(string message) { var result = messagebox.show(message, title, messageboxbuttons.yesno, messageboxicon.exclamation); return result == dialogresult.yes; }