Winforms 从窗体外部的公共方法更新windows窗体中的文本框

Winforms 从窗体外部的公共方法更新windows窗体中的文本框,winforms,textbox,c++-cli,Winforms,Textbox,C++ Cli,我有一个windows窗体(Form1.h),带有按钮和文本框。初始化表单时,文本框为空。单击按钮时,将调用表单外部的方法,该方法应更新文本框。如何从非表单类更新文本框?下面是我的示例代码: // Form1.h private: System::Void findResultButton_Click(System::Object^ sender, System::EventArgs^ e) { FirstResults* firstResults = new FirstResult

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

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

首先,您需要创建表单的静态实例。 然后在任何.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
中包含“form1.h”

或者如果你需要的话

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

不是C++专家,但你能简单地把TyTror的引用传递给函数,然后更新那里的文本吗?从那个函数返回一个<代码>列表> />代码。或者给它传递一个委托,以便它可以进行回调。“像那样的事儿。”谢谢你。我已经用委托尝试过了,现在我正在寻找在非托管类中声明托管委托的方法。在搜索框中键入“marshal::getfunctionpointerfordelegate”。“第一首歌看起来不错!”汉帕桑是的,我已经看完了。似乎每次我寻找确切的帮助时都会碰到你。无论如何,谢谢:)
System::Windows::Forms::myform1->textBox1->Text = L" FROM the main.cpp ";