Results 1 to 6 of 6

Thread: [RESOLVED] Sliding objects with code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Resolved [RESOLVED] Sliding objects with code

    I am having a slight problem sliding controls on a form with C# code. So far I found this article http://vbcity.com/blogs/xtab/archive...on-in-wpf.aspx on how to do it with XAML, but I need to do it with C# because items will only slide to places depending on what item is selected in a textbox.

    I have my controls setup like this:


    On the left is how it should look with item one selected, and on the right is how it should look with item 2 selected. With the textbox gone the height of the form will also reduce as well and when it comes back (item 1) it will increase.

    The code I tried so far is this, but it doesn't seem to work so I must be doing something wrong but don't know what as I am fairly new to WPF.

    Code:
    Storyboard storyboard = new Storyboard();
                TimeSpan duration = new TimeSpan(0, 0, 3);
     
                DoubleAnimation animation = new DoubleAnimation();
                //animation.From = 0.0;
                //animation.To = 1.0;
                animation.By = 100;
                animation.Duration = new Duration(duration);
     
                Storyboard.SetTargetName(animation, btnTest.Name);
                Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.XProperty));
     
                storyboard.Children.Add(animation);
     
                storyboard.Begin(this);

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Sliding objects with code

    If you are not bothered about animating it and simply want the controls below (ie the button) to move up when the textbox is made invisible then you can simply set the textbox's Visibility property to Collapsed rather than Hidden.

    However, I'll assume you do want to animate it, so here's an example of how I would do it (this is to slide the button up to just beneath the combobox) :

    Code:
    Textbox1.Visibility = Windows.Visibility.Hidden
    Dim Anim As New Animation.ThicknessAnimation
    Anim.To = New Thickness(0, -15, 0, 0)
    Anim.Duration = New Duration(TimeSpan.FromSeconds(1))
    Button1.BeginAnimation(FrameworkElement.MarginProperty, Anim)
    and here's a complete example:
    Code:
    Class Window1 
    
        Private Sub ComboBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged
            If ComboBox1.SelectedIndex = 0 Then
                SlideDown()
            ElseIf ComboBox1.SelectedIndex = 1 Then
                SlideUp()
            End If
        End Sub
    
        Private Sub SlideUp()
            Textbox1.Visibility = Windows.Visibility.Hidden
            Dim Anim As New Animation.ThicknessAnimation
            Anim.To = New Thickness(0, -15, 0, 0)
            Anim.Duration = New Duration(TimeSpan.FromSeconds(1))
            Button1.BeginAnimation(FrameworkElement.MarginProperty, Anim)
        End Sub
    
        Private Sub SlideDown()
            Dim Anim As New Animation.ThicknessAnimation
            Anim.To = New Thickness(0, 0, 0, 0)
            Anim.Duration = New Duration(TimeSpan.FromSeconds(1))
            Button1.BeginAnimation(FrameworkElement.MarginProperty, Anim)
            Textbox1.Visibility = Windows.Visibility.Visible
        End Sub
    
    End Class
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: Sliding objects with code

    Wow thanks Chris!!

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Sliding objects with code

    No problem
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: [RESOLVED] Sliding objects with code

    Won't let me add rep to you, says I need to share it around more before giving you more lol

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Sliding objects with code

    lol thats ok, its the thought that counts
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width