Home » VB.NET » VB.NET Processing Before WinForm Display

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).

static void Main()
    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.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. _
            Application.Run(New Form1())
        End If
    End Sub

And that should do the trick for you.


Other post in VB.NET

Related Post

  • UnauthorizedAccessException writing to HKLMUnauthorizedAccessException writing to HKLM Q. I'm trying to write to HKLM on a Vista computer using the following code: Dim k As Microsoft.Win32.RegistryKey k = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software", […]
  • Windows Forms – Passing Parameters at RuntimeWindows Forms – Passing Parameters at Runtime I received the following question over the weekend: I've made a C# form application and I need to send a report name at runtime. How do I add an incoming parameter to the command line? […]
  • WinForms – Change The Active TabWinForms – Change The Active Tab This question came in last Friday: I'm trying to code a windows form in vb.net 2005. In my form I have 2 TabControls and a command button. The button is in the first TabControl, so what […]
  • Manually Adding Event Handlers in VB.NETManually Adding Event Handlers in VB.NET Typically when we write our code, the event handlers get wired up for us using the handles clause.  So we never have to worry about wiring up our event handlers manually. But what […]
  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.