Prime
Prime

Reputation: 13

UWP - Give button a random location

In UWP i would like to give my button a random location on my screen when it is clicked on.

My code looks like this:

    private void DeclareHere()
    {
        Random randomGenerator = new Random();

        int iRndX = randomGenerator.Next(1, 640);
        int iRndY = randomGenerator.Next(1, 360);

        btnRandom.Margin.Left.Equals(iRndX);
        btnRandom.Margin.Top.Equals(iRndY);
    }

    private void btnRandom_Click(object sender, RoutedEventArgs e)
    {
        DeclareHere();
    }

What is it that i'm missing? Would help if you could explain how you accomplished the solution to this.

Upvotes: 0

Views: 154

Answers (1)

Giancarlo Melis
Giancarlo Melis

Reputation: 707

Equals is just a comparison operator.

ex:

bool isEqual = btnRandom.Margin.Left.Equals(iRndY);

You need to assign margin values

So, you should change this:

    btnRandom.Margin.Left.Equals(iRndX);
    btnRandom.Margin.Top.Equals(iRndY);

with this:

 btnRandom.Margin = new Thickness { Left = iRndX, Top = iRndY};

Upvotes: 1

Related Questions