Hiding a form and showing another when you click a button in a Windows Forms application

I am using a Windows Form application. First, a specific form appears, and after the user clicks the next button, this form should be hidden and another form shown.

I tried to do it. I managed to hide the current form, but the next one will not be displayed.

Here is my attempt:

This is a button 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();
    }
}
+3
source share
6 answers

The While statement will not be executed until Form1 is closed - because it is outside the main message loop.

Delete 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 !");  

}

, . Wizard.

ApplicationContext, . , .

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

+3

, Application.Run() .

, VisibleChanged :

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( );
    }
}
+1

A) GUI Application.Run, while

B) ( while (true)) - . , .

"" () Main ( Visual Studio). , ( ). , , . :

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();      
  }
}
+1
private void button5_Click(object sender, EventArgs e)
{
    this.Visible = false;
    Form2 login = new Form2();
    login.ShowDialog();
}
+1

:

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

this.hide();

+1

, form1

 while (true)
    {
        if (form1.Visible == false)
            form2.Show();
    }

form2 form1?

Form2 form2 = new Form2();
 private void button1_Click_1(object sender, EventArgs e)
    {
        if (richTextBox1.Text != null)
        {
            form1.Visible=false;
            form2.Show();

        }
        else MessageBox.Show("Insert Attributes First !");

    }
0

Source: https://habr.com/ru/post/1778304/


All Articles