2014-02-08 43 views
0

我有2個表單 - 當在Form1上按下按鈕時,它會觸發表單2打開。用戶必須輸入一些信息,然後按確定。直到完成後才返回主表單

如果信息沒有填寫我會拋出一個錯誤,但結果是alwayys返回到主窗體 - 我不希望這發生,直到所有信息完成。我怎樣才能做到這一點 ?

也許我應該做的是傳遞一個布爾成功和處理它的方式?

Form1中

FormSaveChanges FormSaveChanges = new FormSaveChanges(); 
var result = FormSaveChanges.ShowDialog(); 

if (result == DialogResult.OK) 
{ 
    // The code comes back here even if not all information was filled out 
} 

表2

private void radButtonSaveChanges_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     if (radTextBoxReferenceNumber.Text == "") 
     { 
      RadMessageBox.Show(this, " You must enter a reference number", "Error", MessageBoxButtons.OK, RadMessageIcon.Error); 
      return; // Quit 
     } 
     else 
     { 
      // Save items and return to the main form 
     } 
    } 
} 

回答

1

只要改變第二形式的屬性DialogResult到DialogResult.None

private void radButtonSaveChanges_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     if (radTextBoxReferenceNumber.Text == "") 
     { 
      RadMessageBox.Show(this, " You must enter a reference number", ....); 

      // Stop the WinForms manager to close this form 
      this.DialogResult = DialogResult.None; 
      return; 
     } 
     else 
     { 
      // all ok.... let's return the DialogResult property of the button 
      // Do nothing, the WinForms manager gets the DialogResult of this button and 
      // assign it to the form closing it.... 
     } 
    } 
] 

這樣第e Form2未關閉,用戶無需重新輸入所有內容即可修復錯誤

表單的DialogResult屬性通常設置爲DialogResult.None並更改爲按鈕上存在的相同屬性的值。如果按鈕具有DialogResult = DialogResult.OK,則代碼從ShowDialog退出,並從單擊的按鈕返回DialogResult的值。 設置形式爲無防止窗體關閉時,你需要修復輸入錯誤

+0

優秀的感謝 – user1438082

3

在窗體2,當一切都OK,添加此行的代碼:

this.DialogResult = DialogResult.OK; 
相關問題