none
Help! Having problems trying to animate dynamically created rectangles. RRS feed

  • Question

  • Hi everyone,

    I have come across a very unusual problem trying to animate dynamically created rectangles in my VB.NET 3.5/WPF project.  I have searched the web for answers and can't find them.  This is a mission critical piece of the project and I'm hoping that someone here has the answers!

    I create a series of rectangles based on the number of columns and rows a user inputs.  That is no problem, they are all created just fine.  I then set each rectangle's event handler for MouseLeftButtonDown:

    AddHandler rec.MouseLeftButtonDown, AddressOf Control_MouseLeftButtonDown

    Again, that works fine, when I click on a rectangle that event is fired and I can pull the name of the rectangle selected.  But when I add animation to it, the rectangle doesn't animate, the entire form animates!  If I maximize the form, it doesn't run the animation on the form or the rectangle, but if it is free floating, the form actually runs the animation...not the rectangle. 

    So instead of the rectangle dropping down and starting out as width=2, height=2 and then expanding to width=50, height=50...the form minimizes down to just the header and grows to 50 x 50.  It's driving me crazy!

    But, as I said, when I check the name of the object (sender)...it comes back with the name of the rectangle...not the form, so sender is being seen as the rectangle and not the form.

    Any ideas as to why this happens?  Please don't give me examples in C# as it never translates directly over to VB.  This is all in VB 3.5 and WPF.

    Thanks,
    Beth


    My code is this:

    Private Sub Control_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)

    Dim strName As String

    strName = sender.Name

    Dim widthAnimation As New DoubleAnimation

    widthAnimation.From = 0

    widthAnimation.To = 50

    widthAnimation.Duration = TimeSpan.FromSeconds(2)

    Dim heightAnimation As New DoubleAnimation

    heightAnimation.From = 0

    heightAnimation.To = 50

    heightAnimation.Duration = TimeSpan.FromSeconds(2)

    sender.fill = New SolidColorBrush(Colors.Green)

    BeginAnimation(sender.WidthProperty, widthAnimation)

    BeginAnimation(sender.HeightProperty, heightAnimation)

    End Sub


    Monday, February 23, 2009 5:05 PM

Answers

  • You are calling BeginAnimation on your Window.   Your code is the same as calling Me.BeginAnimation

    Cast the sender to a Rectangle and call on the instance instead.
     
            Dim r1 As Rectangle = TryCast(sender, Rectangle) 
     
            If r1 Is Nothing Then 
                Return 
            End If 
     
     
            Dim widthAnimation As New DoubleAnimation 
     
            'widthAnimation.From = 0 
     
            widthAnimation.To = 100 
            widthAnimation.Duration = TimeSpan.FromSeconds(2) 
     
            Dim heightAnimation As New DoubleAnimation 
            heightAnimation.From = 0 
            heightAnimation.To = 50 
            heightAnimation.Duration = TimeSpan.FromSeconds(2) 
     
            r1.Fill = New SolidColorBrush(Colors.Green) 
            r1.BeginAnimation(Rectangle.WidthProperty, widthAnimation) 
            r1.BeginAnimation(Rectangle.HeightProperty, heightAnimation) 


    Walt | http://wpfwonderland.wordpress.com
    Tuesday, February 24, 2009 4:32 AM