Which form is active but hiden

  • Thread starter Thread starter Vladimir O¾ura
  • Start date Start date
V

Vladimir O¾ura

I am building a Pocket PC application and I have 5 forms (form1, form2,
....). Form1 starts up and I click a button for Form2 to show up. Form1 is
hiden. On Form2 i click a button for Form3 to show up. Now Form2 is hiden.
Then in Form2 I click a button to show Form1. How can i just say "SHOW
Form1"? I did it by doing this:

Form1 form1 = new Form1();
form1.show();

But this creates a new Form1. Is there another way to do this?


Thanks!!!
 
Vladimir,

You have to do this the same you hide yours forms : with a reference. You
must have a reference to form1 in form2 (passed in the constructor for
example) and call :

this.form1.Show()

Fabien
 
Vladimir O¾ura said:
I am building a Pocket PC application and I have 5 forms (form1, form2,
...). Form1 starts up and I click a button for Form2 to show up. Form1 is
hiden. On Form2 i click a button for Form3 to show up. Now Form2 is hiden.
Then in Form2 I click a button to show Form1. How can i just say "SHOW
Form1"? I did it by doing this:

Form1 form1 = new Form1();
form1.show();

But this creates a new Form1. Is there another way to do this?


Thanks!!!

Some forms only need one instance for the life of the app . The battery
status form is a good example. For example, say I have a battery picture
box on my first form. When some one clicks it, I show the battery status
form:

Initial form:
this.batteryPictureBox.Click += new
System.EventHandler(BatteryStatus.BatteryPictureBox_Click);


BatteryStatusForm:

public System.Windows.Forms.Form ParentForm;
public static BatteryStatus batteryStatusForm;

private void Close_Click(object sender, System.EventArgs e)
{
ParentForm.BringToFront();
this.Hide();
}

public static void BatteryPictureBox_Click(object sender, EventArgs e)
{
//if the form already exists then reuse it.
if(batteryStatusForm == null)
batteryStatusForm = new BatteryStatus();
//get the calling form from the control that was clicked so we know where
to return to
batteryStatusForm.ParentForm = (Form)((Control)sender).Parent;
batteryStatusForm.Show();
}

The above checks to see if the batteryStatusForm already exists. If not,
create it. This is a singleton pattern...always ensuring one instance of
the form exists and no more. The casting is done to get the parent form of
the picture box (battery status icon from the initial form). Now I have a
reference to who ever asked for the battery status form. On closing the
battery status form, I hide it and then bring the parent to the front. You
could have done show() if you'd like. The difference is that BringToFront()
is somewhat out of context here. It is used more with controls that have a
z order.

Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top