Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
2.00/5 (5 votes)
See more:
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

C#
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
Updated 3-Jun-18 20:35pm
v2
Comments
incaunu 9-Jan-12 9:18am    
try newForm.ShowDialog(this);

Do it the other way around, and use ShowDialog instead;

C#
Form2 newform = new Form2();
this.Hide();
newForm.ShowDialog();
this.Show();
ShowDialog will not return until newform is closed.
 
Share this answer
 
Comments
Jashobanta 10-Jan-12 13:35pm    
Thanks @OriginalGriff..It worked..
OriginalGriff 10-Jan-12 13:59pm    
You're welcome!
You can do that on this way:
(I'm not sure for the quality, but it works for me)
C#
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;
}
 
Share this answer
 
v3
Comments
OriginalGriff 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 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 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 10-Jan-12 5:32am    
Thanks
Link the two forms (Form1 knows about Form2 and Forms2 knows about Form1).
Then use Form.Activate, Form.Deactivate, Form.Closed events on both
 
Share this answer
 
There is also a new solution by just changing the receiver form's Constructor with some parameter and based on this parameter you can read the data.

For example

C#
.......
string param;
public ReceiverFormName(string param)
{
this.param=param;
}
.......


And Call it As

C#
........
ReceiverFormName rec = new ReceiverFromName("Hello World");
........



I have Tested this for me! SGSM_Awesh
 
Share this answer
 
v2

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