簡體   English   中英

無法更改標簽文字

[英]Cannot change label text

奇怪的錯誤。 我有以下代碼。

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();
}

“連接成功”和“連接失敗”都可以正常工作。 但是,statusLabel永遠不會更改為“正在連接”。 statusLabel.Text的默認值為“”(無)。

這里發生了什么?

因為您要在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應用程序。

實際上,“正在連接...”文本並未顯示在UI線程中,但確實工作正常。

因為在這種方法中,當第一行之后的狀態為statusLabel.Text = "Connecting..."; 執行后,statuslabel文本將被臨時更改,但不會顯示在Form UI中,然后繼續執行statuslabel文本的代碼將被statusLabel.Text = "Connection Failed";替換statusLabel.Text = "Connection Failed"; 當conn失敗或statusLabel.Text = "Connected Successfully"; 當conn成功時。

因此,在UI線程中,我們只看到文本更改為“連接失敗”或“連接成功”。

調試模式下,將斷點插入statusLabel.Text = "Connecting..."; 行中,按F6鍵越過時,您可以通過Debug Locals窗口看到statuslabel文本更改。

希望對您有所幫助。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM