您可以在非表格类的文本字段中显示实时数据吗?

时间:2017-04-19 09:21:48

标签: c# winforms

我是C#的新手,我正在申请发送邮件。我想要做的是在文本框字段中显示我实时发送的电子邮件。

操作:当我运行应用程序时,它与数据库通信并获取记录,然后有一段时间我浏览这些记录并发送相应的邮件。到目前为止一切都很好并且有效。

我想做什么:在发送每封邮件后,我想要做的是在文本框中显示实时发送的电子邮件,但我无法这样做。

下面我展示了一个非常简单的非常简单的项目结构代码。

/****************CLASS FORM1***************************/
namespace envioEmails
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /********Shipping button*********/
        private void button1_Click(object sender, EventArgs e)
        {
            Consultas objetoConsultas = new Consultas();
            objetoConsultas.consultaBaseDatos();

        }


        private void campoTextBox_TextChanged_1(object sender, EventArgs e)
        {
            //In this textbox is where I want to display the data in real time.          
        }

    }
}   

/*********CLASS CONSULTAS (This class is not a form)*******************/


namespace envioEmails
{

    class Consultas
    {

        public void consultaBaseDatos(){

            //Here I connect to the database and get the records.

            //Then I run the records with a while ...
            while(myreader.Read()){
                string email=myreader["emails"].ToString());
                if(this.enviarCorreo(email){
                    /**This is where I should call the fieldTextBox event, but I am unable to do so.**/
                    campoTextBox.Text=email;
                }

            }
        }

        private bool enviarCorreo(string email){
             //I connect to the SMTP server and send the mail.

            try
            {
                //Code for sending ....
                return true;
            }
            catch()
            {
                return false;
            }

        }
    }
}

我还尝试在While内部实例化Form1类并指向事件,如下面的代码所示,但我没有得到任何结果

while(myreader.Read())
{
  string email=myreader["emails"].ToString());
  if(this.enviarCorreo(email){

      Form1 formularioPadre = new Form1();
     ((TextBox)formularioPadre.Controls["campoTextBox"]).Text = email;

    }
}

感谢您将来的答案。

2 个答案:

答案 0 :(得分:0)

最简单的方法可能是更改您的consultaBaseDatos以使用电子邮件返回List<string>

public List<string> consultaBaseDatos(){
    List<string> listEmails= new List<string>();    
    //Here I connect to the database and get the records.

    //Then I run the records with a while ...
     while(myreader.Read())
     {
         string email=myreader["emails"].ToString());
         if(this.enviarCorreo(email)
         {
                /**This is where I should call the fieldTextBox event, but I am unable to do so.**/
                listEmails.Add(email);
         }
     }
     return listEmails;
}

然后,在您的表单中,您可以做到:

private void button1_Click(object sender, EventArgs e)
{
    Consultas objetoConsultas = new Consultas();
    List<string> listEmails=objetoConsultas.consultaBaseDatos();
    campoTextBox.Text=string.Join(",",listEmails);

}

答案 1 :(得分:0)

抱歉,我暂时无法发表评论,所以我必须将此作为答案发布。

在我看来,您需要一个触发器来检查发送的电子邮件并定期更新您的文本字段内容。你尝试过使用定时器控制吗?每个&#34;计时器刻度事件&#34;,您可以检查是否有要显示的电子邮件列表,如果有,则更新您的文本字段。