NetInfo NetInfo - 1 month ago 15
Vb.net Question

Writing VB.NET Windows Forms Application using CodeDom

How can you write a VB.NET Windows Forms Application via CodeDom?

I have tried everything, the closest i got to it is the code below, which first of all shows command prompt window which is not good, and then shows the form for like a second and everything disappears.
Is there another proper way to do it? An example is greatly appreciated.

Public Module MyApp
Public Sub main()
Dim NewForm As New System.Windows.Forms.Form
NewForm.Name = "Form1"
NewForm.Text = "Form1"
NewForm.Width = 300
NewForm.Height = 300
NewForm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
NewForm.ControlBox = True
NewForm.MaximizeBox = False
NewForm.MinimizeBox = True
NewForm.Show()
End Sub
End Module

Answer

After alot of research online, i have came to the following conclusion which seems to be working just fine. Thanks to everybody for any input on this subject.

First of all open a new project add the following code to a button. This code compiles the code that you write in the text file that you will create in the next step.

  Private Sub CompilerButton_Click(sender As System.Object, e As System.EventArgs) Handles CompilerButton.Click
        Dim objCodeCompiler As System.CodeDom.Compiler.ICodeCompiler = New VBCodeProvider().CreateCompiler() ' We create object of the compiler

        Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters()
        ' Add reference
        objCompilerParameters.ReferencedAssemblies.Add("System.dll")
        objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
        objCompilerParameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")

        'Compile in memory
        Dim Output1 As String = "OutputApp"
        objCompilerParameters.GenerateExecutable = True
        objCompilerParameters.OutputAssembly = Output1
        objCompilerParameters.CompilerOptions = "/target:winexe"

        Dim strCode As String = My.Resources.TextFile1.ToString
        Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = _
        objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode)

        If objCompileResults.Errors.HasErrors Then
            ' If an error occurs
            MsgBox("Error: Line>" & objCompileResults.Errors(0).Line.ToString & ", " & _
            objCompileResults.Errors(0).ErrorText)
            Exit Sub
        End If

    End Sub

And then in the projects resources add a text file and add the following code to it. This code is your application that you want to compile to a standalone EXE. And you can change it to the way you want.

Option Strict On
Imports System
Imports System.Windows.Forms
Imports System.Windows.Forms.Form
Imports Microsoft.VisualBasic

Namespace MyApp
    Public Class EntryPoint
        Public Shared Sub Main(args As [String]())
            Dim FrmMain As New Form1
            System.Windows.Forms.Application.Run(FrmMain)
        End Sub
    End Class
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private WithEvents Button1 As New Button
        Sub New()
            Application.EnableVisualStyles()
            Me.Text = "Form1"
            Button1.Text = "Click Me!"
            Button1.Top = 100
            Button1.Left = 100
            Me.Controls.Add(Button1)
        End Sub
        Private Sub Button1_Click(Sender As Object, E As EventArgs) Handles Button1.Click
            MsgBox("You Clicked Me!")
        End Sub
    End Class
End Namespace

If you have done everything above, after you click compile it should create a standalone EXE in the projects \bin\Debug under the name OutputApp.

Again thanks to everybody. Hope the code above is useful to anyone who is trying to do the same thing.