C#表单按钮单击关闭消息框

时间:2013-06-18 11:32:06

标签: c# email cancel-button

有没有办法让消息框关闭并返回到表单而不通过电子邮件发送数据?

在那里,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("xxxxx@gmail.com");
            mail.To.Add("xxxxx@xxxxxx.co.uk");
            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 ");

        }

2 个答案:

答案 0 :(得分:4)

您实际上忘记了将逗号放在MessageBox.Show上以获得正确的参数/参数。

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

        }

答案 1 :(得分: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);