不能更改标签文本

本文关键字:文本 标签 不能 | 更新日期: 2023-09-27 17:51:12

奇怪的bug。我有以下代码:

private void connectButton_Click(object sender, EventArgs e)
{
    statusLabel.Text = "Connecting...";
    statusLabel.ForeColor = Color.Green;
    serverNameBox.Enabled = false;
    databaseNameBox.Enabled = false;
    connectButton.Enabled = false;
    conn = new SqlConnection("server=" + serverNameBox.Text + ";Trusted_Connection=yes;database=" + databaseNameBox.Text + ";connection timeout=3");
    try
    {
        conn.Open();
    }
    catch (Exception ex)
    {
        statusLabel.Text = "Connection Failed";
        statusLabel.ForeColor = Color.DarkRed;
        MessageBox.Show("Connection Failed. Error message below:'n" + ex.Message);
        serverNameBox.Enabled = true;
        databaseNameBox.Enabled = true;
        connectButton.Enabled = true;
        return;
    }
    statusLabel.Text = "Connected Successfully";
    statusLabel.ForeColor = Color.DarkGreen;
    serverNameBox.Enabled = true;
    connectButton.Enabled = true;
    conn.Close();
    UpdateTraders();
    UpdateTransactions();
}

"连接成功"answers"连接失败"都可以正常工作。然而,statusLabel永远不会更改为"Connecting"。statusLabel的默认值。文本为"(无)。

这是怎么回事?

不能更改标签文本

因为您在UI线程中进行文本更改并阻塞它,因此会发生此问题。

您可以在更改标签文本后使用Application.DoEvents()来解决它,但最好的方法是使用多线程。例如,您可以使用BackgroundWorker类来实现此目的。

如果你使用的是。net 4.5或更高版本,你可以使用async来保持UI流畅。

像这样修改代码(注意asyncawait关键字的使用):

private async void connectButton_Click(object sender, EventArgs e)
{
    statusLabel.Text = "Connecting...";
    statusLabel.ForeColor = Color.Green;
    serverNameBox.Enabled = false;
    databaseNameBox.Enabled = false;
    connectButton.Enabled = false;
    conn = new SqlConnection("server=" + serverNameBox.Text + ";Trusted_Connection=yes;database=" + databaseNameBox.Text + ";connection timeout=3");
    try
    {
        await conn.OpenAsync();
    }
    catch (Exception ex)
    {
        statusLabel.Text = "Connection Failed";
        statusLabel.ForeColor = Color.DarkRed;
        MessageBox.Show("Connection Failed. Error message below:'n" + ex.Message);
        serverNameBox.Enabled = true;
        databaseNameBox.Enabled = true;
        connectButton.Enabled = true;
        return;
    }
    statusLabel.Text = "Connected Successfully";
    statusLabel.ForeColor = Color.DarkGreen;
    serverNameBox.Enabled = true;
    connectButton.Enabled = true;
    conn.Close();
    UpdateTraders();      // This might need the async treatment too
    UpdateTransactions(); // This might need the async treatment too
}

然而,这可能还不够,这取决于UpdateTraders()UpdateTransactions()的作用。它们可能也需要异步,并将一些缓慢的调用转换为await ...(假设它们支持它)。

你可以从这个代码中使用睡眠系统5秒,你可以为这个工作设置图像加载:

System.Thread.Sleep(5000);

如果您使用的是Windows应用程序,请设置进度条。

实际上"Connecting…"文本并没有显示在UI线程中,但它确实工作得很好。

因为在这个方法中,当第一行statusLabel.Text = "Connecting...";执行后,statuslabel文本将被临时更改,但没有显示在Form UI中,然后代码继续执行statuslabel的文本将被statusLabel.Text = "Connection Failed";取代,当conn失败或statusLabel.Text = "Connected Successfully";时,conn成功。

所以在UI线程中,我们只看到文本变为"Connected Failed"或"Connected Successfully"。

Debugging模式下,在statusLabel.Text = "Connecting...";行插入断点,当按F6跳过时,您可以看到通过Debug Locals窗口更改的statuslabel文本。

希望有帮助。谢谢。