Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi,
 
I am creating a C# Windows forms application where there are 2 forms Form1 and Form2. I need to connect these 2 forms in a way that when one form is active other should be disabled. For eg, Form1 has a button on the click event of which form 2 is opened in the way
 
Form2 newForm= new Form2();
newForm.Show();
this.Hide();
 

Now I want the functionality that when the Form2 is activated,then the Form1 which is open should be deactivated or Disabled, and it should be activated again when the form Form2 is closed .Can anybody help me out on solving this issue.Thanks in Advance.
 
Thanks
Jashobanta Chakraborty
Posted 9-Jan-12 4:07am
Edited 9-Jan-12 4:13am
lukeer15.2K
v2
Comments
incaunu at 9-Jan-12 9:18am
   
try newForm.ShowDialog(this);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Do it the other way around, and use ShowDialog instead;
 
Form2 newform = new Form2();
this.Hide();
newForm.ShowDialog();
this.Show();
ShowDialog will not return until newform is closed.
  Permalink  
Comments
Jashobanta at 10-Jan-12 13:35pm
   
Thanks @OriginalGriff..It worked..
OriginalGriff at 10-Jan-12 13:59pm
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can do that on this way:
(I'm not sure for the quality, but it works for me)
private void button1_Click(object sender, EventArgs e)
{
    Form2 newForm = new Form2(this);
    newForm.Show();
    this.Hide();
}
 
//In Form2
Form parentForm;
public Form2(Form form)
{
    InitializeComponent();
 
    this.parentForm = form;
}
 
// and on closing event
private void Form2_FormClosing(object sender, EventArgs e)
{
    this.parentForm.Visible = true;
}
  Permalink  
v3
Comments
OriginalGriff at 9-Jan-12 14:15pm
   
My only criticism of this is that it makes Form2 aware of Form1 - and Form1 is already aware of Form2. This reduces the scope for form reuse, and fixed what happens to an object outside Form2s domain.
This would be a bit better if the Form1 instance was passed through to the Form2 constructor as a generic Form object rather than the derived Form1, but it is still better in OOP terms to let Form1 handle itself, and Form2 remain in ignorance of it's existence. (Oh, and BTW m_variablenames are not the normal style for C#, it is recommended to use camelCase instead.)
Form1 m_form;
public Form2(Form1 form)
becomes
Form parentForm;
public Form2(Form form)
Drazen Pupovac at 9-Jan-12 17:04pm
   
I agree, thanks for the advice. I improved my solution now (How much is it possible :)).
 
One question: I read that for private fields have naming convention like _variable. It's that ok?
OriginalGriff at 10-Jan-12 3:22am
   
Not normally, no. In C# a leading underscore would normally indicate that this is teh private backing variable for a public method of a similar name:
private _myString;
public MyString
{
get {return _myString;}
set {_myString = value;}
}
But not everybody uses them, some would call the private field "myString" (I follow this myself)
 
Have a look at MSDN: http://msdn.microsoft.com/en-us/library/ms229045.aspx
The links on the left are pretty detailed.
Drazen Pupovac at 10-Jan-12 5:32am
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Link the two forms (Form1 knows about Form2 and Forms2 knows about Form1).
Then use Form.Activate, Form.Deactivate, Form.Closed events on both
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150305.1 | Last Updated 9 Jan 2012
Copyright © CodeProject, 1999-2015
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