Solved

C++ delegate to update GUI textbox

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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