How can I hide form1 while running Application.Run (form1)?


I have a form1 that I run with Application.Run.

I want to hide this form (I need it to hide because I run some things in the background, so they have to run) and open another form for login.

How am I trying to do this by running the command in my form1 constructor this.Hide();, and if the login is successful, show my form1, but it does not work. Any ideas?

+3
source share
6 answers

Just override the OnVisibleChanged method and change the visibility of the form there, something like this:

protected override void OnVisibleChanged(EventArgs e)
{
    base.OnVisibleChanged(e);
    this.Visible = false;
}

And this! Simple and clean.

+6

: ApplicationContext

, , , , . ( - , )

: , . , ApplicationContext. , , WinForm .

+2

SetVisibleCore Form1, Form1. , , , , , , .

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

  protected override void SetVisibleCore(bool value) 
  {       
    // Quick and dirty to keep the main window invisible
    // you can put some logic here to decide when to use the
    // incomming value and when to force it to be false as I 
    // am showing here.       
    base.SetVisibleCore(false); 
  } 
} 

, . Form1 Form2, Form2 , Form1 .

using System;
using System.Windows.Forms;

namespace HideMainWinForm
{
  public partial class Form1 : Form
  {
    // Initially the main form cannot show
    private bool _canShow = false; 

    public Form1()
    {
      InitializeComponent();

      Form2 frm = new Form2();
      frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
      frm.Show();      
    }

    void frm_FormClosed(object sender, FormClosedEventArgs e)
    {
      // Once Form2 is closed we now allow the main form to
      // become visible.
      _canShow = true;
      this.Show();
    }

    protected override void SetVisibleCore(bool value)
    {
      base.SetVisibleCore(_canShow && value);
    }
  }
}
+2

- , , .

, , Application.Run .

, , , Application.Run, "" , , . , Application.Exit Application.ExitThread , ( , ).

, :

void Main()
{
    // trigger some background work
    ...

    // and start the message pump
    Application.Run();
}


void SomeBackgroundWork()
{
    // let say now you completed the background work and you want to show your main Form
    MyForm f = new MyForm();
    f.Close += delegate { Application.Exit(); };
    f.Show();
}

, - ...

0

:

this.Hide() , this.Visible = false, . . ( Application.Run) form.Show this.Visible = true (, , Hide...).

:

private bool firstShow = true;

protected override void OnShown(EventArgs e)
{
    if (firstShow)
    {
        this.Hide();
        firstShow = false;
        return;
    }
    base.OnShown(e);
}

, .

firstShow Show(), .

0

Windows Forms , Form.Shown. Form, . digEmAll , .

Add an event handler to the constructor that is called Form1.Shown, then in a hanlder calledHide()

public form1()
{
    InitializeComponent()
    this.Shown += new EventHandler(form1_Shown);
    // call splash page
}

void form1_Shown(object sender, EventArgs e)
{
    this.Hide()
}
0
source

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


All Articles