2012-05-23 58 views
0

我有一個窗體(Form1.hButtonTextBox。表單初始化時,TextBox爲空。點擊按鈕後,將調用表單外的方法,並應更新TextBox。我如何從非形式課程更新TextBox?下面是我的示例代碼:從窗體外的公共方法更新文本框

// Form1.h 
private: System::Void findResultButton_Click(System::Object^ sender, System::EventArgs^ e) { 
    FirstResults* firstResults = new FirstResults(); 
    firstResults->findResult(); 
} 

// FirstResults.cpp 
void FirstResults::findResult() { 
    // do some calculations here and find result. 
    // write the result value to a .txt file. 
    // Update TextBox in Form1.h with result value. 
} 
+0

不是一個C++專家,但是你可以簡單地將引用傳遞給你的函數,然後在那裏更新文本? – Steve

+0

從該函數返回一個'List ^'。或者將它傳遞給一個委託,這樣可以進行回調。類似的東西。 –

+0

@HansPassant謝謝。我已經與代表一起嘗試過了,現在我正在尋找方法來在unmanged類中聲明託管代表。 –

回答

2

首先,你需要創建格式的靜態實例。 然後在任何.cpp文件要訪問TextBox1的項目或textarea你只是

public ref class Form1 : public System::Windows::Forms::Form 
{ 
public: 
    static Form1^ myForm1; 

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

然後在的.cpp #include "form1.h"

Form1^ myform1 = gcnew Form1(); 
Form1::myForm1->textBox1->Text = L" FROM the main.cpp "; 

,或者您需要

System::Windows::Forms::myform1->textBox1->Text = L" FROM the main.cpp "; 
相關問題