2012-01-11 80 views
0

我已經提出了類似的問題,但我找到的解決方案部分幫助我,因此我提出的問題類似於以前的問題。 我的問題是,在線程中,我希望按鈕的文本更改。 線程工作正常,我可以看到我正在顯示的MessageBox,但該按鈕的文本保持不變。 我該如何改變它? 如果我需要使用代表(註釋文本),我是否解決它?因爲該代碼引發了錯誤「(」和一些錯誤「{」但是這之前在一個線程中更改Windows窗體控件

#pragma once 


namespace UIThread { 

using namespace System; 
using namespace System::ComponentModel; 
using namespace System::Collections; 
using namespace System::Windows::Forms; 
using namespace System::Data; 
using namespace System::Drawing; 
using namespace System::Threading; 

/// <summary> 
/// Summary for Form1 
/// 
/// WARNING: If you change the name of this class, you will need to change the 
///   'Resource File Name' property for the managed resource compiler tool 
///   associated with all .resx files this class depends on. Otherwise, 
///   the designers will not be able to interact properly with localized 
///   resources associated with this form. 
/// </summary> 


public ref class Form1 : public System::Windows::Forms::Form 
{ 

public: 
    Form1(void) 
    { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 
    } 

protected: 
    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    ~Form1() 
    { 
     if (components) 
     { 
      delete components; 
     } 
    } 
private: System::Windows::Forms::Button^ BtnStart; 
protected: 

private: 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    System::ComponentModel::Container ^components; 

#pragma region Windows Form Designer generated code 
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    void InitializeComponent(void) 
    { 
     this->BtnStart = (gcnew System::Windows::Forms::Button()); 
     this->SuspendLayout(); 
     // 
     // BtnStart 
     // 
     this->BtnStart->Location = System::Drawing::Point(114, 38); 
     this->BtnStart->Name = L"BtnStart"; 
     this->BtnStart->Size = System::Drawing::Size(124, 37); 
     this->BtnStart->TabIndex = 0; 
     this->BtnStart->Text = L"button1"; 
     this->BtnStart->UseVisualStyleBackColor = true; 
     this->BtnStart->Click += gcnew System::EventHandler(this, &Form1::BtnStart_Click); 
     // 
     // Form1 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(284, 262); 
     this->Controls->Add(this->BtnStart); 
     this->Name = L"Form1"; 
     this->Text = L"Form1"; 
     this->ResumeLayout(false); 

    } 
#pragma endregion 
private: System::Void BtnStart_Click(System::Object^ sender, System::EventArgs^ e) { 
    Form1^ f=gcnew Form1; 
    Thread^ oThread = gcnew Thread(gcnew ThreadStart(f, &Form1::ThreadMethod)); 
    oThread->Start(); 
} 

private: void ThreadMethod(/*Object^ state*/) 
{ 
    BtnStart->Text="hello"; 
    MessageBox::Show("AAAAAA"); 
    //this->Invoke((Action^)delegate(){BtnStart->Text = "Hello";}); 
} 
}; 

我得到了答案}

回答

2

你設法擺脫告訴你,你正在做的運行時異常它是錯誤的,通過創建一個新的表單對象,因爲你從來沒有調用過它的Show()方法,所以你不能看到它,所以你不能看到按鈕的文本更新,你想要做的就是更新現有的 form object:

System::Void BtnStart_Click(System::Object^ sender, System::EventArgs^ e) { 
    Thread^ oThread = gcnew Thread(gcnew ThreadStart(this, &Form1::ThreadMethod)); 
    oThread->Start(); 
} 

您在上一個問題上得到了不好的建議,該語法僅適用於C#。 C++/CLI不支持匿名代理。你必須把它寫出來,就像你一樣的ThreadStart委託:

void UpdateButton() { 
    BtnStart->Text="hello"; 
} 

void ThreadMethod() { 
    this->Invoke(gcnew MethodInvoker(this, &Form1::UpdateButton)); 
} 
+0

謝謝你,我現在就somithing不同,我得到一個運行時錯誤,說(我是從意大利的翻譯),無法調用在控件上調用或BeginInvoke,直到未創建窗口句柄。任何想法? – andrea 2012-01-11 15:16:25

+0

確定找到了!這是一個愚蠢的錯誤,Thread^oThread = gcnew Thread(gcnew ThreadStart(this,&Form1 :: ThreadMethod));使用這個而不是f是正確的....非常感謝你,希望雙倍upvote你的答案 – andrea 2012-01-11 15:26:59

相關問題