开发者

.NET 4.0 in C++ - How call AppendText for RichTextBox from a static member function

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 propose开发者_如何学JAVAd 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜