❔ how to give the Label give better SetLeft() position

B(Baum (Leon)2/9/2023
The labels get random position, but the 6 Label sometimes is on the edge, what it should not be.

The Following code:
case 2:
    maxVal = 5; 
    abstand = 200; // space where labels should be
    i = 0;
    foreach (var x in RememberScreen.Children.OfType<Label>())
    {
        if ((int)x.Tag < 7)
        {
            // set position from left
            posL = random.Next(abstand * i - 10, abstand * (i + 1) - 60); // I tried with - 20 and - 60 but got still the problem
            // set position from top
            posT = random.Next(10, (int)RememberScreen.Height - (int)x.Height - 10);
            // set a number
            x.Content = $"{i + 1}";
            // set the position
            Canvas.SetLeft(x, posL);
            Canvas.SetTop(x, posT);
            // make it visible
            x.Visibility = Visibility.Visible;

            // change Position
            i++;
        }
    }


My canvas is 1200 long.
Image
AAnchy2/9/2023
you could use a clamp function on the posT and posL vars and pass it a padding
AAnchy2/9/2023
int padding = 10;
...
posL = Math.Clamp(posL, 0 + padding, RememberScreen.Width - padding);
AAnchy2/9/2023
which would ensure the labels dont move lower than or over the padding
AAccord2/10/2023
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.