邮件发送时,禁用datagridview中的发送按钮

时间:2014-05-27 18:00:26

标签: c# email datagridview

我正在开展一个项目,我需要你的帮助。我将datagridview的内容发送到我的电子邮件后,我想禁用我的发送按钮。这是发送邮件的代码,我想要在邮件发送后该按钮被禁用。

private void dataGridNarocila_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   try
   {         
      string narocilo = "Narocilo.txt";
      using (StreamWriter pisi = File.CreateText(narocilo))
      {
         for (int i = 0; i > dataGridNarocila.Rows.Count; i++)
         {
            pisi.Write(dataGridNarocila.Rows[i].Cells[0].Value.ToString() + "|" +
            dataGridNarocila.Rows[i].Cells[1].Value.ToString() + "|" +
            dataGridNarocila.Rows[i].Cells[2].Value.ToString() + "|" +
            dataGridNarocila.Rows[i].Cells[3].Value.ToString() + "|" +
            dataGridNarocila.Rows[i].Cells[4].Value.ToString() + "|" +
            dataGridNarocila.Rows[i].Cells[5].Value.ToString());
            pisi.WriteLine(); 
         }
      }
   }

   catch { MessageBox.Show("Napaka pri naročanju"); }

   string mailBody = "\n";

   foreach (DataGridViewRow row in dataGridNarocila.Rows)
   {
      foreach (DataGridViewCell cell in row.Cells)
      {
         mailBody += "" + cell.Value + " \n";
      }
   }
   var client = new SmtpClient("smtp.gmail.com", 587);
   client.EnableSsl = true;
   client.Credentials = new NetworkCredential("urbanchy@gmail.com", "****");

   var mail = new MailMessage();
   mail.From = new MailAddress("youraccount@yahoo.com");
   mail.To.Add("urbanchy@gmail.com");
   mail.Subject = "This is the subject of the mail";
   mail.Body = mailBody;
   client.Send(mail);
   MessageBox.Show("Naročilo oddano");
   }
}

3 个答案:

答案 0 :(得分:2)

将按钮的Enabled属性设置为false,如下所示:

btnNameHere.Enabled = false;

或者,具体使用您的代码:

var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("urbancy@gmail.com", "***********");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount@yahoo.com");
mail.To.Add("urbancy@gmail.com");
mail.Subject = "This is the subject of the mail";
mail.Body = mailBody;
client.Send(mail);
MessageBox.Show("Order taken");
btnNameHere.Enabled = false;  // Add this line of code.

请确保您在某些时候重新启用它 - 以防您再次需要它,正如@alykins在下面的评论中指出的那样。要重新启用它,只需执行与上面显示的相反的操作:

btnNameHere.Enabled = true;

快乐的编码!

答案 1 :(得分:2)

如果您使用的是WPF,请将IsEnabled属性设置为false

btn.IsEnabled = false;

对于Windows应用程序

btn.Enabled = false;

答案 2 :(得分:0)

使用

button1.Click - = button1_Click;

禁用button1的点击事件

并使用

button1.Click + = button1_Click;

再次启用它。

相关问题