1

I need to change the position of a window on mouse click. here is the code.

private void Button_Click(object sender, RoutedEventArgs e)
{
    for(int i=0; i<50; i++)
    {
        this.Top -= i; 
        this.Left -= i;
    } 
}

But whenever i run this program only the last position is shown. My intention is to move it continuosly till the end of loop.

ASh
  • 34,632
  • 9
  • 60
  • 82
Noxious Reptile
  • 838
  • 1
  • 7
  • 24
  • The window moves untill the loop has ended this loop is executed so fast that you cant see the window moving, if that's what you want add some delay. – Sybren Sep 29 '15 at 13:38
  • It will display last position because there is no time interval it does all the execution in for loop. – sumeet kumar Sep 29 '15 at 13:38
  • Wpf already has powerful animation support. Why innovate the wheel? If you want, use `DispatcherTimer` and change something on each tick. But you will never have beautiful and smooth animation (as e.g. [here](http://stackoverflow.com/a/31919365/1997232)). – Sinatr Sep 29 '15 at 13:59

3 Answers3

2

Finally i found the answer myself. It s working perfectly as i expected. I used SynchronizationContext which can post Actions to update controls on UI thread.

    public partial class Splash : Window
    {
        SynchronizationContext sc;
        System.Timers.Timer t;
        double i=0;
        double tempTop;
        double angle = 0;
        public Splash()
        {
            InitializeComponent();
            sc=SynchronizationContext.Current;
        }
        private void Move(object sender, MouseEventArgs e)
        {
            DragMove();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btnMinim_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

                l1.Content = "Helicopter Moving";
            if(t!=null)
            {
                t.Stop();
                t.Dispose();
            }
                //for (double i = 0; i < 1; i += 0.05)
                //{
                //    this.Top -= i;
                //    this.Left -= i;
                //    Thread.Sleep(100);
                //}
                //l1.Content = "Helicopter Stopped";
                tempTop = this.Top;
                t = new System.Timers.Timer();
                t.Interval = 10;
                t.Enabled = true;
                t.Elapsed += Change;
                t.Start();

        }
        void Change(object sender, EventArgs e)
        {
            if (i <= 3)
            {
                sc.Post(o =>
                {
                    this.Top = tempTop * (Math.Cos(Math.PI * angle / 180));
                    this.Left -= i;
                    angle = (angle >= 360) ? 0 : ++angle;
                    i = i + 0.01;
                }, null);
            }
            else
            {
                t.Stop();
                i = i * -1;
            }

        }
    }
}
Noxious Reptile
  • 838
  • 1
  • 7
  • 24
1

Try this should work Thread.Sleep will not work for you as its a UI thread. You need timer to make this work

Timer t; 
private void Button_Click(object sender, RoutedEventArgs e)
{

    i=0;
    if(t!=null)
    { 
       t.Stop();
       t.Dispose();
    }
    t = new Timer();
    t.Interval = 800;
    t.Enabled = true;
    t.Tick += T_Tick;
    t.Start();                                                    

}
int i=0;
private static void T_Tick(object sender, EventArgs e)
{
   if(i<=50)
   {
        this.Top -= i; 
        this.Left -= i;
        i++;
    }
    else
     t.Stop();   
}        
sumeet kumar
  • 2,628
  • 1
  • 16
  • 24
0

Just start an animation when your click event triggers. You can define how long should the animation last.

Basic benefit of using animations instead of doing calculations manually is animations being run in separated thread, so you don't loose application's responsiveness.
What is more, you can edit your animations in separated tools, such as Blend without the need to verifying animations in runtime.

Few sources: http://www.wpf-tutorial.com/styles/trigger-animations-enteractions-exitactions/ http://dotnetslackers.com/articles/wpf/IntroductionToWPFAnimations.aspx http://www.wpftutorial.net/Animation.html

SOReader
  • 5,697
  • 5
  • 31
  • 53