Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project, there are 2 forms. From the 1st (or main) form, I use the code below to show the 2nd one:
C#
formViewData frmViewData = new formViewData(iCase, m_dt[iCase], m_dt[iCase]);
frmViewData.Show();
frmViewData.BringToFront();

However, the 2nd one is still behind the 1st one. What's the proper way to solve it?
Besides I also tried another way, namely hide the 1st form by
C#
this.Hide();  // or this.Visible = false;

However, when I close the 2nd form, I could not use the code to open the 1st form.
C#
View.uxMain.Show();  // Error: An object reference is required for the non-static field, method, or properties ...

What's the proper way to solve this problem? Thanks if you can share your experience.

What I have tried:

Not successful to bring a active form to front or re-show a hidden form
Posted
Updated 24-May-17 5:10am

1 solution

Show is intended to display a new, independant form, and let the existing one continue as before - which means that when it has the user focus, it can't be "behind" the second form or the user can't see what he is doing!.

If what you are trying to do is show a form that "takes control" in the same way that a MessageBox does - effectively disabling the "parent" form until the MessageBox is closed - then that's simple:
C#
formViewData frmViewData = new formViewData(iCase, m_dt[iCase], m_dt[iCase]);
frmViewData.ShowDialog();
Processing on your main form will stop until the form is closed.
If you want to hide your main form while the other is displayed, then either:
C#
formViewData frmViewData = new formViewData(iCase, m_dt[iCase], m_dt[iCase]);
Hide();
frmViewData.ShowDialog();
Show();
Or handle the second form FormClosed event:
C#
formViewData frmViewData = new formViewData(iCase, m_dt[iCase], m_dt[iCase]);
frmViewData.FormClosed += frmViewData_FormClosed;
Hide();
frmViewData.Show();

Then in the handler:
C#
void frmViewData_FormClosed(object sender, FormClosedEventArgs e)
    {
    Show();
    }

You can do it by setting the second form to "always on top" - but that's generally more trouble than it's worth as it's system wide not application specific.
 
Share this answer
 
Comments
s yu 24-May-17 12:54pm    
Thanks for your answer. My problem is actually the previously opened and then hidden (by Hide()) main form can't be open again. In the 2nd form: frmViewData, I typed the main form's name: uxMain. But I could not find any related method to do so. To re-open it in which the previous settings are still there, I have to click the tool icon.
OriginalGriff 24-May-17 14:04pm    
Of course you can't! The second form shouldn't even know the first form exists, much less that it's hidden.
That's why the code I showed you uses Show from the FormClosed event handler: when the second form closes, the main form shows itself...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900