locked
visual studio label box + timers

    Question

  • Hi all

     

    How to Move a label box from left to right in the window and the vice versa using timer

    Also how to give random fore colors using timer control

     

     

    Thanks in advance

    Tuesday, March 25, 2008 11:03 AM

Answers

  • Sure,

    I'll do Wink

    Code Snippet
        REM Let's assume timMovement is your timer and lblBounce is your label.


        Dim Direction As Integer = 1 'Direction, this variable says which way the label is going.

        Private Sub timMovement_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timMovement.tick

            lblBounce.Left += Direction  'Move the label to the left (direction < 0) or right (direction > 0)

            If lblBounce.Left <= 0 Then  'If the label hits the left side of the form, change direction

                Direction = 1

            End If

            If lblBounce.Left + lblBounce.Width >= Me.Width Then  'If the label hits the right side of the form, change direction

                Direction = -1

            End If

        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            timMovement.Interval = 1
            timMovement.Enabled = True
        End Sub

    I hope this helped!




    Tuesday, March 25, 2008 12:14 PM
  • No, not like that. It's coming close though. Try this:

    Code Snippet

    lblBounce.ForeColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)


    You are supposed to ask this in a new topic, but I think this still fits in this one Stick out tongue
    Tuesday, March 25, 2008 1:15 PM

All replies

  • In the timers tick event handler you increment or decrement the labels Left property.

     

     

     

    Tuesday, March 25, 2008 11:32 AM
  • Thanks for your reply dave

    Can u help me with some sample code.

     

    Thanks

    Tuesday, March 25, 2008 11:48 AM
  • Sure,

    I'll do Wink

    Code Snippet
        REM Let's assume timMovement is your timer and lblBounce is your label.


        Dim Direction As Integer = 1 'Direction, this variable says which way the label is going.

        Private Sub timMovement_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timMovement.tick

            lblBounce.Left += Direction  'Move the label to the left (direction < 0) or right (direction > 0)

            If lblBounce.Left <= 0 Then  'If the label hits the left side of the form, change direction

                Direction = 1

            End If

            If lblBounce.Left + lblBounce.Width >= Me.Width Then  'If the label hits the right side of the form, change direction

                Direction = -1

            End If

        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            timMovement.Interval = 1
            timMovement.Enabled = True
        End Sub

    I hope this helped!




    Tuesday, March 25, 2008 12:14 PM
  • Hi Tierro

    Thanks a lot

    Its working.

     

    Do u know how to give random forecolors using timer control.

     

    In general i heard it is like

     

    label1.forecolor=RGB(rnd*255,rnd*255,Rnd*255)

    but its not working

     

     

    Tuesday, March 25, 2008 12:51 PM
  • No, not like that. It's coming close though. Try this:

    Code Snippet

    lblBounce.ForeColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)


    You are supposed to ask this in a new topic, but I think this still fits in this one Stick out tongue
    Tuesday, March 25, 2008 1:15 PM
  • Hi Tierro

     

    The following code you gave for random fore color is working.

     

    lblBounce.ForeColor = Color.FromArgb(Rnd() * 255, Rnd() * 255, Rnd() * 255)

     

    in your code snippet you have given code as follows

    only three arguments allowed.

    lblBounce.ForeColor = Color.FromArgb( 255, Rnd() * 255, Rnd() * 255, Rnd() * 255)

     

    Thanks

    Wednesday, March 26, 2008 5:49 AM
  • Doesn't the one with the four arguments work? That's really wierd!

    The First argument should indicate the opacity of the color. The other three represent red, green and blue. But since you can't do that, I'm really wondering what version of visual basic you are using!
    Wednesday, March 26, 2008 7:14 AM
  •  

    I am using VB.NET 2003

     

    It shows error with the number of argument and the syntax is

     

    Public shared function FromArgb(red as integer,green as integer,blue as integer)

     

    so i deleted first argument then it works.

    Wednesday, March 26, 2008 7:58 AM
  •  suganzeni wrote:

     

    I am using VB.NET 2003

     

    It shows error with the number of argument and the syntax is

     

    Public shared function FromArgb(red as integer,green as integer,blue as integer)

     

    so i deleted first argument then it works.



    Ah, it's in VB'03, that clears up a lot. Though I never knew the alpha channel wasn't in there yet Tongue Tied never mind.
    Wednesday, March 26, 2008 7:45 PM