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

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++;
}
}
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.
2 Replies
Anchy
Anchy2y ago
you could use a clamp function on the posT and posL vars and pass it a padding
int padding = 10;
...
posL = Math.Clamp(posL, 0 + padding, RememberScreen.Width - padding);
int padding = 10;
...
posL = Math.Clamp(posL, 0 + padding, RememberScreen.Width - padding);
which would ensure the labels dont move lower than or over the padding
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server