Frage

Just wondering if there is a way to arrange controls in a circular shape like a round table. Control can be an image or textbox. For example: if i have an ellipse shape, all am looking is to fill controls along the perimeter border of the ellipse. Please let me know if there is a way to do this using xaml and c#. Here is what i have done so far. I created a rectangle and have the X, Y position values using the Point Type. Based on the points i could create a textbox.. but couldn;t loop until the i reach 0,0 again. 0,0 is the bottom left position (x,y)

for (int i = 0; i < count; i++)
        {
            TextBox tb = new TextBox();
            tb.Text = "Button " + i;
            Point p = new Point();
            p.X =  (int)(Math.Cos((double)i) * radius) + radius;
            p.Y = (int)(Math.Sin((double)i) * radius) + radius;
            Canvas.SetLeft(tb, p.X);
            Canvas.SetTop(tb, p.Y);
            SpTable.Children.Add(tb);
        }

Similarly need to add controls in the Y direction... Ellipse can be replaced by Rectangle as well. The controls are adding but they are getting added in only one column just below one another.

<StackPanel x:Name="SpTable1">
War es hilfreich?

Lösung

You have to calculate the positions of the textboxes using cos and sin.

This creates 20 textboxes arranged in a circular pattern.

        int radius = 200;

        for (int i = 1; i < 20; i++)
        {
            TextBox tb = new TextBox();

            tb.Left = (int)(Math.Cos((double)i) * radius) + radius;
            tb.Top = (int)(Math.Sin((double)i) * radius) + radius;

            this.Controls.Add(tb);
        }

Hope this helps

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top