whytheq
whytheq

Reputation: 35557

Show a WinForms from a Console application

I have an existing Console Application project.

I have added a Windows Form to the project called myForm

When the project runs it goes to the Console's Main method - in this method how do I activate/show myForm ?

I assume I need to import the library System.Windows.Forms so the top of my console code looks like the following:

Imports System.Windows.Forms
Module Module1

    Sub Main()

        myForm.    '<<<<not sure how to activate form
        ...

Upvotes: 0

Views: 14294

Answers (4)

loadingnow
loadingnow

Reputation: 607

Try this:

Sub Main()
    'Your code goes here...
    System.Windows.Forms.Application.Run(New myForm)
    'Your code goes here...
End Sub

Upvotes: 2

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11940

Call this function.

Application.Run(myForm)

It runs even from the console app.

From the documentation,

Begins running a standard application message loop on the current thread, and makes the specified form visible.

EDIT: Declare it like this.

Public Class MyForm
    Inherits Form

    ' Make the code here

End Class

Dim form As MyForm = New MyForm
Application.Run(form)

Upvotes: 1

whytheq
whytheq

Reputation: 35557

No need to import the forms library (I've tested) and the working code that I now have is:

My main problem was not declaring and creating an instance of the windows form.

Module Module1

    Sub Main()

        Dim xForm As myForm = New myForm 
        xForm.ShowDialog()

Upvotes: 1

SysDragon
SysDragon

Reputation: 9888

You have to add the reference System.Windows.Forms, and then show the form:

myForm.Show()

Or

myForm.ShowDialog()

myForm has to be a Form type. Maybe you need to instanciate you form first:

Dim myForm as new FormName

Upvotes: 3

Related Questions