无法更改标签文字

时间:2015-05-31 08:15:15

标签: c# winforms label

奇怪的错误。我有以下代码。

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的默认值为“”(无)。

这里发生了什么?

4 个答案:

答案 0 :(得分:0)

因为您在UI线程中进行文本更改并阻止它,因此会出现此问题。

您可以在更改标签文字后使用main': sfml.cpp:(.text+0x12d): undefined reference to解决此问题,但最好的方法是使用多线程。例如,您可以使用#include <SFML/Graphics.hpp> int main() { sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!"); sf::CircleShape shape(100.f); shape.setFillColor(sf::Color::Green); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(); window.draw(shape); window.display(); } return 0; } 类来实现此目的。

答案 1 :(得分:0)

如果您使用的是.Net 4.5或更高版本,则可以使用async来保持用户界面畅通。

更改您的代码(请注意使用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 ...(假设它们支持它)。

答案 2 :(得分:0)

您可以在此代码中使用睡眠系统5秒,然后您可以为此工作加载图像:

System.Thread.Sleep(5000);

如果您使用的是Windows应用程序,请为此工作添加进度条。

答案 3 :(得分:-1)

实际上是&#34;连接......&#34;文本没有在UI线程中显示,但它确实工作正常。

因为在这种方法中,当执行第一行statusLabel.Text = "Connecting...";之后,statuslabel文本将暂时更改但未在表单UI中显示,然后代码继续执行statuslabel的文本将当conn失败时被statusLabel.Text = "Connection Failed";替换,或者当conn成功时被statusLabel.Text = "Connected Successfully";替换。

所以在UI线程中,我们只看到文本更改为&#34; Connected Failed&#34;或者&#34;成功连接&#34;。

调试 模式下,在statusLabel.Text = "Connecting...";行插入断点,按F6跳过,即可看到通过Debug Locals 窗口更改了statuslabel文本。

希望对你有所帮助。感谢。

相关问题