locked
Text Change RRS feed

  • Question

  • How to I make a text label change through code then wait a few seconds then change again?

    Please respond if you can help me out as I am new to this

    Sunday, March 22, 2020 12:21 AM

Answers

  • Hi

    Try this example.

    Setthe period of time between text changes by setting the Timer1 Interval to required (1000ms = every 1 second).

    This example needs a blank default Form1, and copy/replace the default code with this code.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Label1 As New Label
      Dim WithEvents Timer1 As New Timer
    
      ' define list of items for this example
      Dim sample As New List(Of String)({"Sample 1", "Sample 2", "Sample 3"})
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' add a Label to Form1
        With Label1
          .Text = Nothing
          .Location = New Point(20, 20)
          .AutoSize = True
          .Font = New Font("Arial", 14)
          .BorderStyle = BorderStyle.Fixed3D
        End With
        Controls.Add(Label1)
    
        With Timer1
          ' call every 1 second
          .Interval = 1000
          .Enabled = True
        End With
    
      End Sub
      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Static x As Integer = 0
        Label1.Text = sample(x)
        x += 1
        If x > sample.Count - 1 Then x = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, March 22, 2020 12:35 AM
    • Marked as answer by SnageDev Sunday, March 22, 2020 12:37 AM
    • Unmarked as answer by SnageDev Sunday, March 22, 2020 1:15 AM
    • Marked as answer by SnageDev Sunday, March 22, 2020 3:07 AM
    Sunday, March 22, 2020 12:34 AM

All replies

  • Using Task.Delay(X) where X is milliseconds to delay, in the following example there is a two second delay.

    One button named Button1, one Label named label1

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) _
        Handles Button1.Click
    
        Label1.Visible = Not Label1.Visible
        Await Task.Delay(2000)
        Label1.Visible = Not Label1.Visible
    
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 22, 2020 12:26 AM
  • By using this will I need a button and I need full instructions on how to do this? 

    I Would not like to use a button I want it to do it on app open

    • Edited by SnageDev Sunday, March 22, 2020 12:33 AM
    Sunday, March 22, 2020 12:29 AM
  • Hi

    Try this example.

    Setthe period of time between text changes by setting the Timer1 Interval to required (1000ms = every 1 second).

    This example needs a blank default Form1, and copy/replace the default code with this code.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Label1 As New Label
      Dim WithEvents Timer1 As New Timer
    
      ' define list of items for this example
      Dim sample As New List(Of String)({"Sample 1", "Sample 2", "Sample 3"})
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' add a Label to Form1
        With Label1
          .Text = Nothing
          .Location = New Point(20, 20)
          .AutoSize = True
          .Font = New Font("Arial", 14)
          .BorderStyle = BorderStyle.Fixed3D
        End With
        Controls.Add(Label1)
    
        With Timer1
          ' call every 1 second
          .Interval = 1000
          .Enabled = True
        End With
    
      End Sub
      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Static x As Integer = 0
        Label1.Text = sample(x)
        x += 1
        If x > sample.Count - 1 Then x = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, March 22, 2020 12:35 AM
    • Marked as answer by SnageDev Sunday, March 22, 2020 12:37 AM
    • Unmarked as answer by SnageDev Sunday, March 22, 2020 1:15 AM
    • Marked as answer by SnageDev Sunday, March 22, 2020 3:07 AM
    Sunday, March 22, 2020 12:34 AM
  • Oh no got a error

    at line 9 it says

    Event 'Load' cannot be found

    at line 18 it says 

    'Add' is not a member of 'Control'

    CALL STACK

    at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
    at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    --- End of stack trace from previous location where exception was thrown ---
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host) 

    Sunday, March 22, 2020 12:48 AM
  • Hi

    There is no reason for any errors. Are you using VB.NET? Are you using a Windows Form Project?  Did you follow the instructions I poswted EXACTLY?


    Regards Les, Livingston, Scotland

    Sunday, March 22, 2020 1:30 AM
  • I am using a Windows Form Project sorry for late response but where do I insert script?


    • Edited by SnageDev Sunday, March 22, 2020 3:07 AM
    Sunday, March 22, 2020 2:16 AM
  • THANKS

    Thanks Leshay for the help as I have found that I was doing the method wrong I just had to re read your message and I got it!! Thanks soo much

    Sunday, March 22, 2020 3:10 AM
  • By using this will I need a button and I need full instructions on how to do this? 

    I Would not like to use a button I want it to do it on app open

    A button is only a simple way to show an example and also you didn't say how you wanted to use this in a specific context.

    Here is a non-button example. Here I use form shown event as the load event sometimes eats exceptions and not report them.

    If the hour is 8 PM run the for-next.

    Private Async Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
        If Now.Hour = 20 Then
            For index = 0 To 5
                Label1.Visible = Not Label1.Visible
                Await Task.Delay(1000)
                Console.WriteLine(index)
                Label1.Visible = Not Label1.Visible
                Await Task.Delay(1000)
            Next
        End If
    
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 22, 2020 3:49 AM