-
Dec 25th, 2012, 08:58 AM
#1
Thread Starter
Junior Member
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
-
Dec 25th, 2012, 11:02 AM
#2
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:
Public Class Form1 Private labels As Label() Private texts As String() = {"My name", "is", "Ali"} Private labelIndex As Integer = 2 Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown labels = {Label1, Label2, Label3} Timer1.Start() End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick For i = labelIndex To labels.GetUpperBound(0) labels(i).Text = texts(i - labelIndex) Next If labelIndex = 0 Then Timer1.Stop() Else labelIndex -= 1 End If End Sub End Class
-
Dec 25th, 2012, 11:26 AM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 25th, 2012, 12:32 PM
#4
Thread Starter
Junior Member
Re: Need Help: Shifting text in labels
Originally Posted by jmcilhinney
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:
Public Class Form1 Private labels As Label() Private texts As String() = {"My name", "is", "Ali"} Private labelIndex As Integer = 2 Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown labels = {Label1, Label2, Label3} Timer1.Start() End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick For i = labelIndex To labels.GetUpperBound(0) labels(i).Text = texts(i - labelIndex) Next If labelIndex = 0 Then Timer1.Stop() Else labelIndex -= 1 End If End Sub 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.
-
Dec 25th, 2012, 12:33 PM
#5
Thread Starter
Junior Member
Re: Need Help: Shifting text in labels
Originally Posted by .paul.
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
-
Dec 25th, 2012, 12:35 PM
#6
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.
-
Dec 25th, 2012, 12:39 PM
#7
Thread Starter
Junior Member
Re: Need Help: Shifting text in labels
Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|