{"id":7283,"date":"2015-10-23T13:58:24","date_gmt":"2015-10-23T05:58:24","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=7283"},"modified":"2018-06-22T22:20:17","modified_gmt":"2018-06-22T14:20:17","slug":"visual-basic-2015-lesson-32-using-timer","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-32-using-timer\/","title":{"rendered":"Visual Basic 2015 Lesson 32: Using Timer"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-31-filling-shapes-colour\/\">[Lesson 31] <\/a>&lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-33-creating-animation\/\">[Lesson 33]<\/a><\/h4>\n<p>The timer is an interesting and useful control in Visual Basic 2015. It can be used to create Visual Basic 2015 applications\u00a0that are time-related. For example, you can use the timer to create a clock, a stopwatch, a dice, animation and more. Timer is a hidden control at runtime, just like the engine of a\u00a0car. We shall illustrate the usage of timer using a few examples.<\/p>\n<h3>32.1 Creating a Digital Clock<\/h3>\n<p>To create the clock, first of all, start a new project in Visual Basic 2015\u00a0 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. Add the Timer control to the form by double-clicking it in the ToolBox.<\/p>\n<p>The next step is to 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 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. Lastly, set\u00a0the Enabled property of the Timer control to True so that the clock starts running as soon as it is loaded.<\/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=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<pre style=\"font-size: 110%;\">Label1.Text = TimeOfDay<\/pre>\n<p>*TimeOfDay() is a Visual Basic 2015 built-in 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, 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\r\nLblClock.Text = TimeOfDay\r\n\r\nEnd Sub\r\n<\/pre>\n<p>The digital clock is as shown in Figure 32.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 loading=\"lazy\" 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=\"300\" height=\"300\" \/><\/a><strong>Figure 32.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=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>\u00a0<strong>32.2 Creating a Stopwatch<\/strong><\/h3>\n<p>We can create a simple stopwatch using the Timer control in Visual Basic 2015. 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. Insert three \u00a0buttons and change their names to BtnStart, BtnStop and BtnReset respectively. Change their texts 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 32.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 loading=\"lazy\" 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=\"300\" height=\"300\" \/><\/a><strong>Figure 32.2<\/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=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h2>\u00a0<strong>32.3 Creating a Digital Dice<\/strong><\/h2>\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<pre style=\"font-size: 110%;\">n = Int(1 + Rnd() * 6)<\/pre>\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 loading=\"lazy\" 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=\"300\" height=\"300\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>\u00a0Figure 32.3<\/strong><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"5090773108\"><\/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-2015-lesson-31-filling-shapes-colour\/\">[Lesson 31]\u00a0<\/a>&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a>\u00a0&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-33-creating-animation\/\">[Lesson 33]<\/a><\/h4>\n<p>&nbsp;<\/p>\n<pre style=\"font-size: 110%;\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 31] &lt;&lt; [Contents] &gt;&gt; [Lesson 33] The timer is an interesting and useful control in Visual Basic 2015. It can be used to create Visual Basic 2015 applications\u00a0that are time-related. For example, you can use the timer to create a clock, a stopwatch, a dice, animation and more. Timer is a hidden control at &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-32-using-timer\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2015 Lesson 32: 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-7283","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 2015 Lesson 32: Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This article discusses the use of timer to create applications in visual basic 2015\" \/>\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\/vb2015\/vb2015_lesson32.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2015 Lesson 32: Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This article discusses the use of timer to create applications in visual basic 2015\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.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-22T14:20:17+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-2015-lesson-32-using-timer\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.html\",\"name\":\"Visual Basic 2015 Lesson 32: 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\/vb2015\/vb2015_lesson32.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg\",\"datePublished\":\"2015-10-23T05:58:24+00:00\",\"dateModified\":\"2018-06-22T14:20:17+00:00\",\"description\":\"This article discusses the use of timer to create applications in visual basic 2015\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.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\/vb2015\/vb2015_lesson32.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2015 Lesson 32: 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 2015 Lesson 32: Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This article discusses the use of timer to create applications in visual basic 2015","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\/vb2015\/vb2015_lesson32.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2015 Lesson 32: Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This article discusses the use of timer to create applications in visual basic 2015","og_url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.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-22T14:20:17+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-2015-lesson-32-using-timer\/","url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.html","name":"Visual Basic 2015 Lesson 32: 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\/vb2015\/vb2015_lesson32.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure31.1.jpg","datePublished":"2015-10-23T05:58:24+00:00","dateModified":"2018-06-22T14:20:17+00:00","description":"This article discusses the use of timer to create applications in visual basic 2015","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson32.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\/vb2015\/vb2015_lesson32.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2015 Lesson 32: 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\/7283","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=7283"}],"version-history":[{"count":21,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/7283\/revisions"}],"predecessor-version":[{"id":13048,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/7283\/revisions\/13048"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=7283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=7283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=7283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}