Solved

C++ delegate to update GUI textbox

Posted on 2013-12-22
1
902 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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