creating a sub main in a windows forms project

olsonpm

Well-known member
Joined
Aug 24, 2010
Messages
46
Programming Experience
Beginner
What is the vb.net equivalent to accomplishing this?

VB.NET:
static void Main()
    {
        frmCalcView view = new frmCalcView();
        CalculatorModel model = new CalculatorModel();
        CalcController controller = new CalcController(model, view);
        [B]view.addController(controller);[/B]
        Application.Run(view);
    }

Basically I want to be able to add a controller to my view. The problem is that vb.net forces me to use a form as a startup project. I know there is a different way I'm supposed to accomplish this, but I'm very used to java where I control what happens at startup. I looked into the My.Application.Startup event as seen in MSDN, but that doesn't accomplish what I need because the view isn't created at that point. What am I missing here?

thanks,
Phil
 
Last edited:
VB.NET:
Public Sub Main()
    Dim view As New frmCalcView()
    Dim model As New CalculatorModel()
    Dim controller As New CalcController(model, view)
    view.addController(controller)
    Application.Run(view)
End Sub
You could compact the above down to 1 or 2 lines of code if you wanted to
 
let me rephrase the question. My problem is that I don't know how to add a sub main which runs the form - as opposed to vb.net automatically running my form. Say my form is form1.vb. This is my startup file when i run the program. In java, I could do everything I need from a seperate class "myMain", which I set as the startup file. This file will have the "public static void main..." method that lets me control what's going on during initialization/setup. I don't know how to do this in vb.net. Currently my form runs, creates the view behind the scenes, and then doesn't give me a chance to add the controller to to the view manually. I just want a different class to control the logic of the setup, or a correct way to accomplish the same task in vb.net.
 
Ah, ok. Add a module to the project and add Public Sub Main in it , then in the project's properties uncheck the 'Enable Application Framework', then in the Startup Object dropdown, select 'Sub Main'
 
ha, Sorry for the initial confusion. That sounds like it will definitely solve my issue, although I'm not able to test it atm. Thanks for your help.
 
You don't actually need to create your own Main method. When you have the Application Framework enabled, the IDE generates this method:
VB.NET:
<Global.System.Diagnostics.DebuggerStepThroughAttribute()>  _
Protected Overrides Sub OnCreateMainForm()
    Me.MainForm = Global.WindowsApplication1.Form1
End Sub
where Form1 is your startup form. Now, you can't change that method and you can't stop it running, but the important thing to note here is that the main form is the default instance of its type. There's nothing to stop you accessing the default instance of that type earlier than the OnCreateMainForm method, thereby creating that instance earlier.

So, you should leave the Application Framework enabled, make frmCalcView your Startup Form, click the View Application Events button in the project properties and then override the OnRun method like so:
VB.NET:
Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Protected Overrides Sub OnRun()
            frmCalcView.addController(New CalcController(New CalculatorModel, frmCalcView))

            MyBase.OnRun()
        End Sub

    End Class

End Namespace
That's using the default instance of frmCalcView, which is the same instance that will be assigned to the MainForm property in the OnCreateMainForm method.
 
Back
Top