{"id":1059,"date":"2012-04-05T19:26:59","date_gmt":"2012-04-05T11:26:59","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2018-06-24T18:54:26","modified_gmt":"2018-06-24T10:54:26","slug":"visual-basic-2010-lesson-5","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-5\/","title":{"rendered":"visual Basic 2010 Lesson 5-Writing the Code"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-4\/\">[Lesson 4]<\/a>&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS] <\/a>&gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-6\/\"> [Lesson 6]<\/a><\/strong><\/h4>\n<p>In preceding lessons, you have\u00a0learned some basic concepts of\u00a0\u00a0Visual Basic 2010 programming. In this lesson, we learn to\u00a0write\u00a0actual programs.\u00a0We will keep the theories short so that it would not be too difficult for you.<\/p>\n<h3>5.1 The event Procedure<\/h3>\n<p>Visual Basic 2010 is an object-oriented and also a event-driven programming language. In fact, all windows applications are event-driven. Event-driven means the user decides what actions to take, like\u00a0clicking a command button or entering text in a text box. An event is related to an object, it is an incident\u00a0that happens to the object due to the action of the user, such as clicking the mouse or depressing a key on the keyboard. A class has events as it creates an instant of a class or an object.<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=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nWhen we launch\u00a0Visual Basic 2010, we will see a default form with the Form1 appears in its\u00a0IDE, it is actually the Form1 Class that inherits from the Form class System.Windows.Forms.Form, as shown in the Form1 properties windows.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif\" alt=\"Visual Basic 2010\" width=\"307\" height=\"407\" \/><br \/>\n<strong>Figure 5.1<\/strong><\/p>\n<p>When we click on any part of the form, we will see the code window as shown below. The is the structure of an event procedure. In this case, the event procedure is to load Form1 and it starts with 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. 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%; width: auto;\">Public Class Form1\r\nPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p>The are other events associated with the Form1 class, such as click, cursorChanged, DoubleClick, DragDrop, Enter and more as shown in the diagram below (It appears when you click on the upper right pane of the code window)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.2.gif\" alt=\"Visual Basic 2010\" width=\"878\" height=\"276\" \/><br \/>\n<strong>Figure 5.2<\/strong><\/p>\n<h3><strong>5.2 Writing the code<\/strong><\/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%; width: auto;\">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 VB2010 Program\"\r\n Me.ForeColor = Color.ForestGreen\r\n Me.BackColor = Color.Cyan\r\nEnd Sub\r\nEnd Classs\r\n<\/pre>\n<p>The first line of the code will change the title of the form to My First Visual Basic 2010 Program, the second line will change the foreground object to Forest Green( in this case, it is a label that you insert into the form and change its name to Foreground) and the last line changes the background to Cyan color.<\/p>\n<p>The equal (=)in the code actually is used to assign something to the object, like assigning a 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 programmer.<\/p>\n<p>The output is shown in Figure 5.1<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.3.gif\" alt=\"\" width=\"300\" height=\"300\" \/><br \/>\n<strong>Figure 5.1<\/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=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>Here is another example.<\/p>\n<pre style=\"font-size: 110%; width: auto;\">Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\nDim name1, name2, name3 As String\r\n name1 = \"John\"\r\n name2 = \"Georges\"\r\n name3 = \"Ali\"\r\n MsgBox(\" The names are \" &amp; name1 &amp; \" , \" &amp; name2 &amp; \" and \" &amp; name3)\r\nEnd Sub\r\n<\/pre>\n<p>In this example, you insert one command button into the form and rename its caption as Show Hidden Names. The keyword Dim is to declare variables name1, name2 and name3 as a string, which means they can only handle text. The function MsgBox is to display the names in a message box that are joined together by the &#8220;&amp;&#8221; signs. The output is shown in Figure 5.4<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.4.gif\" alt=\"Visual Basic 2010\" width=\"702\" height=\"402\" \/><br \/>\n<strong>Figure 5.4<\/strong><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- vb2010 tutorial matched --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"8075128701\" data-ad-format=\"autorelaxed\"><\/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-2010-lesson-4\/\">[Lesson 4]<\/a>&lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS] <\/a>&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-6\/\">[Lesson 6]<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 4]&lt;&lt;\u00a0[CONTENTS] &gt;&gt; [Lesson 6] In preceding lessons, you have\u00a0learned some basic concepts of\u00a0\u00a0Visual Basic 2010 programming. In this lesson, we learn to\u00a0write\u00a0actual programs.\u00a0We will keep the theories short so that it would not be too difficult for you. 5.1 The event Procedure Visual Basic 2010 is an object-oriented and also a event-driven programming language. &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-5\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">visual Basic 2010 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-1059","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 2010 Lesson 5-Writing the Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This Visual Basic 2010 lesson shows you how to write code in Visual Basic 2010\" \/>\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\/vb2010\/vb2010_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 2010 Lesson 5-Writing the Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This Visual Basic 2010 lesson shows you how to write code in Visual Basic 2010\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_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-24T10:54:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif\" \/>\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\/visual-basic-2010-lesson-5\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html\",\"name\":\"visual Basic 2010 Lesson 5-Writing the 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\/vb2010\/vb2010_lesson5.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif\",\"datePublished\":\"2012-04-05T11:26:59+00:00\",\"dateModified\":\"2018-06-24T10:54:26+00:00\",\"description\":\"This Visual Basic 2010 lesson shows you how to write code in Visual Basic 2010\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"visual Basic 2010 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 2010 Lesson 5-Writing the Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This Visual Basic 2010 lesson shows you how to write code in Visual Basic 2010","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\/vb2010\/vb2010_lesson5.html","og_locale":"en_US","og_type":"article","og_title":"visual Basic 2010 Lesson 5-Writing the Code - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This Visual Basic 2010 lesson shows you how to write code in Visual Basic 2010","og_url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_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-24T10:54:26+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif","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\/visual-basic-2010-lesson-5\/","url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html","name":"visual Basic 2010 Lesson 5-Writing the 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\/vb2010\/vb2010_lesson5.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif","datePublished":"2012-04-05T11:26:59+00:00","dateModified":"2018-06-24T10:54:26+00:00","description":"This Visual Basic 2010 lesson shows you how to write code in Visual Basic 2010","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html#primaryimage","url":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif","contentUrl":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_5.1.gif"},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson5.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"visual Basic 2010 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\/1059","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=1059"}],"version-history":[{"count":39,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1059\/revisions"}],"predecessor-version":[{"id":13128,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1059\/revisions\/13128"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=1059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=1059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}