c#窗体关闭消息框上的按钮单击

本文关键字:按钮 单击 消息 窗体 | 更新日期: 2023-09-27 18:18:48

是否有一种方法可以让消息框关闭并返回到表单,而不需要通过电子邮件发送数据?

已经完成一半了,我只需要第二个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 ");
        }

c#窗体关闭消息框上的按钮单击

您实际上忘记了为MessageBox.Show添加逗号以获得正确的参数/参数。

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

只有当用户单击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);