Sorry for giving code like this but I’m on mobile now and I just wanted to know how to fix this when I get home because yesterday I had problems with this.
Basically, I have 24 buttons and I want to fill them with random numbers (1-24). I think someone like this should work but I can’t find a way to change the content in all buttons in this for a loop. I need to be able to change text before. Content I guess.
Random rnd = new Random();
for (int i = 0; i < 24; i++)
{
int r1 = rnd.Next(1, 24);
Container.listOfNumbers.Remove(r1);
b1.Content = r1;
}
You need to access the buttons directly, which I believe requires you to provide them with names in the XAML for you to refer to them by in the C# code behind.
If you put the buttons in a collection, you can iterate through it and update the content in each button as you go.
I gave them all names like b1, b2… I wanted to make a list of these names but I couldn’t find a way to merge name(string) with them.Content, and also it would take too long I guess.
So I should make a collection of buttons and iterate through it with foreach loop, yes?Create an array of numbers. Bind each button to a different position in the array. Randomize array.
Easy solution: https://pastebin.com/M74kT9ab
Validation: https://i.imgur.com/leNZSut.png
The meat of your question isn’t the numbers part, so I just put in something that provides a random integer between something and something.
C# devs
null reference exceptions