C++ 中的 .NET 4.0 - 如何从静态成员函数调用 RichTextBox 的 AppendText
我的思想不知何故陷入了“错误循环”。我不想再浪费时间进行无休止的试验和错误,所以我最好在这里问:
我有一个如下所示的 Windows 窗体(.NET、C++)。这里的简化版本只有一个RichTextBox、一个静态和一个非静态成员函数。从非静态函数“nonstaticFunc()”将文本附加到 RichTextBox 可以按预期工作。
但是我如何从静态成员函数“staticFunc()”中做到这一点呢?我尝试了本论坛中提出的几种关于如何从静态函数调用非静态函数的方法,但不知何故我不知道如何做到这一点。
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1()
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
protected:
private:
System::ComponentModel::Container ^components;
private: System::Windows::Forms::RichTextBox^ myTextBox;
System::VoidInitializeComponent( System::Void )
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->myTextBox = (gcnew System::Windows::Forms::RichTextBox());
}
public: System::Void nonstaticFunc( System::Void )
{
this->myTextBox->AppendText( L"Append this...\n" );
}
public: static System::Void staticFunc( System::Void )
{
// How do I AppendText here??
// Not working: this->myTextBox->AppendText( L"Append this...\n" );
}
}
感谢您的每一点帮助!非常感谢!
My mind somehow is stuck in a "loop of errors". I don't want to waste time any more with endless trial and error, so I better ask here:
I have a Windows-Form (.NET, C++) like following. The simplified version here only has a RichTextBox, a static and a non-static member function. Appending Text to the RichTextBox from the non-static function "nonstaticFunc()" works as expected.
But how can I do this from the static member function "staticFunc()"? I tried several approaches proposed in this forum on how to call non-static functions from static functions, but somehow I couldn't figure out how to do this.
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1()
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
protected:
private:
System::ComponentModel::Container ^components;
private: System::Windows::Forms::RichTextBox^ myTextBox;
System::VoidInitializeComponent( System::Void )
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->myTextBox = (gcnew System::Windows::Forms::RichTextBox());
}
public: System::Void nonstaticFunc( System::Void )
{
this->myTextBox->AppendText( L"Append this...\n" );
}
public: static System::Void staticFunc( System::Void )
{
// How do I AppendText here??
// Not working: this->myTextBox->AppendText( L"Append this...\n" );
}
}
Thanks for every little bit of help! Appreciated a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确定您对哪个文本框感兴趣。如果有两个可见表单怎么办?你没有足够的背景。
现在您可以保留一个静态成员来跟踪“唯一的真实形式” - 或者您可以将文本框或表单作为参数......但从根本上来说您需要拥有该上下文不知怎的。。
为什么你要从
staticFunc
执行此操作?为什么调用者不能调用适当形式的方法?一旦你理解了问题——为什么它不起作用——你应该能够考虑最合适的改变。我们无法真正告诉你,因为我们没有足够的信息。You need to work out which text box you're interested in. What if there are two visible forms? You don't have enough context.
Now you could keep a static member to keep track of "the one true form" - or you could take the textbox or form as a parameter... but fundamentally you need to have that context somehow.
Why do you want to do this from
staticFunc
anyway? Why can the caller not call the method on the appropriate form? Once you understand the problem - why it won't work - you should be able to think about the most appropriate change. We can't really tell you that, as we don't have enough information.