Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:42:14+00:00 2026-05-23T23:42:14+00:00

I have a canvas where I draw rectangles and move them randomly with the

  • 0

I have a canvas where I draw rectangles and move them randomly with the help of a storyboard. After a few cycles the storyboard.completed event does not fire any more. Does anybody know? here is my xaml:

    <Grid>
        <Canvas Name="movingCanvas" Background="Green" Margin="0,29,0,0"></Canvas>
        <TextBlock Height="23" Name="textBlock1" Text="TextBlock" Margin="528,0,0,538" />
    </Grid>

And the code:

 private Random random = new Random();
    private Storyboard gameLoop = new Storyboard();
    private int i = 0;

    public Window3()
    {
        InitializeComponent();
        this.gameLoop.Duration = TimeSpan.FromMilliseconds(100);
        this.gameLoop.Completed += new EventHandler(this.gameLoop_Completed);
        this.gameLoop.Begin();
    }

    private void gameLoop_Completed(object sender, EventArgs e)
    {
        this.addRectangle();
        this.moveRectangle();
        i++;
        this.textBlock1.Text = i.ToString();
        this.gameLoop.Begin();
    }

    private void addRectangle()
    {
        Rectangle rect = new Rectangle();
        rect.Height = 100;
        rect.Width = 100;
        rect.Stroke = new SolidColorBrush(Colors.Black);
        Canvas.SetLeft(rect, random.Next((int)this.Width));
        Canvas.SetTop(rect, random.Next((int)this.Height));
        this.movingCanvas.Children.Add(rect);
    }

    private void moveRectangle()
    {
        foreach (UIElement elm in this.movingCanvas.Children)
        {
            int moveLeft = random.Next(10);
            int distance = random.Next(-10, 20);
            if (moveLeft > 5)
            {
                Canvas.SetTop(elm, Canvas.GetTop(elm) + distance);
            }
            else
            {
                Canvas.SetLeft(elm, Canvas.GetLeft(elm) + distance);
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:42:15+00:00Added an answer on May 23, 2026 at 11:42 pm

    The Completed event does also not occur whitout creating and moving rectangles:

    private Storyboard gameLoop = new Storyboard();    
    private int i = 0;    
    public Window3()    
    {        
        InitializeComponent();        
        this.gameLoop.Duration = TimeSpan.FromMilliseconds(100);        
        this.gameLoop.Completed += new EventHandler(this.gameLoop_Completed);
        this.gameLoop.Begin();    
    }    
    
    private void gameLoop_Completed(object sender, EventArgs e)    
    {        
        i++;        
        this.textBlock1.Text = i.ToString();        
        this.gameLoop.Begin();    
    }
    

    If you add a animation to the storyboard, the storyboard not stops firing the event.

    public Window3()    
    {        
        InitializeComponent();        
        this.gameLoop.Duration = TimeSpan.FromMilliseconds(100);        
        this.gameLoop.Completed += new EventHandler(this.gameLoop_Completed);
        DoubleAnimation animation= new DoubleAnimation { From = 100, To = 101 };
        ani.SetValue(Storyboard.TargetProperty, this);
        ani.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));            
        this.gameLoop.Children.Add(ani);
    
        this.gameLoop.Begin();    
    }    
    

    Like Kshitij Mehta said above, i think use a timer instead the storyboard, but maybe you have a reason to use a storyboard….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a canvas where I can draw, resize and move rectangles around. I'm
I have a large module written in JS which uses Canvas to draw and
I have a class which inherits from Canvas. On the OnRender method I draw
I have an isometric map which I draw to the canvas. When I try
I have a Canvas that i draw text on. (this is quite advanced i
I have a canvas onto which I draw shapes like rectangle etc. When I
I am playing with the HTML5 canvas element, using JS to draw some rectangles
I need to draw rotated rectangle on Canvas. I have x and y of
I have a canvas in Flex that shall be able only to be scrolled
I have a Canvas in a Flex application which has items inside it that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.