C# .NET - ANIMATE LABEL'S POSITION PROGRAMMATICALLY...C# windows form

Asked By Warjie Malibago on 15-Jun-11 05:31 AM
hi,can someone help me with this? i want to animate (continuously move in a horizontal direction) my label when i open the form but it does not happen..this is my code:

            int height = Screen.PrimaryScreen.Bounds.Height;
            int width = Screen.PrimaryScreen.Bounds.Width;
            this.Size = new Size(width, height);
            for (int loc = width; width <= 0; width--)
            {
// lblCredits.Left -= 1;
                lblCredits.Location = new Point(loc, 5);
            }

whenever the form opens, the label's position get stuck on the last location the loop calculates (which is (0,5))..is my code wrong or where should i put the code? i call it on the Form_Load..thanks a lot
Riley K replied to Warjie Malibago on 15-Jun-11 05:36 AM
You can do it using a timer control

Add a timer to the form..

private int iScroll;
private string strString = "Your Text";


Inside timer click event put the below code

****

iScroll = iScroll+1;
int iLmt = strString.Length - iScroll;
if (iLmt < 20)
{
iScroll = 0;
}

string str = strString.Substring(iScroll,20);
label1.Text = str;

Thanks

Jitendra Faye replied to Warjie Malibago on 15-Jun-11 05:40 AM
This is because of -

When you run application , on that time the code which is written to animate Label, gets executed, and it complete its
execution so you find it at last calulated value.

for that you have to use delay while loading of form.

Write this code in one form and after animation open your main form.

Hope this will help you.


Warjie Malibago replied to Riley K on 15-Jun-11 05:42 AM
ok,now where should i put the code i made?

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            
            int height = Screen.PrimaryScreen.Bounds.Height;
            int width = Screen.PrimaryScreen.Bounds.Width;
            this.Size = new Size(width, height);
            lblCredits.Visible = true;
            lblCredits.Location = new Point(height, 5);
            for (int loc = height; height >= 0; height--)
            {
                lblCredits.Location = new Point(loc, 5);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            limit = lblCredits.Text;
            time = time + 1;
            int limit2 = limit.Length - time;
            if (time < 20)
            {
                time = 0;
            }
            string str = limit.Substring(time, 20);
            lblCredits.Text = str;
        }

how can i combine these codes?
Warjie Malibago replied to Jitendra Faye on 15-Jun-11 05:44 AM
what code should i write for that to happen?
Jitendra Faye replied to Warjie Malibago on 15-Jun-11 06:32 AM
For that  use Timer Control and Write code in Tick() event of Timer.

follow these steps-

1. Take one Timer Control in your page.

2. Set It's Enabled property to true.

3. Set It's Interval Property to 1000 (for 1 sec.)

4. Now Implement It's Tick() Event Like this-

protected void Timer1_Tick(object sender, EventArgs e)
{
       
        if(lblCredits.Left =>  400)  //give your last value

       Timer1.Enabled=False;     //stop animation when label reach to last point.
          }
       else
        {
            lblCredits.Left =   lblCredits.Left +1;
        }
  
 }


Try this code and let me know.