c# - Windows Forms - How to break execution of currently executing code -
c# - Windows Forms - How to break execution of currently executing code -
i have abstract windows form called addeditreminderform containing, amongst others, event handler , method:
buttonsave_click performvalidationshere relevant code addeditreminderform:
protected virtual void buttonsave_click(object sender, eventargs e) { title = textboxtitle.text; description = textboxdescription.text; place = textboxplace.text; date = datepicker.value.date; time = timepicker.value.timeofday; performvalidations(); } protected void performvalidations() { if (string.isnullorempty(title)) { messagebox.show("error: title field left empty!"); return; } if (string.isnullorempty(description)) { messagebox.show("error: description field left empty!"); return; } if (string.isnullorempty(place)) { messagebox.show("error: place field left empty!"); return; } if (date < currentdate) { messagebox.show("error: date must in future!"); return; } if (date == currentdate && time < currenttime) { messagebox.show("error: time must in future!"); return; } }
i have form called addreminderform inherits addeditreminderform. overriding buttonsave_click event handler such saves reminder in add-on normal operations performs in base of operations class. here code addreminderform:
using system; using system.windows.forms; using reminder.classes; namespace reminder { public partial class addreminderform : addeditreminderform { public addreminderform() { initializecomponent(); this.text = "add reminder form"; label labelformheading = this.controls["labelformheading"] label; labelformheading.text = "add reminder"; } protected override void buttonsave_click(object sender, eventargs e) { base.buttonsave_click(sender, e); addreminderoperation(); } protected void addreminderoperation() { reminderclass reminder = new reminderclass(); reminder.id = reminderhelper.getcounter(); reminder.title = title; reminder.description = description; reminder.place = place; reminder.date = date; reminder.time = time; reminderhelper.addreminder(reminder); messagebox.show("the reminder has been saved!"); this.close(); } } }
now, problem have that, when addreminderform open, in case 1 of validations in performvalidations method fails, message box shown execution not stopped , reminder still saved. how can break execution if 1 of validation fails please? using return, if remember correctly, homecoming stops execution of current method. thanks.
i create method in base of operations class, validateandsave
, called form event handler:
protected void validateandsave() { if (this.performvalidations()) { this.save(); } } private void buttonsave_click(object sender, eventargs e) { this.validateandsave(); } protected bool performvalidations() /* virtual, if want additional checks in derived classes */ { ... } protected virtual void save() { ... }
you allow performvalidations
homecoming true
if validations okay. phone call save, can override in deriving classes.
c# winforms return execution
Comments
Post a Comment