daveamour
asked on
Forms Communication
If I have a windows forms application in c# which has a main form with a button on it. When this button is clicked another form is opened and the main form is hidden. when the second form is closed I want to unhide the main form. How do I do this - ie how do I access the main forms properties form the other form?
Thanks
Dave
Thanks
Dave
You don't have to do it that way. When the subform is started using ShowDialog it will stop at that line until the subform is closed.
void someMethodInMainForm()
{
SubForm sf = new SubForm();
this.Hide();
DialogResult dr = sf.ShowDialog();
this.Show();
}
void someMethodInMainForm()
{
SubForm sf = new SubForm();
this.Hide();
DialogResult dr = sf.ShowDialog();
this.Show();
}
ASKER
johan - this is very useful but might not give me enough control - ie I might have the subform closing and opening another one and not until I explicitly decide will I want to show the main form again. This is very useful though, thanks.
develc - Not sure how to bind the class to my main form. Also with your first example aren't you just creating another instance of my main form which will leave you with 2 running and 1 of them hidden?
Thanks
Dave
develc - Not sure how to bind the class to my main form. Also with your first example aren't you just creating another instance of my main form which will leave you with 2 running and 1 of them hidden?
Thanks
Dave
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks EBatista this is exactly what I wanted!
I uses this.Owner.Show() though without the cast. Why did you use a cast?
Thanks
Dave
PS If I have another method in frmMain eg:
public void SayHello()
{
MessageBox.Show("Hello");
}
How would I call this from the second form - it doesn't work using the above method but the Show method does?
I uses this.Owner.Show() though without the cast. Why did you use a cast?
Thanks
Dave
PS If I have another method in frmMain eg:
public void SayHello()
{
MessageBox.Show("Hello");
}
How would I call this from the second form - it doesn't work using the above method but the Show method does?
well, the cast is needed cos the Owner property return a generic Form object, so you have to convert it to the correct type which is frmMain in order to use its properties.
the MessageBox.Show("Hello") should work, it is maybe that frmMain is hidden, try it without hiding it.
the MessageBox.Show("Hello") should work, it is maybe that frmMain is hidden, try it without hiding it.
ASKER
Ok I see now. My code worked as my form is a generic form but if it had been inherited from a class which in turn inherits from the generic form then it would break.
I figured out the SayHello problem - I didn't have the brackets in the right place:
((frmMain)this.Owner).SayH ello() is needed whereas I was trying (frmMain)this.Owner.SayHel lo()
Thanks
Dave
I figured out the SayHello problem - I didn't have the brackets in the right place:
((frmMain)this.Owner).SayH
Thanks
Dave
maybe that helps you :
Second From closed :
{
FormMain<Name> fMain = new FormMain<Name>s();
fMain.Show();
}
Access to the main forms properties you can get on the same way or you can use a global class :
public class Global
{
public Global()
{
}
public static SortedList Data1 = null;
public static ArrayList Data2 = null;
public static Form1 FormMain = null;
public static string strExample = null;
// and so on
}
then you can bind the class to your MainForm and others using static.
Good luck.
Best regards develc