{"id":2253,"date":"2013-02-07T20:00:36","date_gmt":"2013-02-07T12:00:36","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2018-06-24T17:46:49","modified_gmt":"2018-06-24T09:46:49","slug":"visual-basic-2012-lesson-5-writing-the-code","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-5-writing-the-code\/","title":{"rendered":"Visual Basic 2012 Lesson 5-Writing the Code"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-4-object-oriented-programming\/\">[Lesson 4]<\/a>&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS] <\/a>&gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-6-managing-data\/\"> [Lesson 6]<\/a><\/strong><\/h4>\n<p>In this lesson, you will learn some basic theories about Visual Basic 2012 programming. We shall focus more on learning by doing. We will keep the theories short so that it would not be too difficult for beginners.<\/p>\n<p>Visual Basic 2012 is an object-oriented and event driven programming language. Event-driven means the VB program runs in response to the user&#8217;s action. The actions include clicking the command button, entering text in a text box, choosing an item, closing the application and more.<\/p>\n<h3>5.1 The Event Procedure<\/h3>\n<p>Each event is related to an object, it is an incident that happens to the object due to the action of the user. A class has events as it creates an instant of a class or an object. When we start a windows application in Visual Basic 2012, 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 <strong>System.Windows.Forms.Form<\/strong>, as shown in the Form1 properties windows in Figure 5.1.<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=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2260\" title=\"vb2012_figure5.1\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg\" alt=\"visual basic 2012 IDE\" width=\"271\" height=\"441\" \/><\/a><\/p>\n<h4 align=\"center\">Figure 5.1<\/h4>\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=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nTo start writing code in Visual Basic 2012, \u00a0click on any part of the form to go into the code window as shown in Figure 5.2. This is the structure of an event procedure. In this case, the event procedure is to load Form1 and it starts with <strong>Private Sub<\/strong> and ends with <strong>End Sub<\/strong>. This procedure includes the Form1 class and the event <strong>Load<\/strong>, and they are bind together with an underscore, i.e. Form_Load. It does nothing other than loading an empty form. You don&#8217;t have to worry the rest of the stuff at the moment, they will be explained in later lessons.<\/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&lt;\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p>There are other events associated with the Form1 class, such as click, cursorChanged, DoubleClick, DragDrop, Enter and more, as shown in the diagram Figure 5.2 (It appears when you click on the upper right pane of the code window)<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2263\" title=\"vb2012_figure5.3\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.3.jpg\" alt=\" Visual Basic 2012\" width=\"827\" height=\"512\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 5.2<\/strong><\/p>\n<h3>5.2 Writing the code<\/h3>\n<p>Now you are ready to write the code for the event procedure so that it will do something more than loading a blank form. The code must be entered between Private Sub&#8230;&#8230;.End Sub. Let&#8217;s enter the following code :<\/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 Me.Text = \"My First VB2012 Program\"\r\n Me.ForeColor = Color.LightGoldenrodYellow\r\n Me.BackColor = Color.RoyalBlue\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p>The first line of the code will change the title of the form to My First Visual Basic 2012 Program, the second line will change the foreground object to LightGoldenrodYellow( in this case, it is a label that you insert into the form and change its text to Foreground) and the last line changes the background to RoyalBlue\u00a0color. The equal operator = in the code is used to assign something to the object, like assigning yellow color to the foreground of the Form1 object (or an instance of Form1). Me is the name given to the Form1 class. We can also call those lines as Statements. So, the actions of the program will depend on the statements entered by the\u00a0programmer.<\/p>\n<p>The output is shown Figure 5.3 below:<\/p>\n<p align=\"center\"><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.41.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2266\" title=\"vb2012_figure5.4\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.41.jpg\" alt=\" Visual Basic 2012\" width=\"300\" height=\"300\" \/><\/a><\/p>\n<p><span style=\"color: #444444;\"><strong>Figure 5.3<\/strong><\/span><\/p>\n<p>Here is another example. In this project, you insert one button into the form and change its label text to &#8220;Display Hidden Names&#8221;. Click on this button to enter the code window and key-in the following code:<\/p>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\nDim name1, name2, name3, names As String\r\n name1 = \"George\"\r\n name2 = \"Michael Chan\"\r\n name3 = \"Norman\"\r\n names=name1 &amp; \",\"&amp; name2 &amp; \"and \"&amp; name3\r\n MsgBox(\" The hidden names are \" &amp; names,,\"Hidden Names\")\r\nEnd Sub\r\n<\/pre>\n<p>The keyword Dim is to declare variables name1, name2 and name3 , names as string, which means they can only handle text. The variable names is to accept the names that are joined together by the &#8220;&amp;&#8221; signs as a single string. The function MsgBox \u00a0is to display the string in a message box, and the last argument &#8220;Hidden Names&#8221; is the title of the MsgBox function. The \u00a0output is shown in Figure 5.4 below:<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2268\" title=\"vb2012_figure5.5\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.5.jpg\" alt=\"Visual Basic 2012\" width=\"826\" height=\"739\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 5.4<\/strong><\/p>\n<p><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=\"6598395509\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<hr \/>\n<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-4-object-oriented-programming\/\">[Lesson 4]<\/a>&lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS] <\/a>&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-6-managing-data\/\">[Lesson 6]<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 4]&lt;&lt;\u00a0[CONTENTS] &gt;&gt; [Lesson 6] In this lesson, you will learn some basic theories about Visual Basic 2012 programming. We shall focus more on learning by doing. We will keep the theories short so that it would not be too difficult for beginners. Visual Basic 2012 is an object-oriented and event driven programming language. Event-driven &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-5-writing-the-code\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2012 Lesson 5-Writing the 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-2253","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 2012 Lesson 5-Writing the Code in Visual Basic 2012<\/title>\n<meta name=\"description\" content=\"This visual basic 2012 lesson shows you how to write code in Visual Basic 2012\" \/>\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\/vb2012\/vb2012_lesson5.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2012 Lesson 5-Writing the Code in Visual Basic 2012\" \/>\n<meta property=\"og:description\" content=\"This visual basic 2012 lesson shows you how to write code in Visual Basic 2012\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.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-24T09:46:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.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=\"4 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\/visual-basic-2012-lesson-5-writing-the-code\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html\",\"name\":\"Visual Basic 2012 Lesson 5-Writing the Code in Visual Basic 2012\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg\",\"datePublished\":\"2013-02-07T12:00:36+00:00\",\"dateModified\":\"2018-06-24T09:46:49+00:00\",\"description\":\"This visual basic 2012 lesson shows you how to write code in Visual Basic 2012\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg\",\"width\":\"271\",\"height\":\"441\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2012 Lesson 5-Writing the 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 2012 Lesson 5-Writing the Code in Visual Basic 2012","description":"This visual basic 2012 lesson shows you how to write code in Visual Basic 2012","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\/vb2012\/vb2012_lesson5.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2012 Lesson 5-Writing the Code in Visual Basic 2012","og_description":"This visual basic 2012 lesson shows you how to write code in Visual Basic 2012","og_url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.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-24T09:46:49+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-5-writing-the-code\/","url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html","name":"Visual Basic 2012 Lesson 5-Writing the Code in Visual Basic 2012","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg","datePublished":"2013-02-07T12:00:36+00:00","dateModified":"2018-06-24T09:46:49+00:00","description":"This visual basic 2012 lesson shows you how to write code in Visual Basic 2012","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/02\/vb2012_figure5.1.jpg","width":"271","height":"441"},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson5.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2012 Lesson 5-Writing the 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\/2253","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=2253"}],"version-history":[{"count":46,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2253\/revisions"}],"predecessor-version":[{"id":13097,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2253\/revisions\/13097"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=2253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=2253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=2253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}