VB2022 VB2019 VB6 VB Sample Code About Us

Lesson 37 Creating Console App Part I


In Visual Basic 2022, you can build a console application besides Windows Form Applications. To start creating a console application, start Visual Studio 2022 and choose Visual Basic Language and select Console App (.NET Framework)in the Create New Project window, as shown in Figure 37.1 below:

Figure 37.1: Console Application

Retain the name as ConsoleApp or change it to the name of your choice.

Now, click on Console Application to bring up the code window, as shown in Figure 37.2 below:

 
Figure 37.2: The Code Window

The console code window comprises modules, where the main module is module 1. You an add other modules by clicking on Project on the menu bar and click Add Module, as shown in Figure 37.3 below:

figure37.3
Figure 37.3: Add Module

To start writing code for the console application, type your code in between Sub Main() and End Sub, as shown below:

Sub Main ( )
 Your code
End Sub

Example 37.1: Displaying a Message

The following program will display a message " Welcome to Visual Basic 2022".

The function to display a message box is MsgBox(). Enter the code as follows:

Module Module1

    Sub Main()
        Dim myText As String
        myText = InputBox("Enter Your Text")
        MsgBox(myText)
    End Sub

End Module
Running the program produces an InputBox as shown in Figure 37.4 below:

Figure 37.4 The output

Clicking on the OK button produces an output as shown in Figure 37.5:

Figure 37.4 The output

Example 37.2: A Looping Program

You can write a looping program using the Do Until....Loop structure, as shown below:

Sub Main()
Dim x As Single
Do Until x> 100
x = x + 5
Loop
MsgBox("The value of x is" & ""& x)
End Sub

The output is

Figure 37.6: The output


❮ Previous lesson Next lesson ❯


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy