Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/.NET - WinForms - Instantiate a Form without showing it

Tags:

I am changing the Visibility of a Form to false during the load event AND the form still shows itself. What is the right event to tie this.Visible = false; to? I'd like to instantiate the Form1 without showing it.

using System; using System.Windows.Forms;  namespace TestClient {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }          private void Form1_Load(object sender, EventArgs e)         {             this.Visible = false;         }      } } 
like image 868
BuddyJoe Avatar asked Apr 30 '09 13:04

BuddyJoe


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

Regardless of how much you try to set the Visible property before the form has been shown, it will pop up. As I understand it, this is because it is the MainForm of the current ApplicationContext. One way to have the form automatically load, but not show at application startup is to alter the Main method. By default, it looks something like this (.NET 2.0 VS2005):

[STAThread] static void Main() {     Application.EnableVisualStyles();     Application.SetCompatibleTextRenderingDefault(false);     Application.Run(new Form1()); } 

If you instead do something like this, the application will start, load your form and run, but the form will not show:

[STAThread] static void Main() {     Application.EnableVisualStyles();     Application.SetCompatibleTextRenderingDefault(false);     Form1 f = new Form1();     Application.Run();          } 

I am not entirely sure how this is useful, but I hope you know that ;o)

Update: it seems that you do not need to set the Visible property to false, or supply an ApplicationContext instance (that will be automatically created for you "under the hood"). Shortened the code accordingly.

like image 77
Fredrik Mörk Avatar answered Oct 03 '22 22:10

Fredrik Mörk


I know this is an old question, but I just stumbled upon it and am pretty surprised no one has mentioned SetVisibleCore:

bool isVisibleCore = false; protected override void SetVisibleCore(bool value) {     base.SetVisibleCore(isVisibleCore); } 

In that code snippet, as long as isVisibleCore remains false, the form will remain invisible. If it's set to false when the form is instantiated, you won't get that brief flash of visibility that you'd get if you set Visible = false in the Shown event.

like image 41
Nick Spreitzer Avatar answered Oct 04 '22 00:10

Nick Spreitzer