Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

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.
C#
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; };
   form2Obj.Show();
}
Posted
Updated 31-Oct-12 3:31am
v2
Comments
Sergey Alexandrovich Kryukov 31-Oct-12 9:46am    
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?
--SA
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.
--SA

1 solution

Lambda expressions are introduced in C++11:
http://en.wikipedia.org/wiki/C%2B%2B11#Lambda_functions_and_expressions[^].

More exactly, what you need is anonymous methods. Please see:
http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B[^].

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.

—SA
 
Share this answer
 
v2
Comments
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; });

--SA
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.
--SA
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.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900