Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI C# .NET WinForm
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.
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 31-Oct-12 0:22am
Edited 31-Oct-12 4:31am
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 31-Oct-12 10:52am
   
The syntax is a bit different -- please see the article I referenced in my answer.
--SA

1 solution

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

Solution 1

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
  Permalink  
v2
Comments
christmars at 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 at 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 at 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 at 31-Oct-12 11:20am
   
You are very welcome.
Good luck, call again.
--SA
Sergey Alexandrovich Kryukov at 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
christmars at 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 at 1-Nov-12 5:08am
   
Hi SA,
I got it!
Codes:
 
//make two funktions for EventHandler:
public : void DisableButton1(System::Object ^,System::EventArgs ^)
{this->button1->Enabled=false;}
public : void EnableButton(System::Object ^,System::EventArgs ^)
{this->button1->Enabled=true;}
 
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);
form2Obj->Show();
}
 
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!
Sergey Alexandrovich Kryukov at 1-Nov-12 11:15am
   
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.
--SA

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

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2014
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