2014-10-21 2 views
1

내가 추상 창 양식 AddEditReminderForm가, 다른 사람의 사이에, 이벤트 핸들러 및 방법을 포함하는 호출이 실행의 실행을 중단하는 방법 :윈도우 폼 - 현재 코드

  1. buttonSave_Click
  2. PerformValidations을

다음은 관련 코드입니다. 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: The title field was left empty!"); 
     return; 
    } 

    if (String.IsNullOrEmpty(description)) 
    { 
     MessageBox.Show("Error: The description field was left empty!"); 
     return; 
    } 

    if (String.IsNullOrEmpty(place)) 
    { 
     MessageBox.Show("Error: The place field was left empty!"); 
     return; 
    } 

    if (date < currentDate) 
    { 
     MessageBox.Show("Error: The date must be in the future!"); 
     return; 
    } 

    if (date == currentDate && time < currentTime) 
    { 
     MessageBox.Show("Error: The time must be in the future!"); 
     return; 
    } 
} 

나는 다음 AddEditReminderForm에서 상속AddReminderForm라는 형태를 갖는다. buttonSave_Click 이벤트 처리기를 오버라이드하여 기본 클래스에서 수행하는 정상적인 작업 외에도 미리 알림을 저장합니다. 여기에 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"] as 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 successfully saved!"); 
      this.Close(); 
     } 
    } 
} 

, I가 가지고있는 문제는, 그 AddReminderForm는 경우, 열려있을 때 방법이 실패 PerformValidations의 검증 중 하나 인 메시지 상자가 표시되지만 실행이 중지되지 않고 미리 알림이 저장됩니다. 유효성 검사 중 하나라도 실패하면 실행을 중단 할 수 있습니까? 나는 return을 사용하고 있지만 정확하게 기억한다면 return은 현재 메소드의 실행을 멈춘다. 감사.

답변

1

나는 이벤트 핸들러 형태라고하여 기본 클래스, ValidateAndSave에 방법을 만들 것입니다 : 모든 검증이 좋아하는 경우

protected void ValidateAndSave() 
{ 
    if (this.PerformValidations()) 
    { 
     this.Save(); 
    } 
} 

private void buttonSave_Click(object sender, EventArgs e) 
{ 
    this.ValidateAndSave(); 
} 

protected bool PerformValidations() 
    /* could be virtual, if you want to do additional checks in derived classes */ 
{ ... } 

protected virtual void Save() 
{ ... } 

당신은 PerformValidations 반환 true을 할 수 있습니다. 그런 다음 save 클래스를 호출하면 파생 클래스에서 재정의 할 수 있습니다.

+1

설명해 주신 데 대해 감사드립니다. 그것은 많은 의미가 있습니다. 감사. –

관련 문제