2013-06-18 3 views
0

메시지 상자를 닫고 데이터를 전자 메일로 보내지 않고 양식으로 돌아갈 수있는 방법이 있습니까?C# form 단추를 클릭하여 메시지 상자를 닫습니다.

중간에 두 번째 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

아무런 취소 버튼이 나타나지 않는 것 같습니다. 잠시 후 문제가있는 것으로 확인되었습니다. ( –

+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 변수에 넣습니다. 예를 들어 : 사용자가 OK and Cancel와 메시지 박스를 표시하려면 OK 또는

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

Cancel을 누를 경우

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

이 그럼 당신은 확인하실 수 있습니다 당신이 필요합니다

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

시도 할 때 찬성 투표 할 것이다 실현하지 않았다 또한 단지 미스 VS2012 꽃 봉오리 취소 버튼을 표시하고 싶지 않습니다. ( –

+0

@DavidGolding - 확인 및 취소로 MessageBox의 올바른 구문을 보여주기 위해 내 대답을 편집했습니다. –

관련 문제