Question

Is there a way to get the message box to close and return to the form without it emailing the data?

halfway there atm just need a help with the second if/else if statement. I'm very new to all this.

my code is below :)

//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 ");

        }
Was it helpful?

Solution

You actually forgot you place commas to your MessageBox.Show to have the correct arguments/parameters.

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

        }

OTHER TIPS

That will only e-mail if the user clicks OK. You can handle the cancel button by using DialogResult.Cancel

I would personally put the MessageBox in a dialogResult variable. For instance:

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

Then you can check if the user pressed OK or Cancel

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

To show a messagebox with OK and Cancel you need:

MessageBox.Show(this, "Message", "Title", MessageBoxButtons.OKCancel);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top