Transcription

Visual Basic 2019Made EasyBy Dr.Liew

DisclaimerVisual Basic 2019 Made Easy is an independent publication and is not affiliated with,nor has it been authorized, sponsored, or otherwise approved by MicrosoftCorporation.TrademarksMicrosoft, Visual Basic, Excel and Windows are either registered trademarks ortrademarks of Microsoft Corporation in the United States and/or other countries.All other trademarks belong to their respective owners.LiabilityThe purpose of this book is to provide basic guides for people interested in VisualBasic 2019 programming. Although every effort and care has been taken to makeThe information as accurate as possible, the author shall not be liable for any error,Harm or damage arising from using the instructions given in this book.Copyright 2019 Liew Voon Kiong

All rights reserved. No Part of this e-book may be reproduced in any form or by anymeans, without permission in writing from the author.

AcknowledgementI would like to express my sincere gratitude to the many people who have made theircontributions in one way or another to the successful publication of this book.My special thanks go to my children Xiang, Yi and Xun who have contributed theirideas and help in editing this book. I would also like to appreciate the supportprovided by my beloved wife Kim Huang and my youngest daughter Yuan. I wouldalso like to thank the millions of readers who have visited my Visual Basic Tutorialwebsite at vbtutor.net for their support and encouragement.About the AuthorDr. Liew Voon Kiong holds a bachelor’s degree in Mathematics, a master’s degree inManagement and a doctorate in Business Administration. He has been involved inVisual Basic programming for more than 20 years. He created the popular onlineVisual Basic Tutorial at www.vbtutor.net , which has attracted millions of visitorssince 1996. It has consistently been one of the highest ranked Visual Basic websites.To provide more support for Visual Basic students, teachers, and hobbyists, Dr. Liewhas written this book to complement the free Visual Basic 2019 tutorial with muchmore content. He is also the author of the Visual Basic Made Easy series, whichincludes Visual Basic 6 Made Easy, Visual Basic 2008 Made Easy, Visual Basic2010 Made Easy, Visual Basic 2013 Made Easy, Visual Basic 2015 Made Easy,Visual Basic 2017 Made Easy and Excel VBA Made Easy. Dr. Liew’s books havebeen used in high school and university computer science courses all over the world.

Table of ContentsChapter 1 Introduction141.1 A Brief History of Visual Basic141.2 Installation of Visual Studio 2019141.3 Creating a Visual Basic 2019 Project17Chapter 2 Designing the User Interface252.1 Customizing the Form25Example 2.1 Changing Properties at Runtime28Example 2.2 Customizing the Form292.2 Adding Controls to the FormChapter 3 Writing the Code30353.1 The Concept of Event-Driven Programming353.2 Writing the Code36Example 3.1 Displaying a Message37Example 3.3 Arithmetic Calculations38Chapter 4 Working with Controls4.1 TextBoxExample 4.1 Adding two numbers4.2 LabelExample 4.2 Display output on a Label4.3 ListBox3939394041424.3.1 Adding Items to ListBox42Example 4.3 Adding an Item to ListBox43Example 4.4 Adding Items using InputBox44Example 4.5 Geometric Progression45

4.3.2 Removing Items from a List Box47Example 4.6 Removing Items from ListBox47Example 4.6 Removing an Item from ListBox via an Input Box48Example 4.7 Delete a Selected Item from ListBox49Example 4.8 Delete Multiple Selected Items from ListBox51Example 4.9 Clear All Items from ListBox514.4 ComboBox524.4.1 Adding Items to ComboBox52Example 4.10 Adding an Item to ComboBox54Example 4.11 Adding an Item to ComboBox via an Input Box544.4.2 Removing Items from a Combo box55Example 4.12 Delete an Item from ComboBox56Example 4.13 Delete a Selected Item from ComboBox56Example 4.14 Remove All Items from ComboBox56Chapter 5 Handling Images5.1 Loading an Image in a Picture Box57575.1.1 Loading an Image at Design Time575.1.2 Loading an Image at Runtime60Example 5.1 Loading an Image at Runtime605.2 Loading an Image using Open File Dialog Control60Example 5.2 Loading an Image via a Dialog Box61Chapter 6 Working with Data6.1 Visual Basic 2019 Data Types64646.1.1 Numeric Data Types646.1.2 Non-numeric Data Types656.1.3 Suffixes for Literals66

6.2 Variables and Constants676.2.1 Variable Names676.2.2 Declaring Variables68Example 6.1 Declaring Variables68Example 6.2 Declaring a String Variable69Example 6.3 Declaring a Fixed Length String Variable706.2.3 Assigning Values to Variables70Example 6.4 Assigning Values to Variables70Example 6.5 Error Assigning a Variable716.2.4 Scope of Declaration716.2.5 Declaring Constants72Example 6.6 Declaring Constants72Example 6.6 Computing the Area of a Circle72Chapter 7 Arrays747.1 Introduction to Arrays747.2 Dimension of an Array747.3 Declaring Arrays75Example 7.1 To Find the Length of an Array76Example 7.2 Find the length of Non-zeroth Array76Example 7.3 Declaring Two-Dimensional Array77Example 7.4 Creating an Array via an Input Box77Chapter 8 Mathematical Operations808.1 Mathematical Operators808.2 Writing Code that Performs Mathematical Operations80Example 8.1 Standard Arithmetic Calculations81Example 8.1 Performing Arithmetic Operations81

Example 8.2 Pythagorean Theorem81Example 8.3: BMI Calculator82Chapter 9 String Manipulation9.1 String Manipulation Using and & signs8484Example 9.1 Concatenation of Strings84Example 9.2 Data Mismatch859.2 String Manipulation Using Built-in Functions879.2.1 The Len Function87Example 9.3 Using the Len Function to find the Lenth of a Phrase879.2.2 The Right Function88Example 9.4 Extracting the Right Portion of a Phrase889.2.3 The Left Function889.2.4 The Mid Function89Example 9.5 Using the Mid Function to Extract a Part of a Phrase89Example 9.6 Extracting a Part of a Phrase899.2.5 Trim Function90Example 9.7 Trimming a Phrase919.2.6 Ltrim Function919.2.7 The Rtrim Function919.2.8 The InStr function919.2.9 The Ucase and the Lcase Functions929.2.10 The Chr and the Asc functions92Example 9.8 Boggle94Chapter 10 Using If Then Else9710.1 Conditional Operators9710.2 Logical Operators98

10.3 Using If .Then.Else9810.3.1 If .Then Statement98Example 10.1 Using If Then Statement9910.3.2 If.Then Else Statement99Example 10.2 Lucky Draw Simulation100Example 10.3 Another Lucky Draw Program10210.3.3 If.Then.ElseIf Statement105Example 10.4 Grade Generator105Chapter 11 Select Case108Example 11.1: Examination Grades108Example 11.2 Using Case Is110Example 11.3 Select Case for a Range of Values111Example 11.4 Computing Exam Grades112Chapter 12 Looping11412.1 For.Next Loop114Example 12.1 Adding Numbers to a List Box114Example 12.2 Summation of Numbers using a For Next Loop115Example 12.3 Negative Increment in a For Next Loop116Example 12.4 Exit For11712.2 Do Loop118Example 12.5 Adding Numbers in a Do Loop119Example 12.6 Adding Numbers in a Do Loop12012.3 While.End While LoopExample 12.7 Summation of Numbers using a While End While LoopChapter 13 Sub ProceduresExample 13.1 A Sub Procedure that Add Two Numbers121121123123

Example 13.2: Password CrackerChapter 14 Functions14.1 Creating a User-Defined Function124127127Example 14.1: BMI Calculator127Example 14.2: Future Value Calculator12814.2 Passing Arguments by Value and by ReferenceExample 14.3 Square Root FunctionChapter 15 Mathematical Functions15.1 The Abs FunctionExample 15.1 Computing Absolute Value15.2 The Exp functionExample 15.2 Compute Exponential Value15.3 The Fix FunctionExample 15.3 Using Fix13013113413413413513513613615.4 The Int Function13715.5 The Log Function137Example 15.4 The Log Function15.6 The Rnd( ) FunctionExample 15.5 Generating Random Integers15.7 The Round Function137138138139Example 15.6 Rounding Numbers139Chapter 16 The Format Function14116.1 Format Function for Numbers14116.1.1 Built-in Format function for Numbers141Example 16.1 Formatting Numbers14216.1.2 User-Defined Format143

Example 16.2 Using User-Defined Formats16.2 Formatting Date and Time14414516.2.1 Formatting Date and time using predefined formats145Example 16.3 Formatting Date and Time14516.2.2 Formatting Date and time using user-defined formats146Example 16.4 Formatting Date and Time with User-Defined Format147Chapter 17 Checkbox and Radio Button17.1 Checkbox149149Example 17.1: Online Shopping Mall149Example 17.2 Shopping Cart151Example 17.3 Formatting Fonts15217.2 Radio Button154Example 17.4 Shopping Cart with Radio Buttons154Example 17.5 Multiple Selections Using Groupbox156Chapter 18 Errors Handling15918.1 Using On Error GoTo Syntax159Example 18.1 Division Errors16018.2 Using Try Catch .End Try StructureExample 18.1 Data Types Mismatch ErrorsChapter19 Object-Oriented Programming19.1 Concepts of Object-Oriented Programming161162164164(a) Encapsulation164(b) Inheritance164(c) Polymorphism16419.2 Creating a Class165Example 19.1 BMI Class166

Example 19.2 Examination Class169Example 19.3 Future Value Class171Chapter 20 Creating Graphics17320.1 Introduction to Graphics Creation17320.2 Creating the Graphics Object17320.3 Creating the Pen Object17420.4 Drawing a Straight Line17420.5 Drawing Lines that Connect Multiple Points176Example 20.1 Drawing straight lines that connect multiple points20.6 Drawing a curve that Connect Multiple PointsExample 20.2 Drawing a Curve20.7 Drawing a Quadratic CurveExample 20.3 Drawing the Quadratic20.8 Drawing a Sine CurveExample 20.4 Drawing a Sine Curve17717817918018018218320.8 Drawing a Rectangle18420.9 Customizing Line Style of the Pen Object186Example 20.5 Drawing a Rectangle with Dash Line Border20.10 Drawing an Ellipse186187Example 20.6 Drawing an Ellipse188Example 20.7 Drawing Ellipse with DrawEllipse Method18920.11 Drawing a CircleExample 20.8 Drawing a Circle20.12 Drawing TextExample 20.10 Drawing TextExample 20.11 Drawing Text190190190192193

20.13 Drawing Polygons194Example 20.12 Drawing a Triangle195Example 20.13 Drawing a Quadrilateral19620.14 Drawing a PieExample 20.14 Drawing a pie that sweeps clockwise through 60 degrees.20.15 Filling Shapes with Color19719819920.15.1 Drawing and Filling a Rectangle with Color199Example 20.15 Drawing a Square19920.15.2 Drawing and Filling an Ellipse with Color201Example 20.16 Drawing an Ellipse and Color it20120.15.3 Drawing and Filling a Polygon with Color202Example 20.17 Drawing a Polygon and Color It20220.15.4 Drawing and Filling a Pie203Example 20.18 Drawing a Pie and Color It203Chapter 21 Using Timer204Example 21.1 Creating a Digital Clock205Example 21.2 Creating a Stopwatch206Example 21.3 Creating a Digital Dice207Chapter 22 Creating Animation210Example 22.1 Creating a Moving Object210Example 22.2 Creating a Graphical Dice211Example 22.3 Creating a Slot Machine214Chapter 23 Working with Databases21723.1 Introduction to Database21723.2 Creating a Database Application21723.3 Creating a Connection to a Database using ADO.NET219

23.4 Populating Data in ADO.NETExample 23.1 Creating a Customer Contact List22722923.5 Browsing Records23123.6 Editing, Saving, Adding and Deleting Records232Example 23.2 Browsing the Database23.7 Accessing Database using DataGridView233237Example 23.3 Browsing Data Using DataGridView23723.8 Performing Arithmetic Calculations in a Database238Example 23.4 Performing Arithmetic Calculations in a Database239Example 23.5 Calculating Income241Example 23.6 Using the Count Function243Chapter 24 Reading and Writing Text Files24524.2 Reading a Text File24524.3 Writing to a Text File249Chapter 25 Building Console Applications25.1 IntroductionExample 25.1: Displaying a Message25225225425.2 Creating a Text File Writer in Console255Example 25.2 Creating a Text File Writer25525.3 Creating a Text File Reader in Console256Example 25.3 Creating a Text File Reader25625.4 Creating a Console App using If.Then.ElseExample 25.4 Arithmetic Console AppChapter 26 Creating Menu Bar and Toolbar25725726026.1 Creating Menu Items on the Menu Bar26026.2 Creating the Toolbar269

Chapter 27 Deploying your VB 2019 ApplicationsIndex274279

Chapter 1 Introduction1.1 A Brief History of Visual BasicVisual Basic is a third-generation event-driven programming language first releasedby Microsoft in 1991. The final version of the classic Visual Basic was Visual Basic6. Visual Basic 6 is a user-friendly programming language designed for beginners. Itenables anyone to develop GUI Windows applications easily. Many developers stillfavor VB6 over its successor VB.NET.In 2002, Microsoft released Visual Basic.NET (VB.NET) to replace Visual Basic 6.Thereafter, Microsoft declared VB6 a legacy programming language in 2008.However, Microsoft still provides some form of support for VB6. VB.NET is a fullyobject-oriented programming language implemented in the .NET Framework. It wascreated to cater for the development of the web as well as mobile applications.Subsequently, Microsoft has released many versions of VB.NET. They are VisualBasic 2005, Visual Basic 2008, Visual Basic 2010 , Visual Basic 2012, Visual Basic2013 , Visual Basic 2015, Visual Basic 2017 and Visual Basic 2019 . Although the.NET portion was discarded in 2005, all versions of the Visual Basic programminglanguage released since 2002 are regarded as the VB.NET programming languageMicrosoft has released Visual Studio 2019 in early 2019. VS 2019 allows you to codein different programming languages and different platforms, Visual Basic 2019 isone of them. The other Programming languages are C# C , F#, JavaScript, Javaand Python. Visual Basic 2019 is the latest version of the VB.NET programminglanguage released by Microsoft.1.2 Installation of Visual Studio 2019

You can download the free version of Visual Studio 2019 from the following g the link brings up the Visual Studio 2019 download page, as shown inFigure 1.1Figure 1.1You can choose the free Visual Studio Community 2019 or the Full-featuredProfessional 2019 and End-to-End solution Enterprise 2019 to download. The freeversion provides full-featured IDE for students, open source community andindividuals. Since this book was written based on the free version, proceed todownload the free Visual Studio 2019 Community, select community and downloadthe installer file. The downloaded installer file will appear on your Windows 10taskbar. Click it to install Visual Studio 2019. Clicking the Visual Studio 2019 Installerwill start downloading, unpacking and installing the files necessary for the installationof Visual Studio 2019, as shown in Figure 1.2

Figure 1.2You will see several status screens that show the progress of the installation. Afterthe installer has finished installing, it is time to pick the feature set that you wish toinstall, as shown in Figure 1.3. Since we are focusing on developing Visual Basic2019 desktop app, we will select the .NET desktop development component. Aftermaking your selections, click install.Figure 1.3Upon completion of the installation, you are now ready to launch Visual Studio 2019and start programming in Visual Basic 2019

1.3 Creating a Visual Basic 2019 ProjectTo create a Visual Basic 2019 project, launch Microsoft Visual Studio 2019.Launching VS 2019 will bring up the Visual Studio 2019 Start Page, as shown inFigure 1.4Figure 1.4 Visual Studio 2019 Start PageThe Visual Studio 2019 start page comprises two sections, the Open Recent sectionand the Get Started section. In the start page, you can select a recent project file orchoose any option in the Get Started section. You can choose to clone a project fromGitHub or Azure DevOps, open a project or solution, open a local folder, create anew project, or continue without code.

Let us create a new project by clicking on the Create a new project option. You willsee the Create a new project template page, as shown in Figure 1.5. In the Create anew project page, select Visual Basic language.Figure 1.5 Create a new project templateNext, select the Windows Forms App (.Net Framework) template as we want todevelop a Windows desktop project, as shown in Figure 1.5

Figure 1.6 Create a new project templateUpon clicking the selected project template, the project configuration page appears,as shown in Figure 1.7. You can configure your project by typing the project nameand select a few other options.

Figure 1.7 Configuring ProjectAt the bottom of this dialog box, you can change the default project nameWindowsApplication1 to some other name you like, for example, My First VisualBasic 2019 App. After renaming the project, click OK to continue. The Visual Basic2019 IDE Windows will appear, as shown in Figure 1.8. Visual Basic 2019 IDEcomprises a few windows, the Form window, the Solution Explorer window, and theProperties window. It also consists of a toolbox which contains many useful controlsthat allows the programmer to develop his or her Visual Basic 2019 programs.

Figure 1.8 The Visual Basic 2019 Express IDEThe Toolbox is not shown until you click on the Toolbox tab. When you click on theToolbox tab or use the shortcut keys Ctrl Alt x, the common controls Toolbox willappear, as shown in Figure 1.9. You can drag and move your toolbox around anddock it to the right, left, top or bottom of the IDE.Figure 1.9 Visual Basic 2019 Toolbox

Next, we shall proceed to show you how to create your first VB2019 application.First, change the text of the form to ‘My First VB 2019 App’ in the properties window;it will appear as the title of the application. Next, insert a button and change its text toOK. The design interface is shown in Figure 1.10Figure 1.10 The Design InterfaceNow click on the OK button to bring up the code window and enter the followingstatement between Private Sub and End Sub procedure, as shown in Figure 1.11.MsgBox( "My First Visual Basic 2019 App" )Clicking the Start button on the toolbar or press F5 to run the application will launchthe runtime interface, as shown in Figure 1.12. Executing the application by clickingon the OK button will bring up a dialog box that displays the “My First Visual Basic2019 App" message, as shown in Figure 1.13. The function MsgBox is a built-infunction of Visual Basic 2019 which can display the text enclosed within thebrackets.

Figure 1.11 Visual Basic 2019 Code WindowFigure 1.12 The Runtime InterfaceFigure 1.13 The Message Box

Summary In section 1.1, you have learned about the history of Visual Basic 2019 In section 1.2, you have learned how to install and launch Visual Basic Studio2019 In section 1.3, you have learned how to launch the new project dialog and theVisual Basic 2019 IDE. You have also learned how to write your first program.

Chapter 2 Designing the User InterfaceThe first step in developing an application is to design the user interface (UI). Tobuild a graphical user interface, first you need to customize the default form bychanging its properties at design phase and at runtime, including its name, title,background color and so forth. After customizing the default form, you may proceedto add controls from the toolbox to the form and then customize their properties.2.1 Customizing the FormWhen you start a new Visual Basic 2019 project, the VB2019 IDE will display thedefault form along with the Solution Explorer window and the Properties window, asshown in Figure 2.1. The name of the default form is Form1. The properties windowdisplays all the properties associate with Form1 and their corresponding attributes orvalues. You can change the name of the form, the title of the form using the textproperty, the background color, the foreground color, size and more. Try changingthe properties as shown in Table 2.1Table 2.1PropertyValueNameMyFormTextMy First VB2019 oxFalseYou do not have to type in the color manually, you can indeed select a color from thecolor drop-down list that comprises three tabs, Custom, Web, and System, as shownin Figure 2.1. Clicking on the drop-down arrow will bring out a color palette or a list ofcolor rectangles where you can select a color.

Figure 2.1Another method of setting the colors is to manually type in the RGB color code or thehex color code. The values of R, G and B ranges from 0 to 255, therefore, by varyingthe values of the RGB we can obtain different colors.The hex color code system usesa six-digit, three- byte hexadecimal number torepresent colors. The bytes represent the red, green and blue components of thecolor. One byte represents a number ranging from 00 to ff, or 0 to 255 in decimalnotation. For example, #0000ff represents the cyan color. However, when you typethe Hex color code in the properties window, it automatically converts the color toRGB color. Figure 2.2 shows a list of Hex color codes and the corresponding colors.

Figure 2.2 Hex Color CodesThe design interface is shown in Figure 2.3 and the runtime interface is shown inFigure 2.4. In the runtime interface, notice that the title has been changed fromForm1 to My First Visual Basic 2019 App, background color changed to LavenderBlush , the text OK color is Crimson and the window cannot be maximized.Figure 2.3 Design UI

Figure 2.4 Runtime UIYou can also change the properties of the form at runtime by writing relevant codes.The default form is an object and an instant of the form can be denoted by thename Me . The property of the object can be defined by specifying the object’s namefollowed by a period, as follows:ObjectName.propertyFor example, setting the background of the form to blue using the following code:Me. BackColor Color . BlueIn addition, you can use the FromArgb method to specify the color using the RGBcodes, as follows:Me .BackColor Color .FromArgb(0, 255, 0)Example 2.1 Changing Properties at RuntimePrivate Sub Form1 Load (sender As Object , e As EventArgs) HandlesMyBase .Load Me . Text "My First Visual Basic 2019 Application"Me .BackColor Color .Turquoise

e .ForeColor Color .IvoryMMyBtn.BackColor Color .DodgerBlue Me .MaximizeBox False Me .MinimizeBox TrueEnd SubTo runtime UI is shown in Figure 2.5. Notice that is now different from that shown inFigure 2.4,Figure 2.5In place of Turquoise , you can use RGB code as follows:Me .BackColor Color .FromA rgb(64,224,208)In addition, you can specify the size, the opacity and the position of the default formusing the code, as in Example 2.2Example 2.2 Customizing the FormPrivate Sub Form1 Load(sender As Object , e As EventArgs HandlesMyBase .LoadMe . Text "My First VB2019 App"Me .BackColor Color .BeigeMe .MaximizeBox False

Me .MinimizeBox TrueMe .Size New Size (400, 400)Me .Opacity 0.85Me .CenterToParent()End SubThe property Opacity sets the degree of transparency. The runtime interface is asshown in Figure 2.6Figure 2.62.2 Adding Controls to the FormIn section 2.1, we have learned how to build an UI by customizing the default form.Next, we shall continue to build the UI by adding some controls to the form. Thecontrols are objects that consist of three elements, namely properties, methods, and

events. We can add them to the form from the Toolbox. Among the controls, themost used ones are button, label, textbox, list box, combo box, picture box, checkbox, radio button and more. These controls can be made visible or invisible atruntime. However, some controls will only run in the background and never be seenat runtime, one such control is the timer.The Toolbox is usually hidden when you start Visual Basic 2019 IDE, you need toclick View on the menu bar and then select Toolbox to reveal the toolbox, as shownin Figure 2.6. You can also use shortcut keys Ctrl w x to bring out the toolbox.Figure 2.6: ToolboxYou can position the Toolbox by dragging it anywhere you like while its status is setto float. You can also dock the toolbox by right-clicking on the toolbox and choosedock from the pop-up menu. The docked Toolbox appears side by side with theSolution Explorer, or as one of the tabbed windows together with the Form Designwindow and the code window, as shown in Figure 2.7.

Figure 2.7 ToolboxYou can also dock the toolbox at the bottom, below the default form, as shown inFigure 2.8. Further, you may also pin the toolbox to the side bar or the bottom bar byclicking on the pin icon on the menu bar of the toolbox.How and where you want to position your toolbox is entirely up to you but westrongly suggest that you place the tool box alongside or at the bottom of the defaultform for ease of use. You should never cover the form with the toolbox because itwill be difficult to add controls to the form.

Figure 2.8Adding a control to the form is an easy task, what you need to do is double click it ordrag it onto the form. You can drag the control around the form, and resize it.To demonstrate how to add the controls and then change their properties, we shalldesign a picture viewer. First, change the title of the default form to Picture Viewer inits properties window. Next, insert a picture box on the form and change itsbackground color to white. To do this, right click the picture box and select propertiesin the popup menu, then look for the BackColor Property as shown in the propertieswindow in Figure 2.9.Finally, add two buttons to the form and change the text to View and Close in theirrespective properties’ windows. The picture viewer is not functional yet until we writecode for responding to events triggered by the user. We will deal with theprogramming part in the coming chapters.

Figure 2.9Summary In section 2.1, you have learned how to customize the form by changing thevalues of its properties. In section 2.2, you have learned how to add controls to the form and change theirproperties at design phase and at runtime.

Chapter 3 Writing the CodeIn the previous chapter, we have learned how to design the user interface by addingcontrols to the form and by changing their properties. However, the user interfacealone will not work without adding code to them. In this chapter, we shall learn howto write code for all the controls to enable them to interact with the events triggeredby the users. Before learning how to write Visual Basic 2019 code, let us delve intothe concept of event-driven programming3.1 The Concept of Event-Driven ProgrammingVisual Basic 2019 is an event-driven programming language, meaning that the codeis executed in response to events triggered by the user like clicking the mouse orpressing a key on the keyboard. Some other events are selecting an item from adrop-down list, typing some words into a text box and more. It may also be an eventthat runs in response to some other program. Some of the common events in VisualBasic 2019 are load, click, double-click, drag-drop, keypress and more.Every control you place on the form has a set of events associate with it. To view theevents, double-click the control on the form to enter the code window. The defaultevent will appear at the top right side of the code window. You must click on thedefault event to view other events associated with the control. The code appears onthe left side is the event procedure associated with the load event. Figure 3.1illustrates the event procedure Load associated with the Form (its name has beenchanged to PicViewer therefore you can see the words PicViewer events) and Figure3.2 shows the events associated with the button.

Figure 3.1: Events associated with FormFigure 3.2: Events associated with the button

3.2 Writing the CodeTo write code, click on any part of the form to go into the code window, as shown inFigure 3.1. The event procedure is to load Form1 which starts with the keywordsPrivate Sub and ends with End Sub . This procedure includes the Form1 class andthe event Load, and they are bound together with an underscore, i.e. Form Load . Itdoes nothing other than loading an empty form. To make the load event dosomething, insert the following statement.MsgBox ( "Welcome to Visual Basic 2019")Example 3.1 Displaying a MessagePublic Class Form1Private Sub Form1 Load(sender As Object, e As EventArgs ) Handles MyBase .LoadMsgBox ( "My First Visual Basic 2019 APP", ,"My Message")End SubEnd ClassMsgBox is a built-in function that displays a message in a pop-up message box. TheMsgBox function comprises a few arguments, the first is the message that isdisplayed and the third one is the title of the message box. When you run theprogram, a message box displaying the phrase "My First Visual Basic 2019 APP" willappear, as shown in Figure 3.3.

Figure 3.3You will notice that above the Private Sub structure there is a preceding keywordPublic Class Form1 . This has to do with the concept of the object-orientedprogramming language. When we start a windows application in Visual Basic 2019,we will see a default form with the name Form1 appear in the IDE, it is the Form1Class that inherits from the Form class System.Windows.Forms.Form .Now, let us write a code that perform arithmetic calculation , as in Example 3.2Example 3.3 Arithmetic CalculationsPrivate Sub Form1 Load(sender As Object , e As EventArgs ) HandlesMyBase .LoadMsgBox( "2" & " " & "5" & " " & 2 5 )End Sub*The symbol & (ampersand) is to perform string concatenation. The output is asshown in Figure 3.4Figure 3.4Summary 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

2.1 Customizing the Form 25 Example 2.1 Changing Properties at Runtime 28 Example 2.2 Customizing the Form 29 2.2 Adding Controls to the Form 30 C h a p t e r 3 W ri t i n g t h e C o d e 3 5 3.1 The Concept of Event-Driven Programming 35 3.2 Writing the Code 36 Example 3.1 Displ