Hiding a form and showing another when a button is clicked in a Windows Forms application

124,994

Solution 1

The While statement will not execute until after form1 is closed - as it is outside the main message loop.

Remove it and change the first bit of code to:

private void button1_Click_1(object sender, EventArgs e)  
{  
    if (richTextBox1.Text != null)  
    {  
        this.Visible=false;
        Form2 form2 = new Form2();
        form2.show();
    }  
    else MessageBox.Show("Insert Attributes First !");  

}

This is not the best way to achieve what you are looking to do though. Instead consider the Wizard design pattern.

Alternatively you could implement a custom ApplicationContext that handles the lifetime of both forms. An example to implement a splash screen is here, which should set you on the right path.

http://www.codeproject.com/KB/cs/applicationcontextsplash.aspx?display=Print

Solution 2

A) The main GUI thread will run endlessly on the call to Application.Run, so your while loop will never be reached

B) You would never want to have an endless loop like that (the while(true) loop) - it would simply freeze the thread. Not really sure what you're trying to achieve there.

I would create and show the "main" (initial) form in the Main method (as Visual Studio does for you by default). Then in your button handler, create the other form and show it as well as hiding the main form (not closing it). Then, ensure that the main form is shown again when that form is closed via an event. Example:

public partial class Form1 : Form
{
  public Form1()
  {
    InitializeComponent();
  }    

  private void button1_Click(object sender, EventArgs e)
  {      
    Form2 otherForm = new Form2();
    otherForm.FormClosed += new FormClosedEventHandler(otherForm_FormClosed);
    this.Hide();
    otherForm.Show();      
  }

  void otherForm_FormClosed(object sender, FormClosedEventArgs e)
  {
    this.Show();      
  }
}

Solution 3

private void button5_Click(object sender, EventArgs e)
{
    this.Visible = false;
    Form2 login = new Form2();
    login.ShowDialog();
}

Solution 4

Anything after Application.Run( ) will only be executed when the main form closes.

What you could do is handle the VisibleChanged event as follows:

static Form1 form1;
static Form2 form2;

static void Main()
{

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    form2 = new Form2();
    form1 = new Form1();
    form2.Hide();
    form1.VisibleChanged += OnForm1Changed;
    Application.Run(form1);

}

static void OnForm1Changed( object sender, EventArgs args )
{
    if ( !form1.Visible )
    {
        form2.Show( );
    }
}

Solution 5

To link to a form you need:

Form2 form2 = new Form2();
        form2.show();

this.hide();

then hide the previous form

Share:
124,994
Nataly
Author by

Nataly

Updated on July 09, 2022

Comments

  • Nataly
    Nataly almost 2 years

    I am doing an application a Windows Form application. At first, a certain form appears, and after the user hits the next button, this form should be hidden and another form is shown.

    I tried to do it. I managed to hide the current form, but the next one won't show.

    Here is my attempt:

    This is the button's event handler

    private void button1_Click_1(object sender, EventArgs e)
    {
        if (richTextBox1.Text != null)
        {
            this.Visible=false;
    
    
        }
        else
            MessageBox.Show("Insert Attributes First !");
    }
    

    This is the main function:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form2 form2 = new Form2();
        Form1 form1 = new Form1();
        form2.Hide();
        Application.Run(form1);
        while (true)
        {
            if (form1.Visible == false)
                form2.Show();
        }
    }
    
  • Nick
    Nick over 13 years
    I can see what they were trying to do though... basically run a loop checking for changes to an object - not very GUI oriented but perfectly valid in some situations.
  • Nataly
    Nataly over 13 years
    i tried that , but the question is where should i put the Application.run(form2) instruction , putting it inside form1 raises an exception...btw the infinite loop caused no exceptions i will try to come up with alternatives
  • Nataly
    Nataly over 13 years
    That's a very good solution to the problem , yet two error msgs were shown when i tried this , fomr1,form2 were not recognized inside the onForm1Changed method..weired huh?
  • Bolu
    Bolu over 13 years
    @Nataly, I bet that was an NullReferenceException :)
  • Nataly
    Nataly over 13 years
    yup it was , it turned out that form2.show() is sufficient no need for running the app, since form1 app would be still running , thx for your helpful answer
  • Bolu
    Bolu over 13 years
    @Nataly yes, as you have overseen the first line of my code. But anyway, glad you've solved your problem
  • Nick
    Nick over 13 years
    Ah yes of course, my mistake, they would have to be declared outside of the Main( ) method. I'll update the code. Sorry!
  • Nick
    Nick over 13 years
    Ok, I've updated the code, however it's probably not the greatest solution ever!!