Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 30-Oct-12 0:14am
v2

Use event based communication. That way your UI conforms to proper coding etiquette.
C#
// 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();
}
 
Share this answer
 
Comments
adriancs 26-Oct-12 11:54am    
This looks better.
fjdiewornncalwe 26-Oct-12 11:57am    
Thanks
christmars 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 29-Oct-12 3:49am    
Thanks a lot!
[Edit]
Marcus's Answer would be better.
[/Edit]

This can be done by passing the button control from Form1 into Form2.
Example:
C#
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:
C#
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;
    }
}
 
Share this answer
 
v3
Comments
fjdiewornncalwe 26-Oct-12 11:23am    
My vote of 1: No form should ever know anything about another Form's visual components.
adriancs 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().
fjdiewornncalwe 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 26-Oct-12 11:56am    
I saw your code. That sounds better.
fjdiewornncalwe 26-Oct-12 11:57am    
Thanks.

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