0

我有一个带有 a和 a的 windows 窗体 ( Form1.h) 。初始化表单时为空。单击按钮时,将调用表单外部的方法,它应该更新. 我将如何从非表单类更新?下面是我的示例代码:ButtonTextBoxTextBoxTextBoxTextBox

// 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.
}
4

1 回答 1

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 ";
于 2012-10-14T19:22:12.150 回答