{"id":4139,"date":"2014-01-06T11:17:20","date_gmt":"2014-01-06T03:17:20","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=4139"},"modified":"2018-06-23T11:38:58","modified_gmt":"2018-06-23T03:38:58","slug":"writing-the-vb-code","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/writing-the-vb-code\/","title":{"rendered":"Visual Basic 2013 Lesson 4: Writing the Visual Basic Code"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/adding-controls-to-the-form\/\">[Lesson 3] <\/a>&lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents] <\/a>&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/working-with-controls\/\">[Lesson 5]<\/a><\/strong><\/h4>\n<p>In the preceding lesson, you have learned how to design the user interface by inserting\u00a0controls to the form and changing their properties. However, the controls will not function\u00a0without adding code to them.\u00a0 Therefore,\u00a0 we shall learn how to write code for all the controls so that they can interact with events triggered by the users. However, before learning how to write Visual Basic code, let me first explain\u00a0the concept of event-driven programming<\/p>\n<h3>4.1 The Concept of Event-Driven Programming<\/h3>\n<p>Visual Basic 2013 is an event-driven programming language, which means that the code is executed in response to events. In addition, every control(object) you place on the form has a set of events related to them.\u00a0Some of the events are load, click, double click, drag, and drop, pressing the keys and more.<\/p>\n<p>In order to view the events, you need to double-click the control on the form to enter the code window.\u00a0 The default event will appear at the top portion on the right side of the code window.\u00a0 In addition, you need to click on the default event to view other events associated with the control.\u00a0 As you shall see, the VB code appears on the left side is the event procedure associated with the load event. Figure 4.1 illustrates the event procedure <strong>load<\/strong> associated with the default form.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4151\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg\" alt=\"vb2013_figure4.1\" width=\"960\" height=\"692\" \/><\/a><\/p>\n<p><strong>Figure 4.1: Events associated with Form<\/strong><\/p>\n<p>Figure 4.2 shows the events associated with the button<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4158\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.2.jpg\" alt=\"vb2013_figure4.2\" width=\"960\" height=\"692\" \/><\/a><\/p>\n<p><strong>\u00a0<span style=\"line-height: 1.714285714; font-size: 1rem;\">Figure 4.2<\/span><\/strong><\/p>\n<h3>4.2 Writing the VB Code<\/h3>\n<p>After opening up the code window, you can start writing the VB code.\u00a0 The procedure in this example is to load the default form\u00a0\u00a0<strong>Form1. <\/strong>As you can see, the control Form1 is associated with the\u00a0<strong>Load <\/strong>event with an underscore, i.e. Form1_Load. It does nothing other than loading an empty form. Furthermore, the event procedure usually starts with the keywords\u00a0<strong>Private Sub<\/strong>\u00a0and ends with the keywords\u00a0<strong>End Sub<\/strong>.\u00a0 In order to make the load event does something,\u00a0 we shall insert the statement MsgBox( &#8220;Welcome to Visual Basic 2013&#8221;)<\/p>\n<pre style=\"font-size: 110%;\">Public Class Form1\r\nPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\r\n MsgBox( \"My First Visual Basic 2013 Program\")\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p>When you run the program, a message box that displays the text\u00a0&#8220;My First Visual Basic 2013 Program&#8221; will appear, as shown in Figure 4.3. <strong>MsgBox<\/strong> is a built-in function in Visual Basic 2013 that displays a message in a pop-up message box.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/12\/vb2013_figure1.7.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-4021\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/12\/vb2013_figure1.7.jpg\" alt=\"vb2013_figure1.7\" width=\"80%\" height=\"auto\" \/><\/a><strong>Figure 4.3<\/strong><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>&nbsp;<\/p>\n<h4>4.2.1 The Concept Object-Oriented Programming<\/h4>\n<p>You will notice that in the event procedure illustrated above,\u00a0 there is a preceding keyword <strong>Public Class<\/strong> Form1. This is the concept of object-oriented programming language.\u00a0\u00a0<span style=\"font-size: 1rem;\">When we start a windows application in Visual Basic 2013, we will see a default form with the name Form1 appears in the IDE, it is actually the Form1 Class that inherits from the Form class\u00a0<\/span><strong style=\"font-size: 1rem;\">System.Windows.Forms.Form.\u00a0<\/strong><span style=\"line-height: 1.714285714; font-size: 1rem;\">A class has events as it creates an instance of a class or an object.\u00a0 We shall learn more about object-oriented programming in another lesson.<\/span><\/p>\n<h4>Example 4.1<\/h4>\n<p>You can also write VB code to perform arithmetic calculations. For example, you can use the MsgBox and the arithmetic operator plus to perform an addition of two numbers, as shown below:<\/p>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load\r\nMsgBox(\"2\" &amp; \"+\" &amp; \"5\" &amp; \"=\"&amp; 2 + 5)\r\nEnd Sub\r\n<\/pre>\n<p>*The symbol &amp; (<i>ampersand) <\/i>is to perform\u00a0string concatenation.<\/p>\n<p>The output is as shown in Figure 4.4<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4170\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.4.jpg\" alt=\"vb2013_figure4.4\" width=\"154\" height=\"155\" \/><\/a><strong>Figure 4.4\u00a0<\/strong><\/p>\n<p>If you wish to close the window after the message, you can add the statement\u00a0<strong>Me.Close(), <\/strong>as follows:<\/p>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load\r\n MsgBox(\"2\"&amp; \"+\"&amp; \"5\"&amp;\"=\" &amp; 2 + 5)\r\n Me.Close\r\nEnd Sub\r\n<\/pre>\n<p>We will learn more about code writing in coming lessons<\/p>\n<div><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"2306771905\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/div>\n<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/adding-controls-to-the-form\/\">[Lesson 3]\u00a0<\/a>&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents]\u00a0<\/a>&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/working-with-controls\/\">[Lesson 5]<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 3] &lt;&lt; [Contents] &gt;&gt; [Lesson 5] In the preceding lesson, you have learned how to design the user interface by inserting\u00a0controls to the form and changing their properties. However, the controls will not function\u00a0without adding code to them.\u00a0 Therefore,\u00a0 we shall learn how to write code for all the controls so that they can &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/writing-the-vb-code\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2013 Lesson 4: Writing the Visual Basic Code<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-4139","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2013 Lesson 4: Writing the Visual Basic Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"The lesson explains how to write VB code\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2013 Lesson 4: Writing the Visual Basic Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"The lesson explains how to write VB code\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-23T03:38:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/writing-the-vb-code\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html\",\"name\":\"Visual Basic 2013 Lesson 4: Writing the Visual Basic Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg\",\"datePublished\":\"2014-01-06T03:17:20+00:00\",\"dateModified\":\"2018-06-23T03:38:58+00:00\",\"description\":\"The lesson explains how to write VB code\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg\",\"width\":960,\"height\":692},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2013 Lesson 4: Writing the Visual Basic Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2013 Lesson 4: Writing the Visual Basic Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"The lesson explains how to write VB code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2013 Lesson 4: Writing the Visual Basic Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"The lesson explains how to write VB code","og_url":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-06-23T03:38:58+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/writing-the-vb-code\/","url":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html","name":"Visual Basic 2013 Lesson 4: Writing the Visual Basic Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg","datePublished":"2014-01-06T03:17:20+00:00","dateModified":"2018-06-23T03:38:58+00:00","description":"The lesson explains how to write VB code","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure4.1.jpg","width":960,"height":692},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson4.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2013 Lesson 4: Writing the Visual Basic Code"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=4139"}],"version-history":[{"count":101,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4139\/revisions"}],"predecessor-version":[{"id":13059,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4139\/revisions\/13059"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=4139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=4139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=4139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}