How to move controls in stackpanels at runtime? RRS feed

  • Question

  • Hi!

    Let's say that I have listed 10 ComboBoxes in a StackPanel (1 to 10). I want to move ComboBox 9 between ComboBox 2 and 3 so that the order now is (1,2,9,3,4,5,6,7,8,10).
    I want to move the controls around by using let's say leftmousebuttondown and at runtime. Is this possible and if yes, how can I do it?

    Lars ;o)  
    Monday, January 12, 2009 8:48 PM


All replies

  •  Hi

    in the below example I have moved the 5th Combobox between 2nd and 3rd in the StackPanel .

                ComboBox cb = StackPanel1.FindName("ComboBox5"as ComboBox;  
                ComboBox cb2 = StackPanel1.FindName("ComboBox2"as ComboBox;  
                int i = 0;  
                if (cb != null)  
                    if (cb2 != null)  
                        i = StackPanel1.Children.IndexOf(cb2);  
                        StackPanel1.Children.Insert(i + 1, cb);  

    I hope this will help you :)

    Prasad - www.beautifulmind.blog.co.in
    • Proposed as answer by prasad22 Tuesday, January 13, 2009 4:08 AM
    • Marked as answer by Hua Chen Thursday, January 15, 2009 8:05 AM
    Tuesday, January 13, 2009 4:08 AM
  • Hi prasad22

    Thanks for the answer.

    Can you explain it more specific. The code that you are showing, is this used in the leftmousebuttondown event?

    I want to make a program, where the user can change the order of all the controls, that are located in the stackpanel
    simply by dragging/dropping the them around at runtime.

    Lars ;o)
    Tuesday, January 13, 2009 8:54 AM
    • Proposed as answer by prasad22 Tuesday, January 13, 2009 4:14 PM
    • Marked as answer by Lars Theisen Thursday, January 15, 2009 6:45 AM
    Tuesday, January 13, 2009 4:13 PM