VB.NET Processing Before WinForm Display
I woke up this morning to an interesting question.
“Using VB.net 2008, I want my project to be a Windows Forms Application, but upon startup, I want to check a few files to see if they exist and if they don’t I do not want the startup form to load. I just want the program to quit. If you have to start this type of application with a form, how do you keep the form from displaying?”
If you program in CSharp, you probably already know the answer to this question, or at least you should. If you don’t, you will when we finish here. So since I consider this a VB.NET-specific question, I’m going to answer it using VB.NET syntax.
When CSharp runs a Windows Forms application, it writes out the following code in Program.cs (in VS 2008, earlier versions put this in the main form).
[STAThread] static void Main() { Application.EnableVisualStyles(); Application. SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
In VB.NET there is no code that looks like this, because VB.NET writes the code for us behind the scenes.
So to do what you want to do, we need to take over control of the Windows Form application.
Since I’m assuming that you already have the Windows Form application created, the next thing you’ll want to do is to create a module. You can name it what ever you want, but I’m going to name mine “Main” for purposes of this article.
In your module, create a function called “main” that has the code CSharp would have given us.
Public Sub main() Application.EnableVisualStyles() Application.SetCompatibleTextRenderingDefault(False) Application.Run(New Form1()) End Sub
Now go to your project properties and go to the Application tab.
Find the check box that says, “Enable Application Framework” and un-check it.
Then change the startup object to “Sub Main".”
At this point, your application should run as it always has. To put the checks in that you requested, write that code prior to all the Application… statements that we put in sub main and put an if/then/end if statement around the Application… statements.
Public Sub main() Dim ChecksWereOk As Boolean = False ' your checks here If ChecksWereOk Then Application.EnableVisualStyles() Application. _ SetCompatibleTextRenderingDefault(False) Application.Run(New Form1()) End If End Sub
And that should do the trick for you.
Other post in Advanced VB.NET
- VB.NET - Char from String with Option Strict - April 8th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- VB.NET Hide Module Name - June 22nd, 2009
- Manually Adding Event Handlers in VB.NET - July 15th, 2009
- VB.NET Nullable Value Types - July 22nd, 2009
- VB.NET Processing Before WinForm Display - August 6th, 2009
Other Related Items:
Juveker Keratin Hair Building Fibers - Color Light Blond; 28 Grams (Net Wt .98oz)The solution to instantly conceal bald spots and thicken fine hair is finally here-and it's called Juveker Hair Building Fibers. Juveker is not intend... Read More >
Poly-Klear® Right Window Insurance Form Envelopes, #10-1/2, 4-1/2x9-1/2, 500/BoxPoly-Klear Right Window Insurance Form Envelopes, #10-1/2, 4-1/2x9-1/2, 500/BoxInsurance Form Envelopes 24-lb. white wove paper with Poly-Klear® r... Read More >
Professional Visual Basic 2008 (Programmer to Programmer)The 2008 version of Visual Basic is tremendously enhanced and introduces dramatic new concepts, techniques, and features to this popular object-orient... Read More >









