在运行时向文本框发送值

本文关键字:文本 运行时 | 更新日期: 2023-09-27 18:07:06

我正在运行一个控制台应用程序。我有一个文本框的形式。我需要计算在另一个类的最后五分钟阅读的消息数,并在文本框中显示值。当我运行代码时,我可以看到文本框。文本值正确。但在UI中,我看不到文本框中显示的值。但是我可以在运行时手动编辑文本框。

下面是我的代码: 在代码后面

for (int i = 1; i <= numberOfMsgs; i++)
{
    if (addDateTime.AddMinutes(2).Minute==DateTime.Now.Minute)
    {
        //FilesProcessedInFiveMinutes();
        //Thread thread1 = new Thread(new ThreadStart(FiveMinutesMessage));
        //thread1.Start();
        WebSphereUI webSphereUi = new WebSphereUI();
        webSphereUi.count(fiveMinutesCount);
        addDateTime = DateTime.Now;
    }
    fiveMinutesCount = fiveMinutesCount + 1;
}
在form.cs

public void count(int countValue)
{
    Thread.Sleep(2000);
    txtLastFiveMins.Focus();
    txtLastFiveMins.Text = countValue.ToString();
    txtLastFiveMins.Refresh();
    txtLastFiveMins.Show();
    backgroundWorker1.RunWorkerAsync(2000);
}

在运行时向文本框发送值

看起来每次输入if语句时都在创建一个新表单。下面这行创建了一个新的WebSphereUI表单:

    WebSphereUI webSphereUi = new WebSphereUI();

然后,调用count方法:

    webSphereUi.count(fiveMinutesCount);

然后你继续,不显示这个表单。如果您添加:

    webSphereUi.Show();

然后您可能会看到表单出现在屏幕上,并按预期显示值。然而,这将在每次执行if语句时显示一个新的形式。您可以通过在其他地方声明它并在循环中使用它来重用相同的表单:

class yourClass
{
    WebSphereUI webSphereUi = new WebSphereUI();
    ...
    private void yourFunction()
    {
        for (int i = 1; i <= numberOfMsgs; i++)
        {
            if (addDateTime.AddMinutes(2).Minute==DateTime.Now.Minute)
            {
                webSphereUi.count(fiveMinutesCount);
                webSphereUi.Show();
                addDateTime = DateTime.Now;
            }
            fiveMinutesCount = fiveMinutesCount + 1;
        }
    }
}