{"id":2585,"date":"2013-02-15T10:37:27","date_gmt":"2013-02-15T02:37:27","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2018-06-24T18:21:12","modified_gmt":"2018-06-24T10:21:12","slug":"visual-basic-2012-lesson-27","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-27\/","title":{"rendered":"Visual Basic 2012 Lesson 27\u2013 Using Timer"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-26\/\">[Lesson 26]<\/a> <\/strong>&lt;&lt;\u00a0<strong><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-28\/\"> [Lesson 28]<\/a><\/strong><\/h4>\n<p>In this lesson, we shall show you how to use the timer in Visual Basic 2012. The timer is used to control and manage events that are time-related. For example, you need the timer to create a clock, a stopwatch, a dice, animation and more.<\/p>\n<p>In order to create a clock, you need to use the Timer control.It is invisible during runtime and it does not allow the user to interact with it.<\/p>\n<h3>27.1 Creating a Clock<\/h3>\n<p>To create the clock, first of all, start a new project in Visual Basic 2012 Express and select a new Windows Application. You can give the project any name it MyClock. Change the caption of the Form1 to MyClock in the properties window. Now add the Timer control to the form by dragging it from the ToolBox. Next, insert a label control into the form. Change the Font size of the label to 14 and set the text alignment to center. Set the Interval property of the Timer control to 1000, which reflects a one-second interval(1 unit is 1 millisecond).<\/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=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>Now, 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%; width: 70%;\">Label1.Text = TimeOfDay\r\n<\/pre>\n<p>To actually create the clock, click on the Timer control and insert 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 Label1.Text = TimeOfDay\r\nEnd Sub\r\n<\/pre>\n<p>The Clock is shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_1.jpg\" alt=\"\" width=\"243\" height=\"161\" \/><\/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=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>27.2 Creating a Stopwatch<\/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. Also, set the timer Enabled property to False so that it will not start ticking when the program is started. Insert three command buttons and change their names to StartBtn, StopBtn and ResetBtn respectively. Change their text to &#8220;Start&#8221;, &#8220;Stop&#8221; and &#8220;Reset&#8221; accordingly. Now, \u00a0key in the code as follows:<\/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'To increase one unit per second\r\n Label1.Text = Val(Label1.Text) + 1\r\nEnd Sub\r\n\r\nPrivate Sub StopBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopBtn.Click\r\n'To stop the Timer\r\n Timer1.Enabled = False\r\nEnd Sub\r\n\r\nPrivate Sub StartBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click\r\n'To start the Timer\r\n Timer1.Enabled = True\r\nEnd Sub\r\n\r\nPrivate Sub ResetBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetBtn.Click\r\n'To reset the Timer to 0\r\n Label1.Text = 0\r\nEnd Sub\r\n<\/pre>\n<p>The Interface of the Stopwatch is as shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_2.jpg\" alt=\"\" width=\"300\" height=\"300\" \/><\/p>\n<h3>27.3 Creating a Digital Dice<\/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 statement<\/p>\n<pre style=\"font-size: 110%; width: 70%;\">n = Int(1 + Rnd() * 6)\r\n<\/pre>\n<p>generates integers from 1 to 6 randomly.<\/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.<\/p>\n<p>The code is shown below:<\/p>\n<pre style=\"font-size: 110%; width: 70%;\">Public Class Form1\r\nDim n, m As Integer\r\nPrivate Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick\r\n m = m + 10\r\nIf m &lt; 1000 Then\r\n n = Int(1 + Rnd() * 6)\r\n LblDice.Text = n\r\nElse\r\n Timer1.Enabled = False\r\n m = 0\r\nEnd If\r\nEnd Sub\r\n\r\nPrivate Sub RollDiceBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RollDiceBtn.Click\r\n Timer1.Enabled = True\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p>Running the program produces a dice with fast-changing numbers which stop at a certain number. The interface is shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_3.jpg\" alt=\"\" width=\"300\" height=\"300\" \/><br \/>\n<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<h4 style=\"text-align: center;\"><strong><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-26\/\">[Lesson 26]<\/a> <\/strong>&lt;&lt;\u00a0<strong><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-28\/\"> [Lesson 28]<\/a><\/strong><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 26] &lt;&lt;\u00a0[CONTENTS] &gt;&gt; [Lesson 28] In this lesson, we shall show you how to use the timer in Visual Basic 2012. The timer is used to control and manage events that are time-related. For example, you need the timer to create a clock, a stopwatch, a dice, animation and more. In order to create &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-27\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2012 Lesson 27\u2013 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-2585","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 27\u2013 Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This Visual Basic 2012 lesson illustrates the use of Timer 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_lesson27.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 27\u2013 Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This Visual Basic 2012 lesson illustrates the use of Timer in Visual Basic 2012\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.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:21:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_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-2012-lesson-27\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html\",\"name\":\"Visual Basic 2012 Lesson 27\u2013 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\/vb2012\/vb2012_lesson27.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_1.jpg\",\"datePublished\":\"2013-02-15T02:37:27+00:00\",\"dateModified\":\"2018-06-24T10:21:12+00:00\",\"description\":\"This Visual Basic 2012 lesson illustrates the use of Timer in Visual Basic 2012\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_1.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2012 Lesson 27\u2013 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 2012 Lesson 27\u2013 Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This Visual Basic 2012 lesson illustrates the use of Timer 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_lesson27.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2012 Lesson 27\u2013 Using Timer - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This Visual Basic 2012 lesson illustrates the use of Timer in Visual Basic 2012","og_url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.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:21:12+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_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-2012-lesson-27\/","url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html","name":"Visual Basic 2012 Lesson 27\u2013 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\/vb2012\/vb2012_lesson27.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_1.jpg","datePublished":"2013-02-15T02:37:27+00:00","dateModified":"2018-06-24T10:21:12+00:00","description":"This Visual Basic 2012 lesson illustrates the use of Timer in Visual Basic 2012","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html#primaryimage","url":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/vb2010_lesson27_1.jpg"},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson27.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2012 Lesson 27\u2013 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\/2585","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=2585"}],"version-history":[{"count":33,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2585\/revisions"}],"predecessor-version":[{"id":13119,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2585\/revisions\/13119"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=2585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=2585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=2585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}