?
Solved

C++ delegate to update GUI textbox

Posted on 2013-12-22
1
Medium Priority
?
995 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 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