Click here to Skip to main content
13,046,679 members (66,759 online)
Rate this:
Please Sign up or sign in to vote.
See more:

could somebody tell me how can I write following codes in C++/CLI WinForm?
I've searched two days and tried several times, but without success.
private void OnOpenForm2Click(object sender, EventArgs e)
   var form2Obj = new Form2();
   form2Obj.Shown += (o, args) => { btnOpenForm2.Enabled = false; };
   form2Obj.FormClosed += (o, args) => { btnOpenForm2.Enabled = true; };
Posted 30-Oct-12 23:22pm
Updated 31-Oct-12 3:31am
You don't need lambda here so much, you need anonymous delegates. First of all, you can do it with "regular" named functions. If you need anonymous, which is way better in this case, you need the version of C++/CLI of VS2010 or later. What do you use?
christmars 31-Oct-12 10:23am
Yes the version I am using is VS2010. But I don't really understand how to use the "anonymous delegates" under c++/cli.

I've tried :

form2Obj->Shown += delegate(System::Object^ o, System::EventArgs^ args ) => {btnOpenForm2->Enabled = false;};

which didn't work...
Sergey Alexandrovich Kryukov 31-Oct-12 10:52am
The syntax is a bit different -- please see the article I referenced in my answer.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Lambda expressions are introduced in C++11:[^].

More exactly, what you need is anonymous methods. Please see:[^].

If your version of C++/CLI is new enough, you can use it on C++/CLI as well.

Please see also this article:
Using lambdas - C++ vs. C# vs. C++/CX vs. C++/CLI[^].

And also see my comment to the question. In worst case, if you just want the functionality or the code you show, you can always define separate named functions FormShownHandler(System::Object, System::EventHandler) and FormClosedHandler(System::Object, System.Windows.Forms::FormClosedEventArgs) instead of anonymous methods.

christmars 31-Oct-12 10:41am
So I still can't make it. I tried:

form2Obj->Shown += gcnew FormShownHandler(System::Object, System::EventArgs) => {btnOpenForm2->Enabled = false;};

The error is, FormShownHandler is not identified.
Could you please show what you would write? What you meant is right, but I need to see the form for the functionality!
Sergey Alexandrovich Kryukov 31-Oct-12 10:48am
No! You did not get it. In this case, FormShownHandler should be a function defined separately.

But OK, your version of C++/CLI is new enough. Use the anonymous delegate syntax shown in the article I referenced -- it will work. Locate this code snippet in this article:
RunFoo( [](int x) -> int { return x; });

christmars 31-Oct-12 11:08am
Sorry I didn't see the reference. I would read the article you give me carefully.
Thank you!
Sergey Alexandrovich Kryukov 31-Oct-12 11:20am
You are very welcome.
Good luck, call again.
Sergey Alexandrovich Kryukov 31-Oct-12 11:26am
After I saw your clarification on VS2010, it became clear that you can keep anonymous methods in C++/CLI, as you can use new features of C++11.

I found that Wikipedia covers this topic really well, with syntax and samples. Please see my updated answer.
christmars 1-Nov-12 4:26am
Hi SA,I have read the articles. And I think your suggestion is like this:

//firstlly I declared Form to be openedauto Form^ form2Obj = gcnew Form2();

//Then, define FormShownHandler(here use the anonymous method)

//Although refers to the wiki samples, I didn't define this succesfully:

auto FormShownHandler = [ ] ( ) { button->Enabled=false; };

//After that, use the self created FormShownHandler instead of the system defined one:

form2Obj->Shown += FormShownHandler;

Have I gotten your idea? The use of anonymous function was wrong, could you help?
christmars 1-Nov-12 5:08am
Hi SA,
I got it!

//make two funktions for EventHandler:
public : void DisableButton1(System::Object ^,System::EventArgs ^)
public : void EnableButton(System::Object ^,System::EventArgs ^)

private :System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
Form^ form2Obj = gcnew Form2();
form2Obj->Shown += gcnew System::EventHandler(this, &Form1::DisableButton1);
form2Obj->Closed += gcnew System::EventHandler(this, &Form1::EnableButton1);

I did't use anonymous methods to solve this problem, but without your advices I couldn't know where was my mistake. Thanks again for the articles you gave me, they are helpful!
Well, I would always prefer anonymous method, because they simplify code and improve maintenance. You better learn using them.
But I'm so glad you can do the solution now.

You are very welcome.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100