i learned earlier how to move between two forms back and forth. but what if there are more forms? this is my code for form1:

Form2 form2 = new Form2();
    private void aboutoldtrafford_MouseClick(object sender, MouseEventArgs e)
    {
        this.Hide();
        form2.ShowDialog();
        this.Show();
    }

when i click the button I can go to form2 and there's two button there: back and next

private void backbutton_MouseClick(object sender, MouseEventArgs e)
    {
        this.DialogResult = DialogResult.OK;
    }

    Form3 form3 = new Form3();
    private void nextbutton_MouseClick(object sender, MouseEventArgs e)
    {
            this.Hide();
            form3.ShowDialog();
            this.Show();

    }

back button will return to form1 and the next button will go to form3. below is my code for form3. in form3, there are two buttons: back and finish

private void back_MouseClick(object sender, MouseEventArgs e)
    {
        this.DialogResult = DialogResult.OK;
    }

    private void finish_MouseClick(object sender, MouseEventArgs e)
    {
        this.Hide();
        // i want to go back to form1

    }

back button will return to form2 and the finish button will go back to form1. obviously, i cant do "this.DialogResult = DialogResult.OK;" in the finish button. how can i go back to form1 without going to form2? please help...

please try

private void finish_MouseClick(object sender, MouseEventArgs e)
    {
       Form1 form1 = new Form1();

            form1.Show();
            this.Hide();

If you use different DialogResult values depending on what was clicked you can pass that back/forth between your forms. For example instead of using DialogResult.OK when you click the back button, use DialogResult.Cancel .

So you'd end up with something like this:

// in Form3
private void finishButton_MouseClick(object sender, MouseEventArgs e)
{
    this.DialogResult = DialogResult.OK;
}

private void backButton_MouseClick(object sender, MouseEventArgs e)
{
    this.DialogResult = DialogResult.Cancel;
}
// in Form2
private void backbutton_MouseClick(object sender, MouseEventArgs e)
{
    this.DialogResult = DialogResult.Cancel;
}
 
private void nextbutton_MouseClick(object sender, MouseEventArgs e)
{
    Form3 form3 = new Form3();
    this.Hide();
    form3.ShowDialog();
    if (form3.DialogResult == DialogResult.Cancel)
    {
        //this means that the Back button was pressed in Form 3, so we want to:
        this.Show();
    }
    else if (form3.DialogResult == DialogResult.OK)
    {
        //this means the Finish button was pressed in Form 3, so we want to:
        this.DialogResult = DialogResult.OK;
        //which will kick back up to Form1. Remember, this (form2) is still hidden, so it won't appear
    }
}
// in Form1
private void aboutoldtrafford_MouseClick(object sender, MouseEventArgs e)
{
    Form2 form2 = new Form2();
    this.Hide();
    form2.ShowDialog();
    if (form2.DialogResult == DialogResult.Cancel)
    {
        //Form2's back button was pressed.
    }
    else if (form2.DialogResult == DialogResult.OK)
    {
        //Only way to get here is if Form3's Finish button was pressed.
    }
    this.Show();
}

Suggestion:
Create an Admin class containing the instantiation of the forms, including the current state;
then you end up with something like this

class FormAdmin
{
   Form currentForm {get; set;}
   
   Form Form1 = new Form();
   Form Form2 = new Form();
   Form Form3 = new Form();


   function changeVisibleForm( int ID )
   {
      switch (ID)
      {
         case 1:
            currentForm.Hide();
            currentForm = Form1;
        
         case 2:
            currentForm.Hide();
            currentForm = Form2;

         case 3:
            currentForm.Hide();
            currentForm = Form3;
        }
      currentForm.Show();
   }
}

All you have to do in you buttons click event is to define the form to show...
And in my opinion it is scalable, but should only be used for a small number of concurrent existing forms.

Hope it answers your question

Edited 5 Years Ago by Bridgekeepers: n/a

This article has been dead for over six months. Start a new discussion instead.