Click here to Skip to main content
15,886,095 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Create Windows Form Application without Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.59/5 (10 votes)
13 Aug 2016CPOL1 min read 44.2K   7   10
A template for Windows Form Application written and compiled without Visual Studio.

Introduction

You may find yourself in a situation that asks for a simple application, but there is no suitable software on the system and you aren't allowed to install it. Luckily, a .NET framework with its compiler is always there for you. All you need is Notepad, compiler and little know-how from this article.

Background

An application compiled from command line is by default a command line app. To show a Form, you need to write a class and create an instance of if. By default, the command line window remains displayed. Although it may be useful for debugging purposes, for ordinary use, you'll likely want to hide it.

What's demonstrated in the template:

  • Definition of the Form's class and its call
  • Enabling or disabling of single instance feature
  • Hiding the command line window. By default, it remains show
  • Adding controls to the form and assigning their event procedures
  • A feature to help you with controls. You need to define the controls position and size numerically. This template helps you get the coordinates by clicking on the form of your running app.
  • Closing your application from the Form

Using the Code

Copy it to Notepad and save it as plain text (.txt extension).

VB.NET
Imports System.Drawing                
Imports System.Windows.Forms                
                
Namespace MyNamespace                
    Public Class MyFormClass            
    Inherits Form            
    private singleInstance as boolean = false            
    private hideCommandlineWindow as boolean = false                
                
' this will be used later for single instance mode                
    Private objMutex As System.Threading.Mutex            
                
' this will be used later to hide the command line window               
    Private Declare Auto Function GetConsoleWindow Lib "kernel32.dll" () As IntPtr            
    Private Declare Auto Function ShowWindow Lib "user32.dll" _
         (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean            
    Private Const     SW_HIDE    as Integer =     0
    Private Const     SW_SHOW as Integer =     5    
    private hWndConsole As Integer            
                
    Private XPreviousClick, YPreviousClick as integer            

' declare variables for your controls                
    Private tmr1 As Timer            
    Private btn1, btn2 As Button            
    Private lbl1 as Label            
    Private txb1 as textbox            
                
    Public Sub New()            
' single instance mode 
        if singleinstance then  'Prevent opening second instance if desired        
            objMutex = New System.Threading.Mutex(False, "Name of your program")    
            If objMutex.WaitOne(0, False) = False Then    
                objMutex.Close()
                objMutex = Nothing
                MessageBox.Show("Another instance is already running!")
                End
            End If    
        End if        

' hide the command line window               
        hWndConsole = GetConsoleWindow() 'get the handle of commandline window        
        if hideCommandlineWindow then         
            If hWndConsole <> IntPtr.Zero Then ShowWindow(hWndConsole, SW_HIDE)    
        End If        
                
        DisplayTheForm()        
    End Sub            
                
Private Sub DisplayTheForm()                
    Me.Name = "Windows Form" '            
    Me.Text = "Here's your windows header text."            
    Me.Size = New Size(706, 250)            
    Me.StartPosition = FormStartPosition.CenterScreen            
                
'create your controls and define their Event handlers              
    'timer 
    tmr1 = new timer()            
    AddHandler tmr1.Tick, AddressOf Tmr1Tick            
           
    'buttons     
    btn1 = New Button()            
    btn1.Name = "button"            
    btn1.Text = "Hide"            
    btn1.Location = New Point(10, 5)            
    btn1.Size = New Size(80, 35)            
    Me.Controls.Add(btn1)            
    AddHandler btn1.Click, AddressOf btn1Click        

    btn2 = New Button()            
    btn2.Name = "button"            
    btn2.Text = "Start timer1"            
    btn2.Location = New Point(300, 5)            
    btn2.Size = New Size(80, 35)            
    me.controls.add(btn2)            
    AddHandler btn2.Click, AddressOf btn2Click         
                
    lbl1  = new Label()            
    lbl1.text = "Left-click on left top corner of intended control to see its coordinates" & vbcrlf & "Right-click on right bottom corner of intended control to see it's size."            
    lbl1.Location = New Point(11, 45)            
    lbl1.Size = New Size(370, 37)            
    me.controls.add(lbl1 )            
  txb1  = New textbox()            
    txb1.text = " "            
    txb1.Location = New Point(95, 6)            
    txb1.Size = New Size(200, 25)            
    me.controls.add(txb1)            
                
    'an example of froms event handler            
    Addhandler me.mousedown ,Addressof MeMousedown            
End Sub                

' the actual event handler subroutines

'close the application when the form closes       
Private Sub FormcloseClick(source As Object, e As EventArgs)                  
    '  to close your application when the form closes            
    application.exit()            
    end            
End Sub                
                
Private Sub Tmr1Tick(source As Object, e As EventArgs)                
    tmr1.stop()            
    MessageBox.Show("Timer 1 has elapsed !")            
End Sub                

' Toggle visibility of command line window usigh previously stored window handle     
Private Sub btn1Click(source As Object, e As EventArgs)                
    if btn1.text = "Hide" then             
        ShowWindow(hWndConsole, SW_HIDE)        
        btn1.text = "Show"        
    else            
        ShowWindow(hWndConsole, SW_SHOW)        
        btn1.text = "Hide"        
    end if            
End Sub                
                
Private Sub btn2Click(source As Object, e As EventArgs)                
    tmr1.interval = 5000  ' 5 seconds            
    tmr1.start()            
End Sub                
                
Private Sub MeMousedown(source As Object, e As MouseEventArgs)                
    If (e.Button = MouseButtons.Left) then            
        lbl1 .text = "New Point(" & convert.tostring(e.x) & "," &  convert.tostring(e.y) & ")"        
        XPreviousClick = e.x         
        YPreviousClick = e.y        
    end if            
    If (e.Button = MouseButtons.Right) then            
        lbl1 .text = "New Size(" & convert.tostring(e.x - XPreviousClick ) & "," & convert.tostring(e.y - YPreviousClick ) & ")"        
    end if            
    txb1.text = lbl1.text            
End Sub                
                
        Public Shared Sub Main(args As [String]())        
            'Create an instance of the form and show it    
            Application.Run(New MyFormClass())    
        End Sub        
                
    End Class            
End Namespace 

Once the code is saved, you need to compile it. The hard way from command-line. To save the hassle, copy the code below to Notepad and save as text with .bat extension, e.g., compile.bat. The compilation becomes a matter of dragging and dropping your .txt file on the compile.bat file.

@rem remove the temp file should it remain in place from previous attempt 
@del %1_temp.vb

@rem  the compiler requires .vb extension, so we'll have to copy our source to a temporary file
copy %1 %1_temp.vb /y

@rem prepare string without the .txt extension
set str=%1
set str=%str:.txt=%

@rem the actual build. check/update the path to your compiler according it's location on your system
@rem if you imported more namespaces in you source filem don't forget to add corresponding libraries
@rem 
C:\Windows\Microsoft.NET\Framework\v2.0.50727\vbc.exe /platform:AnyCPU /r:Microsoft.VisualBasic.dll, system.management.dll,system.data.dll,mscorlib.dll,system.windows.forms.dll /target:exe %1_temp.vb /out:%str%.exe

@rem compiler will print out syntax errors it may have found in your code
@rem a pause is added to keep compiler messages on screen, useful for debugging

pause

@rem clean up the temporary file
del %1_temp.vb

Here you are, there's nothing to stop you from coding.

History

  • 13th August, 2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow is this used once compiled? Pin
Member 137059272-Mar-18 13:28
Member 137059272-Mar-18 13:28 
AnswerRe: how is this used once compiled? Pin
Zdenek Jagrik9-Mar-18 8:51
Zdenek Jagrik9-Mar-18 8:51 
Questioncompile error (vbc.exe is not recognized...) Pin
Member 137059272-Mar-18 12:41
Member 137059272-Mar-18 12:41 
AnswerRe: compile error (vbc.exe is not recognized...) Pin
Zdenek Jagrik9-Mar-18 8:48
Zdenek Jagrik9-Mar-18 8:48 
QuestionError Compiling Pin
Eduardo De Leo21-Feb-17 3:36
Eduardo De Leo21-Feb-17 3:36 
AnswerRe: Error Compiling Pin
Zdenek Jagrik28-Feb-17 10:55
Zdenek Jagrik28-Feb-17 10:55 
QuestionCompilation Fails! Pin
Member 1275630023-Sep-16 6:46
Member 1275630023-Sep-16 6:46 
AnswerRe: Compilation Fails! Pin
Zdenek Jagrik5-Oct-16 8:04
Zdenek Jagrik5-Oct-16 8:04 
QuestionTemp file to compile?? Pin
Thornik15-Aug-16 8:06
Thornik15-Aug-16 8:06 
Any explanation why we need it?

AnswerRe: Temp file to compile?? Pin
Zdenek Jagrik15-Aug-16 9:10
Zdenek Jagrik15-Aug-16 9:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.