Results 1 to 8 of 8

Thread: Label Text

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Label Text

    Hello to the community!

    I am trying to figure out how i can make a "spelling text" in a label...
    I have written a text to a label (i.e "Hello World") and I want to display one letter per second when the main form is loaded.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Label Text

    You would create a string variable to hold the text you wanted to display. Second, add a timer to the form and set the intervale to 1000 (since it's milliseconds). In the timer tick event handler, update the text of the label.

    There are several ways to do the last step, but probably the easiest is to use Substring. To do this, have a counter variable at form scope. Every time the timer ticks, increase this counter by one. The code to fill the label would then look like this:

    yourLabel.Text = yourString.Substring(0,counter)

    There are variations on that which might be more pleasing. The way I showed it, I think you will end up with two letters to start with, then add one each time. You might start the counter at -1 to get it letter by letter, or you might start with the first letter before the timer ticks.
    My usual boring signature: Nothing

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Label Text

    I was going to suggest something similar to what Shaggy suggested, only I would store the String value that you wish to show in the Label's Tag property. Here is an example:
    Code:
    Public Class Form1
    
        Private lblDisplay As Label
        Private tmrDisplay As Windows.Forms.Timer
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            'Create the controls
            lblDisplay = New Label With {.AutoSize = True, .Location = New Point(5, 5) .Tag = "Some String", .Text = String.Empty}
            tmrDisplay = New Timer With {.Enabled = True, .Interval = 1000, .Tag = -1}
    
            'Add the Label to the Form
            Me.Controls.Add(lblDisplay)
    
            'Create an instance of the Timer's Tick event
            AddHandler tmrDisplay.Tick, AddressOf tmrDisplay_Tick
        End Sub
    
        Private Sub tmrDisplay_Tick(ByVal sender As Object, ByVal e As EventArgs)
            'Increment the Tag by 1 each tick
            tmrDisplay.Tag = Convert.ToInt32(tmrDisplay.Tag) + 1
    
            If Convert.ToInt32(tmrDisplay.Tag) = lblDisplay.Tag.ToString.Length - 1 Then
                'If the Tag is the desired String Length then stop the Timer and reset the counter
                tmrDisplay.Enabled = False
                tmrDisplay.Tag = -1
            Else
                'Otherwise display the Text equal to the Substring from 0 until the counter
                lblDisplay.Text = lblDisplay.Tag.ToString.SubString(0, Convert.ToInt32(tmrDisplay.Tag))
                lblDisplay.Refresh()
            End If
        End Sub
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Label Text

    Thanks for the response guys but I am doing something wrong... It's not working...
    @Shaggy. How do I increase the counter by one when the timer ticks...?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Label Text

    I'd just use:

    yourCounterNameHere += 1

    What DDay posted is a pretty complete setup. I don't necessarily agree with keeping the counter in the .Tag property rather than as a separate variable, but I do like the fact that he's stopping the timer when the length of the string is reached. That's a nice touch.

    If that code isn't working, then is it giving you an error message? If not, then put a breakpoint in the Tick Event handler and see whether or not it is being hit.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Label Text

    It seeems there was a small issuee in the above code... A missing comma poped up an error.
    "lblDisplay = New Label With {.AutoSize = True, .Location = New Point(5, 5), .Tag = "Some String", .Text = String.Empty}"

    I still have a couple of question...

    1) As I see, the above code adds a couple of controls (Timer, Label). When I already have those controls (Timer1, Label1) and I fix the above code, it doesn't seem to work... Here is my code


    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            'I have added manually to the Form1 two controls: Timer1 ,Label1
            'Label1 properties: Tag: "Hello World", Text: empty
            
           
            AddHandler Timer1.Tick, AddressOf Timer1_Tick
        End Sub
    
       Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
            'I have set those values to the Properties of the Timer1 : Tag=-1, Interval=1000
            Timer1.Tag = Convert.ToInt32(Timer1.Tag) + 1
    
            If Convert.ToInt32(Timer1.Tag) = Label1.Tag.ToString.Length - 1 Then
                Timer1.Enabled = False
                Timer1.Tag = -1
    
            Else
                Timer1.Text = Label1.Tag.ToString.SubString(0, Convert.ToInt32(Timer1.Tag))
                Label1.Refresh()
            End If
        End Sub
    
    End Class
    2) @dday9
    Your code works fine, but for some reason it doesn't display the whole text "Some String". It displays only ""Some Stri".
    You have set AutoSize=True, so I suppose the Label has the same size as the String value... Why it doesn't work?

    3) If I want to display the Text a little bit faster, do I change the Interval of the Timer or do I have to change something else too?

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Label Text

    1) As I see, the above code adds a couple of controls (Timer, Label). When I already have those controls (Timer1, Label1) and I fix the above code, it doesn't seem to work... Here is my code
    You can use your own controls, I like to dynamically declare mine in examples so that users know exactly what controls I'm using and what properties I have set.

    2) @dday9
    Your code works fine, but for some reason it doesn't display the whole text "Some String". It displays only ""Some Stri".
    You have set AutoSize=True, so I suppose the Label has the same size as the String value... Why it doesn't work?
    I was free-typing everything(hence the missing comma too), what should probably be happening in the Timer's tick is:
    Code:
            Timer1.Tag = Convert.ToInt32(Timer1.Tag) + 1
    
            If Convert.ToInt32(Timer1.Tag) = Label1.Tag.ToString.Length + 1 Then
                Timer1.Enabled = False
                Timer1.Tag = -1
    
            Else
                Timer1.Text = Label1.Tag.ToString.SubString(0, Convert.ToInt32(Timer1.Tag) + 1)
                Label1.Refresh()
            End If
    3) If I want to display the Text a little bit faster, do I change the Interval of the Timer or do I have to change something else too?
    Nope, just lower the Interval.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Label Text

    Thanks guys! Works brilliant!
    Now I am trying to embed a sound file to my project (I want it to play with the standalone .exe file).
    I will come back again, if I need help

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width