Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI C# WinForm
Hello WinForm App experts,
 
Could you tell me if this is possible:
 
1. Form1 Button1_Click -> Form2 Show(), Form1 Button1 disable
// The first event above is clearly no prolem!
 
2. Form2 closed -> Form1 Button1 enable
// I can't make this till now
 

Why I want to do this? Because I must use Show() method instead of ShowDialog(), so that the form1 and form2 can be operated parallelly. But the problem is that, I want the button1 in form1 only to be enabled while form2 is closed.
 
Is controlling a control in anoter form possible? I saw articles for c#, but when I "translate" them in C++/CLI, it doesn't seem to work.
So I used varible to enable the button1. Also when Form2 Closing, do a=1;
I want get the a=1 in an event for form1, but I can't find a suitable one.
When my idea is impossible. Could you give me your advices?
 
Thank you in advance!
 

Hi mates,
 
now it works well with Marcus' c# code. But I also want to write it into C++/CLI, and I don't quite understand what "(o, arg)=>" is. Is it the so-called "lambda expression"? How should it be written in C++/CLI?
I know at least that the "var form2Obj = new Form2();" should like this in C++/CLI:
auto form2Obj = gcnew Form2();
 
Thank you for helping!
Posted 26-Oct-12 4:38am
Edited 30-Oct-12 1:14am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use event based communication. That way your UI conforms to proper coding etiquette.
// In Form1 code
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();
}
  Permalink  
Comments
adriancs at 26-Oct-12 11:54am
   
This looks better.
Marcus Kramer at 26-Oct-12 11:57am
   
Thanks
christmars at 29-Oct-12 4:58am
   
Hi Marcus! Your code works perfect under c#. Can you tell me how can I code it into C++/CLI. I tried this:
auto form2Obj = gcnew Form2();
form2Obj->Shown += (Object^ o, EventArgs^ args)=> {button1->Enabled = false;};
...
But this is wrong.
I don't think I 've understood "(o, args)=>". Can you show me how they should look like under C++/CLI?
 
Thanks again!
christmars at 29-Oct-12 3:49am
   
Thanks a lot!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

[Edit]
Marcus's Answer would be better.
[/Edit]
 
This can be done by passing the button control from Form1 into Form2.
Example:
private void button1_Click(object sender, EventArgs e)
{
    button1.Visible = false;
    // pass in the button into Form2
    Form2 f = new Form2(ref button1);
    f.Show();
}
Then, during Form2 is closing, make the button visible again.
Example:
public partial class Form2 : Form
{
    Button btForm1 = null;
 
    // Receive button1 control from Form1
    public Form2(ref Button bt)
    {
        btForm1 = bt;
        InitializeComponent();
        FormClosing += new FormClosingEventHandler(Form2_FormClosing);
    }
 
    void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        // make the button visible again
        btForm1.Visible = true;
    }
}
  Permalink  
v3
Comments
Marcus Kramer at 26-Oct-12 11:23am
   
My vote of 1: No form should ever know anything about another Form's visual components.
adriancs at 26-Oct-12 11:42am
   
This is just depends on the programmer's requirements and needs.
He need the 2nd form to alter 1st form's button, then he'll code it that way.
By hiding the button1, this can prevent user for opening the same form twice.
The OP opens the form by using Show(), not ShowDialog().
Marcus Kramer at 26-Oct-12 11:52am
   
I totally understand the OP's problem. See my answer for the proper way to do this.
My point isn't whether or not your code would work because it would to some extent. My point is that doing it your way here would cause a horrible maintenance issue because any changes to Form1 would require the developer to know that Form2 references it which adds unneeded and unwarranted complexity. Forms should never know about the internal working of another form.
adriancs at 26-Oct-12 11:56am
   
I saw your code. That sounds better.
Marcus Kramer at 26-Oct-12 11:57am
   
Thanks.
christmars at 29-Oct-12 3:47am
   
Thank you guys!

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

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 30 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