Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I want to create silverlight application in C#, I added a button and I want to create rectangles with each click one by one, now i managed to create one rectangle but the problem is that i don't know how to change the location of the next rectangle to be created?

Please Help I want each click create rectangle next to another?!
Thanks
here is the code:

C#
C#
public MainPage()
        {
            InitializeComponent();

            Canvas c = new Canvas();

            Button b = new Button();
            Canvas.SetLeft(b, -50);
            Canvas.SetTop(b, 20);
            b.Width = 75;
            b.Height = 23;
            LayoutRoot.Children.Add(b);
            b.Click += new RoutedEventHandler(b_Click);
            

        }

        void b_Click(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();

            Rectangle rectangle = new Rectangle();
            rectangle.Fill = new SolidColorBrush(Colors.Cyan);
            Canvas.SetLeft(rectangle, 100);
            Canvas.SetTop(rectangle, 100);
            rectangle.Width = 200;
            rectangle.Height = 100;

            LayoutRoot.Children.Add(rectangle);

        }


xaml:
HTML
<canvas x:name="LayoutRoot" removed="White" height="215" width="292" xmlns:x="#unknown">

</canvas>
Posted
Updated 10-Sep-11 10:22am
v3

1 solution

You can write c# code to set the next rectangle based on previous one. Modified code will be:

C#
static double prevLoc = 0;
void b_Click(object sender, RoutedEventArgs e)
{
    //throw new NotImplementedException();

    Rectangle rectangle = new Rectangle();
    rectangle.Fill = new SolidColorBrush(Colors.Cyan);
    rectangle.Width = 200;
    rectangle.Height = 100;
    rectangle.SetValue(Canvas.LeftProperty, prevLoc);
    prevLoc += 25;
    LayoutRoot.Children.Add(rectangle);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900