3.2 Writing the Code

 

3.2 Writing the Code

To write code, click any part of the form to enter the code window, as shown in Figure 3.1. The event procedure is to load Form1 which starts with the keywords Private Sub and ends with End Sub. This procedure includes the Form1 class and the event Load, and they are bound together with an underscore, i.e., Form_Load. It does nothing other than loading an empty form. To make the load event does something, insert the following statement.

MsgBox ( "Welcome to Visual Basic 2022")






Example 3.1 Displaying a Message

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) HandlesMyBase.Load

MsgBox ( "My First Visual Basic 2022 APP", ,"My Message")


End Sub
End Class

 

MsgBox is a built-in function that displays a message in a pop-up message box. The MsgBox function comprises a few arguments, the first is the message that is displayed and the third one is the title of the message box. When you run the program, a message box displaying the phrase "My First Visual Basic 2022 APP" will appear, as shown in Figure 3.3.

Figure 3.3

You will notice that above the Private Sub structure there is a preceding keyword Public Class Form1. This has to do with the concept of the object-oriented programming language. When we start a windows application in Visual Basic 2022, we will see a default form with the name Form1 appear in the IDE, it is the Form1 Class that inherits from the Form class System.Windows.Forms.Form

Now, let us write a code that perform arithmetic calculation, as in Example 3.2

Example 3.3 Arithmetic Calculations

Private Sub Form1_Load(sender As Objecte As EventArgs) Handles MyBase.Load


MsgBox("2" & "+" & "5" & "=" & 2 + 5)


End Sub

*The symbol & (ampersand) is to perform string concatenation. The output is as shown in Figure 3.4

Figure 3.4

Summary

     In section 3.1, you have learned the concepts of event driven programming.

     In section 3.2, you have learned how to write code for the controls.

Post a Comment

0 Comments