【问题标题】:Label Text not changing on C++/CLR Windows Forms标签文本在 C++/CLR Windows 窗体上未更改
【发布时间】:2021-12-12 18:23:15
【问题描述】:

我正在使用 .NET Framework 4.0 在 Visual Studios Community 2019 上开发一个小型 C++/CLR Windows 窗体项目,其中我有一个组合框和一个标签。

下面的代码片段可以正常工作:

private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
            label1->Text = "comboBox1->Text";
        }

但如果我在label1->Text = "comboBox1->Text"; 之后添加Sleep(1000);,我希望标签会在睡眠期之前更改,但在睡眠期结束后会更改。

一般来说,label1->Text = "comboBox1->Text"; 会在该行下方的任何内容之后执行。

对于下面的代码片段,我希望程序在更改 label1 文本后休眠。

private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
            label1->Text = "comboBox1->Text";
            Sleep(1000);
        }

【问题讨论】:

  • 按照设计,标签不会重新绘制自身,直到操作系统告诉它这样做。当 UI 线程处于睡眠状态时,这不会发生。 label1->Update() 在睡眠加速之前。永远不要让 UI 线程休眠,总是更喜欢 Timer。

标签: c++ visual-studio winforms visual-c++ clr


【解决方案1】:

我明白你的意思。要实现此功能,您需要使用timer。您需要在 WinForm 中添加一个timer,然后在 timer 属性中将 Interval 值设置为 1000。你需要使用Start来启动定时器,你可以参考我的代码。

this->timer1->Interval = 1000;

this->timer1->Tick += gcnew System::EventHandler(this, &MyForm::timer1_Tick);

timer1->Start();

private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { label1->Text = comboBox1->Text; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多