How to change the label text every 3 seconds using a timer VB.NET












-2















Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
Dim timing As Boolean = True
Dim i As Integer = 0
Dim n As Integer = 3000
Private Sub Button1_Click(sender As Object, e As EventArgs)
Timer1.Start()
For i = 0 To 11
While timing = True
If Timer1.Interval = n Then
Lbl_Word.Text = arr(i)
timing = False
End If
End While
timing = True
Next
Timer1.Stop()
End Sub


The text of the label isn't changing and i'm not sure what I've done wrong. Please help.










share|improve this question























  • What is Timer1? You should use a System.Windows.Forms.Timer and it's Tick event to update the UI.

    – Rango
    Nov 27 '18 at 13:44













  • Thats not how Timers work and you should set Option Strict On

    – None of the Above
    Nov 27 '18 at 20:04
















-2















Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
Dim timing As Boolean = True
Dim i As Integer = 0
Dim n As Integer = 3000
Private Sub Button1_Click(sender As Object, e As EventArgs)
Timer1.Start()
For i = 0 To 11
While timing = True
If Timer1.Interval = n Then
Lbl_Word.Text = arr(i)
timing = False
End If
End While
timing = True
Next
Timer1.Stop()
End Sub


The text of the label isn't changing and i'm not sure what I've done wrong. Please help.










share|improve this question























  • What is Timer1? You should use a System.Windows.Forms.Timer and it's Tick event to update the UI.

    – Rango
    Nov 27 '18 at 13:44













  • Thats not how Timers work and you should set Option Strict On

    – None of the Above
    Nov 27 '18 at 20:04














-2












-2








-2








Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
Dim timing As Boolean = True
Dim i As Integer = 0
Dim n As Integer = 3000
Private Sub Button1_Click(sender As Object, e As EventArgs)
Timer1.Start()
For i = 0 To 11
While timing = True
If Timer1.Interval = n Then
Lbl_Word.Text = arr(i)
timing = False
End If
End While
timing = True
Next
Timer1.Stop()
End Sub


The text of the label isn't changing and i'm not sure what I've done wrong. Please help.










share|improve this question














Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
Dim timing As Boolean = True
Dim i As Integer = 0
Dim n As Integer = 3000
Private Sub Button1_Click(sender As Object, e As EventArgs)
Timer1.Start()
For i = 0 To 11
While timing = True
If Timer1.Interval = n Then
Lbl_Word.Text = arr(i)
timing = False
End If
End While
timing = True
Next
Timer1.Stop()
End Sub


The text of the label isn't changing and i'm not sure what I've done wrong. Please help.







arrays vb.net timer boolean






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 13:39









LorellLorell

31




31













  • What is Timer1? You should use a System.Windows.Forms.Timer and it's Tick event to update the UI.

    – Rango
    Nov 27 '18 at 13:44













  • Thats not how Timers work and you should set Option Strict On

    – None of the Above
    Nov 27 '18 at 20:04



















  • What is Timer1? You should use a System.Windows.Forms.Timer and it's Tick event to update the UI.

    – Rango
    Nov 27 '18 at 13:44













  • Thats not how Timers work and you should set Option Strict On

    – None of the Above
    Nov 27 '18 at 20:04

















What is Timer1? You should use a System.Windows.Forms.Timer and it's Tick event to update the UI.

– Rango
Nov 27 '18 at 13:44







What is Timer1? You should use a System.Windows.Forms.Timer and it's Tick event to update the UI.

– Rango
Nov 27 '18 at 13:44















Thats not how Timers work and you should set Option Strict On

– None of the Above
Nov 27 '18 at 20:04





Thats not how Timers work and you should set Option Strict On

– None of the Above
Nov 27 '18 at 20:04












3 Answers
3






active

oldest

votes


















0














If you are using a forms timer then the code to update the label must be in the .Tick event. The code below uses a static variable in the event handler to track which string to show.



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 3000
If Not Timer1.Enabled Then 'already running?
Timer1.Start() 'no
End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
Static idx As Integer = 0 'which index in arr
If idx < arr.Length Then 'more to show?
'yes
Lbl_Word.Text = arr(idx)
idx += 1
Else
'no
Timer1.Stop()
idx = 0
End If
End Sub


An alternative approach without using a timer.



Private TmrTask As task
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TmrTask Is Nothing OrElse TmrTask.IsCompleted Then
TmrTask = Task.Run(Sub() MyTimerTask())
End If
End Sub

Private Sub MyTimerTask()
Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
For Each s As String In arr
Me.BeginInvoke(Sub()
Lbl_Word.Text = s
End Sub)
Threading.Thread.Sleep(3000)
Next
End Sub





share|improve this answer
























  • It worked, thank you, you just saved my project!

    – Lorell
    Nov 27 '18 at 18:37











  • @Lorell - mark this as the answer.

    – dbasnett
    Nov 27 '18 at 18:43



















0














you have to put the code inside the Timer events. not in the Button Click event.
and in the Button click event just enable the timer.






share|improve this answer































    0














    -Add a timer




    • Make the interval 3000

    • On the timer event change the label background color to the color you want you can use from RGB or use random numbers






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53501042%2fhow-to-change-the-label-text-every-3-seconds-using-a-timer-vb-net%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If you are using a forms timer then the code to update the label must be in the .Tick event. The code below uses a static variable in the event handler to track which string to show.



      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Timer1.Interval = 3000
      If Not Timer1.Enabled Then 'already running?
      Timer1.Start() 'no
      End If
      End Sub

      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
      Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
      'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
      Static idx As Integer = 0 'which index in arr
      If idx < arr.Length Then 'more to show?
      'yes
      Lbl_Word.Text = arr(idx)
      idx += 1
      Else
      'no
      Timer1.Stop()
      idx = 0
      End If
      End Sub


      An alternative approach without using a timer.



      Private TmrTask As task
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      If TmrTask Is Nothing OrElse TmrTask.IsCompleted Then
      TmrTask = Task.Run(Sub() MyTimerTask())
      End If
      End Sub

      Private Sub MyTimerTask()
      Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
      'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
      For Each s As String In arr
      Me.BeginInvoke(Sub()
      Lbl_Word.Text = s
      End Sub)
      Threading.Thread.Sleep(3000)
      Next
      End Sub





      share|improve this answer
























      • It worked, thank you, you just saved my project!

        – Lorell
        Nov 27 '18 at 18:37











      • @Lorell - mark this as the answer.

        – dbasnett
        Nov 27 '18 at 18:43
















      0














      If you are using a forms timer then the code to update the label must be in the .Tick event. The code below uses a static variable in the event handler to track which string to show.



      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Timer1.Interval = 3000
      If Not Timer1.Enabled Then 'already running?
      Timer1.Start() 'no
      End If
      End Sub

      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
      Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
      'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
      Static idx As Integer = 0 'which index in arr
      If idx < arr.Length Then 'more to show?
      'yes
      Lbl_Word.Text = arr(idx)
      idx += 1
      Else
      'no
      Timer1.Stop()
      idx = 0
      End If
      End Sub


      An alternative approach without using a timer.



      Private TmrTask As task
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      If TmrTask Is Nothing OrElse TmrTask.IsCompleted Then
      TmrTask = Task.Run(Sub() MyTimerTask())
      End If
      End Sub

      Private Sub MyTimerTask()
      Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
      'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
      For Each s As String In arr
      Me.BeginInvoke(Sub()
      Lbl_Word.Text = s
      End Sub)
      Threading.Thread.Sleep(3000)
      Next
      End Sub





      share|improve this answer
























      • It worked, thank you, you just saved my project!

        – Lorell
        Nov 27 '18 at 18:37











      • @Lorell - mark this as the answer.

        – dbasnett
        Nov 27 '18 at 18:43














      0












      0








      0







      If you are using a forms timer then the code to update the label must be in the .Tick event. The code below uses a static variable in the event handler to track which string to show.



      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Timer1.Interval = 3000
      If Not Timer1.Enabled Then 'already running?
      Timer1.Start() 'no
      End If
      End Sub

      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
      Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
      'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
      Static idx As Integer = 0 'which index in arr
      If idx < arr.Length Then 'more to show?
      'yes
      Lbl_Word.Text = arr(idx)
      idx += 1
      Else
      'no
      Timer1.Stop()
      idx = 0
      End If
      End Sub


      An alternative approach without using a timer.



      Private TmrTask As task
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      If TmrTask Is Nothing OrElse TmrTask.IsCompleted Then
      TmrTask = Task.Run(Sub() MyTimerTask())
      End If
      End Sub

      Private Sub MyTimerTask()
      Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
      'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
      For Each s As String In arr
      Me.BeginInvoke(Sub()
      Lbl_Word.Text = s
      End Sub)
      Threading.Thread.Sleep(3000)
      Next
      End Sub





      share|improve this answer













      If you are using a forms timer then the code to update the label must be in the .Tick event. The code below uses a static variable in the event handler to track which string to show.



      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Timer1.Interval = 3000
      If Not Timer1.Enabled Then 'already running?
      Timer1.Start() 'no
      End If
      End Sub

      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
      Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
      'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
      Static idx As Integer = 0 'which index in arr
      If idx < arr.Length Then 'more to show?
      'yes
      Lbl_Word.Text = arr(idx)
      idx += 1
      Else
      'no
      Timer1.Stop()
      idx = 0
      End If
      End Sub


      An alternative approach without using a timer.



      Private TmrTask As task
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      If TmrTask Is Nothing OrElse TmrTask.IsCompleted Then
      TmrTask = Task.Run(Sub() MyTimerTask())
      End If
      End Sub

      Private Sub MyTimerTask()
      Dim arr() As String = {"Cake", "Mouse", "Heat", "Tent", "Boots", "Pen", "Stairs", "Cube", "Lion", "Cookies", "Ruler", "Ink"}
      'Dim arr() As String = {"Cake", "Mouse", "Heat"} 'to test
      For Each s As String In arr
      Me.BeginInvoke(Sub()
      Lbl_Word.Text = s
      End Sub)
      Threading.Thread.Sleep(3000)
      Next
      End Sub






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 27 '18 at 14:22









      dbasnettdbasnett

      8,17721928




      8,17721928













      • It worked, thank you, you just saved my project!

        – Lorell
        Nov 27 '18 at 18:37











      • @Lorell - mark this as the answer.

        – dbasnett
        Nov 27 '18 at 18:43



















      • It worked, thank you, you just saved my project!

        – Lorell
        Nov 27 '18 at 18:37











      • @Lorell - mark this as the answer.

        – dbasnett
        Nov 27 '18 at 18:43

















      It worked, thank you, you just saved my project!

      – Lorell
      Nov 27 '18 at 18:37





      It worked, thank you, you just saved my project!

      – Lorell
      Nov 27 '18 at 18:37













      @Lorell - mark this as the answer.

      – dbasnett
      Nov 27 '18 at 18:43





      @Lorell - mark this as the answer.

      – dbasnett
      Nov 27 '18 at 18:43













      0














      you have to put the code inside the Timer events. not in the Button Click event.
      and in the Button click event just enable the timer.






      share|improve this answer




























        0














        you have to put the code inside the Timer events. not in the Button Click event.
        and in the Button click event just enable the timer.






        share|improve this answer


























          0












          0








          0







          you have to put the code inside the Timer events. not in the Button Click event.
          and in the Button click event just enable the timer.






          share|improve this answer













          you have to put the code inside the Timer events. not in the Button Click event.
          and in the Button click event just enable the timer.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 13:44









          Pda FoxPda Fox

          11




          11























              0














              -Add a timer




              • Make the interval 3000

              • On the timer event change the label background color to the color you want you can use from RGB or use random numbers






              share|improve this answer






























                0














                -Add a timer




                • Make the interval 3000

                • On the timer event change the label background color to the color you want you can use from RGB or use random numbers






                share|improve this answer




























                  0












                  0








                  0







                  -Add a timer




                  • Make the interval 3000

                  • On the timer event change the label background color to the color you want you can use from RGB or use random numbers






                  share|improve this answer















                  -Add a timer




                  • Make the interval 3000

                  • On the timer event change the label background color to the color you want you can use from RGB or use random numbers







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 6 '18 at 6:05









                  Nick

                  33.6k132042




                  33.6k132042










                  answered Nov 27 '18 at 13:47









                  Sami KaraeenSami Karaeen

                  12




                  12






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53501042%2fhow-to-change-the-label-text-every-3-seconds-using-a-timer-vb-net%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)