Click here to Skip to main content
14,361,043 members
Rate this:
Please Sign up or sign in to vote.
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:
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
this.Hide();  // or this.Visible = false;

However, when I close the 2nd form, I could not use the code to open the 1st form.
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 6:10am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
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:
formViewData frmViewData = new formViewData(iCase, m_dt[iCase], m_dt[iCase]);
Hide();
frmViewData.ShowDialog();
Show();
Or handle the second form FormClosed event:
formViewData frmViewData = new formViewData(iCase, m_dt[iCase], m_dt[iCase]);
frmViewData.FormClosed += frmViewData_FormClosed;
Hide();
frmViewData.Show();

Then in the handler:
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.
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100