{"id":4969,"date":"2014-01-30T08:05:33","date_gmt":"2014-01-30T00:05:33","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=4969"},"modified":"2018-06-24T17:23:53","modified_gmt":"2018-06-24T09:23:53","slug":"visual-basic-2013-lesson-31-using-timer","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-31-using-timer\/","title":{"rendered":"Visual Basic 2013 Lesson 31: Using Timer"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-30-creating-graphics-filling-shapes-color\/\">[Lesson 30] <\/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\/visual-basic-2013-lesson-32-creating-animation\/\">[Lesson 32]<\/a><\/h4>\n<h3>31.1 What is Timer?<\/h3>\n<p>The timer is a useful control in Visual Basic 2013. It can be used to write code for events that are time-related. For example, you can use the timer to create a clock, a stopwatch, a dice, an animated application and more. The timer is a hidden control at runtime, like the engine of an automobile. We shall illustrate the usage of timer using a few examples.<\/p>\n<h3>31.2 Creating a Digital Clock<\/h3>\n<p>To create the clock, first of all, start a new project in Visual Basic 2013 Express and select a new Windows Application. You can give the project any name you wish, but we will name it MyClock. Change the text of the Form1 to MyClock in the properties window.<\/p>\n<p>Now add the Timer control to the form by double-clicking it in the ToolBox. Next, insert a label control into the form. Change the Font size of the label to any size you wish, and set the text alignment to be center. Before we forget, you shall also set the Interval property of the Timer control to 1000, which reflects a one-second interval(1 unit is 1 millisecond). Remember to set the MaximizeBox property of Form1 to false so that the user cannot enlarge the clock. You also need to ensure that the Enabled property of the Timer control is set to True so that the clock starts running as soon as it is loaded.<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><br \/>\nNow, you are ready for the coding. Actually, you would be surprised that what you need to create a clock is only a one-line code, that is:<\/p>\n<pre style=\"font-size: 110%;\">Label1.Text = TimeOfDay<\/pre>\n<p>*TimeOfDay() is a Visual Basic 2013 function that returns the current time today based on your computer system time.<\/p>\n<p>Click on the Timer control and enter the code above, \u00a0three \u00a0buttons as shown below:<\/p>\n<pre style=\"font-size: 110%;\">Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick\r\n LblClock.Text = TimeOfDay\r\nEnd Sub\r\n<\/pre>\n<p>The digital clock is as shown in Figure 31.1<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-5056\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg\" alt=\"vb2013_figure31.1\" width=\"60%\" height=\"auto\" \/><\/a><strong>Figure 31.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=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>\u00a0<strong>31.3 Creating a Stopwatch<\/strong><\/h3>\n<p>We can create a simple stopwatch using the Timer control. Start a new project and name it stopwatch. Change the Form1 caption to Stopwatch. Insert the Timer control into the form and set its interval to 1000 which is equal to one second. Besides that, set the timer Enabled property to False so that it will not start ticking when the program is started. In addition, insert three buttons and change their names to BtnStart, BtnStop and BtnReset respectively. Change their text to \u201cStart\u201d, \u201cStop\u201d and \u201cReset\u201d accordingly. Now, enter the code as follows:<\/p>\n<pre style=\"font-size: 110%;\">Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click\r\n Timer1.Enabled = True\r\nEnd Sub\r\n\r\nPrivate Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick\r\n LblPanel.Text = Val(LblPanel.Text) + 1\r\nEnd Sub\r\n\r\nPrivate Sub BtnStop_Click(sender As Object, e As EventArgs) Handles BtnStop.Click\r\n Timer1.Enabled = False\r\nEnd Sub\r\n\r\nPrivate Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click\r\n LblPanel.Text = 0\r\nEnd Sub\r\n<\/pre>\n<p>The Interface of the Stopwatch is as shown in Figure 31.2<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.2.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-5063\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.2.jpg\" alt=\"vb2013_figure31.2\" width=\"40%\" height=\"auto\" \/><\/a><strong>Figure 31.2<\/strong><\/p>\n<h3>\u00a0<strong>31.4 Creating a Digital Dice<\/strong><\/h3>\n<p>We can create a digital dice easily using the Timer Control. To create a dice, you need to generate random numbers using the Rnd function. Rnd generates numbers between 0 and 1. The following statement\u00a0<span style=\"font-size: 1rem;\">generates random integers from 1 to 6\u00a0<\/span><\/p>\n<p><strong>n = Int(1 + Rnd() * 6)<\/strong><\/p>\n<p>In the code, we introduce the variable m to control the length of time of the rolling process. If m is more than 1000, then the rolling process will stop by setting the timer enabled property to False. Set the timer interval to 10 so that the number changes every 0.01 second.<\/p>\n<p><strong>The Code<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Public Class Form1\r\nDim n, m As Integer\r\nPrivate Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick\r\n m = m + 10\r\n If m<\/pre>\n<p>Running the program produces a dice with fast changing numbers which stops at a certain number. The interface is as \u00a0shown in Figure 31.3<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.3.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-5066\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.3.jpg\" alt=\"vb2013_figure31.3\" width=\"40%\" height=\"auto\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>\u00a0Figure 31.3<\/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=\"2306771905\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-30-creating-graphics-filling-shapes-color\/\">[Lesson 30]\u00a0<\/a>&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents]<\/a>\u00a0&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-32-creating-animation\/\">[Lesson 32]<\/a><\/h4>\n<p>&nbsp;<\/p>\n<pre style=\"font-size: 110%;\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 30] &lt;&lt; [Contents] &gt;&gt; [Lesson 32] 31.1 What is Timer? The timer is a useful control in Visual Basic 2013. It can be used to write code for events that are time-related. For example, you can use the timer to create a clock, a stopwatch, a dice, an animated application and more. The timer &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-31-using-timer\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2013 Lesson 31: Using Timer<\/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-4969","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 31: Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This article discuss the use of timer in visual basic 2013\" \/>\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_lesson31.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 31: Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This article discuss the use of timer in visual basic 2013\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.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:23:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.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\/visual-basic-2013-lesson-31-using-timer\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html\",\"name\":\"Visual Basic 2013 Lesson 31: Using Timer - 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_lesson31.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg\",\"datePublished\":\"2014-01-30T00:05:33+00:00\",\"dateModified\":\"2018-06-24T09:23:53+00:00\",\"description\":\"This article discuss the use of timer in visual basic 2013\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg\",\"width\":300,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2013 Lesson 31: Using Timer\"}]},{\"@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 31: Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This article discuss the use of timer in visual basic 2013","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_lesson31.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2013 Lesson 31: Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This article discuss the use of timer in visual basic 2013","og_url":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.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:23:53+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.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\/visual-basic-2013-lesson-31-using-timer\/","url":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html","name":"Visual Basic 2013 Lesson 31: Using Timer - 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_lesson31.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg","datePublished":"2014-01-30T00:05:33+00:00","dateModified":"2018-06-24T09:23:53+00:00","description":"This article discuss the use of timer in visual basic 2013","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg","width":300,"height":300},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson31.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2013 Lesson 31: Using Timer"}]},{"@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\/4969","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=4969"}],"version-history":[{"count":39,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4969\/revisions"}],"predecessor-version":[{"id":13085,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4969\/revisions\/13085"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=4969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=4969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=4969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}