Single Form Hide on Startup

Single Form Hide on Startup

I'm coming at this from C#, but should be very similar in vb.net.

In your main program file, in the Main method, you will have something like:

Application.Run(new MainForm());

This creates a new main form and limits the lifetime of the application to the lifetime of the main form.

However, if you remove the parameter to Application.Run(), then the application will be started with no form shown and you will be free to show and hide forms as much as you like.

Rather than hiding the form in the Load method, initialize the form before calling Application.Run(). I'm assuming the form will have a NotifyIcon on it to display an icon in the task bar - this can be displayed even if the form itself is not yet visible. Calling Form.Show() or Form.Hide() from handlers of NotifyIcon events will show and hide the form respectively.

Hiding forms on startup: why doesn't this.Hide() hide my form?

you can use this line of code. It wont hide it, but it will be minimized:

this.WindowState = FormWindowState.Minimized;

in addition, if you don't want it showing on the task bar either, you can add this line:

this.ShowInTaskbar = false;

But why do you create the form if you don't want it to be visible in the first place?

What is the best way to hide the main form on application's startup

The simplest way is to set Opacity = 0 in the designer. Of course you will want to set it back to 100 at some point later..

Or you may want to use a splash screen, maybe like this:

static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Splash splash = new Splash();
splash.Show();
Application.Run();
}
}

With a splash screen:

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

Form1 form1 = new Form1();

private void Splash_Load(object sender, EventArgs e)
{

form1.WindowState = FormWindowState.Minimized;
form1.Hide();
}

}

You can then show it for example when the splash screen is closed:

private void Splash_FormClosed(object sender, FormClosedEventArgs e)
{
form1.Show();
form1.WindowState = FormWindowState.Normal;
}

Which would happen whenever you want or maybe after some time:

public Splash()
{
InitializeComponent();
Timer timer = new Timer();
timer.Interval = 5000;
timer.Enabled = true;
timer.Tick += (s,e) =>{ this.Close();};
}

Since the program is not watching a Form to close we also need to add this to the main form's closed event:

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}

If you don't want the splash screen to be visible at all you can hide it like this:

public Splash()
{
InitializeComponent();
this.Opacity = 0;

But please make sure you don't leave the users in the blind: When I start a program I want immediate response!!

Hide form at launch

// In Your Program.cs Convert This
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

// To This
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 TheForm = new Form1();
Application.Run();
}

// Call Application.Exit() From Anywhere To Stop Application.Run() Message Pump and Exit Application

Make the startup form visible again after hiding it

Do this:

private void button1_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
this.Hide();
x.ShowModal();
this.Show();
}

How to make the startup form initially invisible or hidden

I'm guessing that what you're asking is how to make the form not appear in the task bar and only appear in the system tray, just like an IM or an anti virus?

If so, just set the ShowInTaskbar property of the Form to false.

As for making the initial form invisible, you'll have to use an ApplicationContext within Application.Run instead of the main form.

How to have an invisible start up form?

Paste this in your form code:

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
If Not Me.IsHandleCreated Then
Me.CreateHandle()
value = False
End If
MyBase.SetVisibleCore(value)
End Sub

The way that works is that the very first request to show the form, done by the Application class, this code overrides the Visible property back to False. The form will behave as normal after this, you can call Show() to make it visible and Close() to close it, even when it was never visible. Note that the Load event doesn't fire until you show it so be sure to move any code in your event handler for it, if any, to the constructor or this override.

How to hide WinForm after it run?

In the form Load override you can use one of the following tricks:

  1. Make the form completely transparent:

    private void OnFormLoad(object sender, EventArgs e)
    {
    Form form = (Form)sender;
    form.ShowInTaskbar = false;
    form.Opacity = 0;
    }
  2. Move the form way off the screen:

    private void OnFormLoad(object sender, EventArgs e)
    {
    Form form = (Form)sender;
    form.ShowInTaskbar = false;
    form.Location = new Point(-10000, -10000);
    }

How to hide or close startup loginform after main form is open?

use this

  static void Main()

{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmLog ());

}

inside frmlog

use

this.hide();
new frmMain().ShowDialog();


Related Topics



Leave a reply



Submit