2013-06-18 36 views
0

有沒有辦法讓消息框關閉並返回到表單,而不通過電子郵件發送數據?C#窗體關閉按鈕單擊消息框

半途atm只需要第二個if/else if語句的幫助。我對這一切都很陌生。

我的代碼如下:)

//Button 
    private void submitButton_Click(object sender, EventArgs e) 
    { 
     string software = null; 
     string hardware = null; 



     foreach (string s in softwareNeededCheckedListBox.CheckedItems) 
     { 
      software = software + '\n' + s; 
     } 

     foreach (string s in hardwareNeededCheckedListBox.CheckedItems) 
     { 
      hardware = hardware + '\n' + s; 
     } 

     if (yesRadioButton.Checked == true) 
     { 
      yesRadioButton.Text = "Yes"; 
      noRadioButton.Text = " "; 
     } 


     if (noRadioButton.Checked == true) 
     { 
      noRadioButton.Text = "No"; 
      yesRadioButton.Text = " "; 
     } 



     if (MessageBox.Show("First name: " + firstNameTextBox.Text + " " + "Last name: " + lastNameTextBox.Text + "\n" + "\n" + 
       "Will be working in " + areaOfTheCompanyComboBox.SelectedItem + "\n" + "\n" + 
       "They will need the following software:" + software + "\n" + "\n" + 
       "They will need the following hardware:" + hardware + "\n" + "\n" + 
       "They will be seated: " + seatingLocationRichTextBox.Text + "\n" + "\n" + 
       "Any further Comments: " + notesRichTextBox.Text + "\n" + "\n" + 
       "Do they require a phone: " + noRadioButton.Text + yesRadioButton.Text + "\n" + "\n" + 
       "Date they start: " + completionDateTimePicker.Text + "\n" + "\n" + 
       "Are you happy with the information shown above?" + "\n" + "\n" + 
       MessageBoxButtons.OKCancel) == DialogResult.OK) 
     { 
      MailMessage mail = new MailMessage(); 
      SmtpClient smtpserver = new SmtpClient("smtp.gmail.com"); 

      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("[email protected]"); 
      mail.Subject = "Test Mail"; 
      mail.Body = "First name: " + firstNameTextBox.Text + " " + "Last name: " + lastNameTextBox.Text + "\n" + "\n" + 
       "Will be working in " + areaOfTheCompanyComboBox.SelectedItem + "\n" + "\n" + 
       "They will need the following software:" + software + "\n" + "\n" + 
       "They will need the following hardware:" + hardware + "\n" + "\n" + 
       "They will be seated: " + seatingLocationRichTextBox.Text + "\n" + "\n" + 
       "Any further Comments: " + notesRichTextBox.Text + "\n" + "\n" + 
       "Do they require a phone: " + noRadioButton.Text + yesRadioButton.Text + "\n" + "\n" + 
       "Date they start: " + completionDateTimePicker.Text; 
      smtpserver.Port = 587; 
      smtpserver.Credentials = new System.Net.NetworkCredential("**", "********"); 
      smtpserver.Send(mail); 
      MessageBox.Show("Your Form has been sent "); 

     } 
+0

你在做什麼應該沒問題......就是說:如果你按下取消,那麼電子郵件就不會被髮送。你的'if'對我來說很合適。即使您點擊取消,它是否發送? – Jcl

+0

沒有取消按鈕似乎並沒有出現這是目前的問題,因爲你已經證實,如果shuld工作:( –

+0

@DavidGolding看看我的答案:) – aiapatag

回答

4

事實上,你忘記你把逗號你的MessageBox.Show有正確的參數/參數。

 if (MessageBox.Show("Body of your message box", 
          "Title Of Your MessageBox", 
          MessageBoxButtons.OKCancel, 
          MessageBoxIcon.Information) == DialogResult.OK) 
     { 

     } 
0

只會如果用戶點擊OK電子郵件。你可以通過使用DialogResult.Cancel

來處理取消按鈕我會親自把MessageBox放在一個dialogResult變量中。例如:

var dialogResult = MessageBox.Show("FIrst name .... "); 

然後你可以檢查用戶是否按下OKCancel

if (dialogResult == DialogResult.OK) { 
    // Send the e-mail 
} else if (dialogResult == DialogResult.Cancel) { 
    // Do what you need to do/return to the form. 
} 

要顯示一個消息框與OK and Cancel你需要:

MessageBox.Show(this, "Message", "Title", MessageBoxButtons.OKCancel); 
+0

試過,也芽VS2012只是女士似乎並不想顯示取消按鈕:( –

+0

@DavidGolding - 我編輯了我的答案,向您展示MessageBox的正確語法,並確定並取消 –

相關問題