Results 1 to 7 of 7

Thread: Need Help: Shifting text in labels

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    20

    Need Help: Shifting text in labels

    I was doing this using VB6 the old days. But I really can't find a way for it using VB.NET.
    The idea is to shift a text or caption of a label into the next label.

    just like in this simple example:

    Label Name >>>>>> Caption/Text

    LBOut1 >>>>>> (NULL/No caption)
    LBOut2 >>>>>> (NULL/No caption)
    LBOut3 >>>>>> My name

    after a second or specific time (using a timer)

    LBOut1 >>>>>> (NULL/No caption)
    LBOut2 >>>>>> My name
    LBOut3 >>>>>> is

    after a second or specific time (using a timer)

    LBOut1 >>>>>> My name
    LBOut2 >>>>>> is
    LBOut3 >>>>>> Ali

    I used to do it using vb6 by creating a label then just copying and pasting two times to create another labels with an array.
    and using this code to display them by shifting:

    For shift = 2 To 0 Step -1 'Shift previous hex strings up on the display
    LBOUT(shift + 1).Caption = lbOUT(shift).Caption


    Next shift

    I really can't find a way using vb.net. I had this problem for 5days now, and I'm disappointed in my skill
    Any help from you guys is much appreciated. Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Need Help: Shifting text in labels

    You wouldn't use a loop for a start. You would use a Timer, which you can add to the form in the designer, and then handle its Tick event. If you want the delay between movements to be 1 second then you set the Interval of the Timer to 1000 (milliseconds). Here's one implementation of what you're asking for:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private labels As Label()
    4.     Private texts As String() = {"My name", "is", "Ali"}
    5.     Private labelIndex As Integer = 2
    6.  
    7.     Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    8.         labels = {Label1, Label2, Label3}
    9.         Timer1.Start()
    10.     End Sub
    11.  
    12.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    13.         For i = labelIndex To labels.GetUpperBound(0)
    14.             labels(i).Text = texts(i - labelIndex)
    15.         Next
    16.  
    17.         If labelIndex = 0 Then
    18.             Timer1.Stop()
    19.         Else
    20.             labelIndex -= 1
    21.         End If
    22.     End Sub
    23.  
    24. End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Need Help: Shifting text in labels

    try this:

    Code:
    Public Class Form1
    
        Dim firstUsed As Integer = 2
        Dim labels() As Label
        Dim strings() As String = {"My name", "is", "Paul"}
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            labels = New Label() {Label1, Label2, Label3}
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            For x As Integer = 0 To firstUsed
                labels(x).Text = ""
            Next
            For x As Integer = firstUsed To labels.GetUpperBound(0)
                labels(x).Text = strings(x - firstUsed)
            Next
            firstUsed = If(firstUsed = 0, 2, firstUsed - 1)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = True
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    20

    Re: Need Help: Shifting text in labels

    Quote Originally Posted by jmcilhinney View Post
    You wouldn't use a loop for a start. You would use a Timer, which you can add to the form in the designer, and then handle its Tick event. If you want the delay between movements to be 1 second then you set the Interval of the Timer to 1000 (milliseconds). Here's one implementation of what you're asking for:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private labels As Label()
    4.     Private texts As String() = {"My name", "is", "Ali"}
    5.     Private labelIndex As Integer = 2
    6.  
    7.     Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    8.         labels = {Label1, Label2, Label3}
    9.         Timer1.Start()
    10.     End Sub
    11.  
    12.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    13.         For i = labelIndex To labels.GetUpperBound(0)
    14.             labels(i).Text = texts(i - labelIndex)
    15.         Next
    16.  
    17.         If labelIndex = 0 Then
    18.             Timer1.Stop()
    19.         Else
    20.             labelIndex -= 1
    21.         End If
    22.     End Sub
    23.  
    24. End Class
    Thanks for the info.
    I tried it and there is an error at the line number 8. it says Error 1 Expression expected.
    But any way I got what you meant. Now I have to figure out how to include this code into my program for receiving data from a COM Port, the data that will be inputted there is an array of bytes.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    20

    Re: Need Help: Shifting text in labels

    Quote Originally Posted by .paul. View Post
    try this:

    Code:
    Public Class Form1
    
        Dim firstUsed As Integer = 2
        Dim labels() As Label
        Dim strings() As String = {"My name", "is", "Paul"}
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            labels = New Label() {Label1, Label2, Label3}
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            For x As Integer = 0 To firstUsed
                labels(x).Text = ""
            Next
            For x As Integer = firstUsed To labels.GetUpperBound(0)
                labels(x).Text = strings(x - firstUsed)
            Next
            firstUsed = If(firstUsed = 0, 2, firstUsed - 1)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = True
        End Sub
    
    End Class
    That was one good example. Thank you so much. Runs like a Charm.
    SOLVED!

    Thank you again.
    Next step, Integrating with my project to read a HEX String from my Bill Acceptor

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Need Help: Shifting text in labels

    I ran that code myself and it worked. If you mean this line:
    Code:
    labels = {Label1, Label2, Label3}
    then that will require VB 2010 or later to compile. In earlier versions you'd require this:
    Code:
    labels = New Label() {Label1, Label2, Label3}
    You'd also need something similar for the String array.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    20

    Re: Need Help: Shifting text in labels

    Quote Originally Posted by jmcilhinney View Post
    I ran that code myself and it worked. If you mean this line:
    Code:
    labels = {Label1, Label2, Label3}
    then that will require VB 2010 or later to compile. In earlier versions you'd require this:
    Code:
    labels = New Label() {Label1, Label2, Label3}
    You'd also need something similar for the String array.
    ohh I see.
    I used 2008. For faster creation. I tried it using 2010 and it worked fine now. thank you so much.
    it was easier using vb6. just copy and paste and you will have an array for labels, textboxes, etc, hehehe..

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