Solved

C++ delegate to update GUI textbox

Posted on 2013-12-22
1
809 Views
Last Modified: 2013-12-27
I'm moving from C# where I know how to do things to C++ where I'm unfamiliar with the syntax.

I have a simple C++ winforms application with a textbox named textBox1, and I want to write a delegate so I can update the text from other threads.
    delegate void UpdateTextBox1delegate(System::String^ someText);

  public: 
    void Update(System::String^ someText) {

        if (this->textBox1->InvokeRequired) {

            UpdateTextBox1delegate^ d = gcnew UpdateTextBox1delegate(this, &Form1::UpdateTextBox1delegate);
            this->Invoke(d,someText);
        }
        else {
            textBox1->Text = someText;
        }
    }

Open in new window

Gives me this error complaining about line 8:
error C2275: 'DelegateTest::Form1::UpdateTextBox1delegate' : illegal use of this type as an expression
error C3350: 'DelegateTest::Form1::UpdateTextBox1delegate' : a delegate constructor expects 2 argument(s)
(I'm also surprised I can't find any sample C++ code that does this.)
0
Comment
Question by:deleyd
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39735811
I think "the standard example" for this is: http://msdn.microsoft.com/en-us/library/vstudio/ms171728(v=vs.90).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-2

There are some user comments that complain about missing info but it seems it only needs a delegate definition from a later example on the same page. You already have that defined so should be able to use:
    delegate void UpdateTextBox1delegate(System::String^ someText);

  public: 
    void Update(System::String^ someText) {

        if (this->textBox1->InvokeRequired) {

            UpdateTextBox1delegate^ d = gcnew UpdateTextBox1delegate(this, &Form1::Update);
            this->Invoke(d, gcnew array<Object^> { someText });
        }
        else {
            this->textBox1->Text = someText;
        }
    }

Open in new window

So 2 changes:
1) refer to the class method, not the delegate when creating the delegate.
2) pass an object array instead of the argument directly

Note I'm not a C++.NET programmer myself (so please double check syntax), but have made mistakes like this in several languages in the past ;-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now