【问题标题】:Display text in textbox after call a function调用函数后在文本框中显示文本
【发布时间】:2018-09-19 09:44:56
【问题描述】:

我有一个基于 datagridview 数据发送电子邮件的程序。发送电子邮件时效果很好,但我希望在每次函数调用后更改文本框的内容。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;
using System.Net.Mail;
using System.Net;


namespace sendEmail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            string connectionString = "connectionString";
            string sql = "sqlQuery";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
            DataSet ds = new DataSet();
            connection.Open();
            dataadapter.Fill(ds, "data");
            connection.Close();
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "data";
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                textBox1.Text += "Processing .... " + row.Cells["data"].Value.ToString();
                sendMail("subject", row.Cells["data"].Value.ToString(), "emailString");
                 )
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;
        }

        private void sendMail (string subject, string content, string email)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("myEmail@gmail.com");
                mail.To.Add("toEmail@gmail.com");
                mail.Subject = "Mail for " + content;
                mail.Body = content;


                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("myEmail@gmail.com", "myPassword");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                textBox1.Text += " -> Success\n";
            }
            catch
            {
                textBox1.Text += " -> Fail\n";
            }
        }


    }
}

在运行程序时,文本框的内容只有在函数一直调用时才会改变(datagrid循环中的所有行)。

那么,我怎样才能让文本框在每次调用函数时更改内容。

感谢阅读并请帮助!

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    使用background worker。在后台线程中发送邮件(DoWork 方法)并在 UI 线程中显示状态(ProgressChanged 方法)。

    【讨论】:

      【解决方案2】:

      如果您不想使用 BackGroundWorker,可以强制文本框自行更新。
      试试这样的

      foreach (DataGridViewRow row in dataGridView1.Rows)
      {
          textBox1.Text += "Processing .... " + row.Cells["data"].Value.ToString();
          textBox1.Update();
          sendMail("subject", row.Cells["data"].Value.ToString(), "emailString");
          textBox1.Update();
      }
      

      【讨论】:

      • 是的,使用 Application.DoEvents()。
      • @AccessDenied 永远不要使用Application.DoEvents() 这不是我的建议。如果textBox1.Update() 不这样做,你仍然可以这样做this.Update() 但是,Backgroundworker 是一个更好的解决方案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2016-03-24
      • 2011-09-23
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多