-->
If the icons show up in the same cells every game, it's not very challenging. To avoid this, assign the icons randomly to the Label controls on your form by using an
AssignIconsToSquares()
method.To assign a random icon to each label
I am not able to repro your issue behavior with both Visual Studio 2015 and Visual Studio 2015 Update1 on Windows 7/8/8.1/10. The icon file will be opened on Visual Studio when right-click the icon file and select Visual studio.
- Before adding the following code, consider how the method works. There's a new keyword:
foreach
in C# andFor Each
in Visual Basic. (One of the lines is commented out on purpose, which is explained at the end of this procedure.)ImportantUse the programming language control at the top right of this page to view either the C# code snippet or the Visual Basic code snippet. - Add the
AssignIconsToSquares()
method as shown in the previous step. You can put it just below the code you added in Step 2: Add a Random object and a list of icons.As mentioned earlier, there's something new in yourAssignIconsToSquares()
method: aforeach
loop in C# andFor Each
in Visual Basic. You can use aFor Each
loop any time you want to do the same action multiple times. In this case, you want to execute the same statements for every label on your TableLayoutPanel, as explained by the following code. The first line creates a variable namedcontrol
that stores each control one at a time while that control has the statements in the loop executed on it.NoteThe names 'iconLabel' and 'control' are used because they are descriptive. You can replace these names with any names, and the code will work exactly the same as long as you change the name in each statement inside the loop.TheAssignIconsToSquares()
method iterates through each label control in the TableLayoutPanel and executes the same statements for each of them. Those statements pull a random icon from the list that you added in Step 2: Add a Random object and a list of icons. (That's why you included two of each icon in the list, so there would be a pair of icons assigned to random Label controls.)Look more closely at the code that runs inside theforeach
orFor Each
loop. This code is reproduced here.The first line converts the control variable to a label named iconLabel. The line after that is anif
statement that checks to make sure the conversion worked. If the conversion does work, the statements in theif
statement run. (As you may recall from the previous tutorials, theif
statement is used to evaluate whatever condition you specify.) The first line in theif
statement creates a variable named randomNumber that contains a random number that corresponds to one of the items in the icons list. To do this, it uses the Next() method of the Random object that you created earlier. TheNext
method returns the random number. This line also uses the Count property of the icons list to determine the range from which to choose the random number. The next line assigns one of the icon list items to the Text property of the label. The commented-out line is explained later in this topic. Finally, the last line in theif
statement removes from the list the icon that has been added to the form.Remember, if you're not sure about what some part of the code does, you can position the mouse pointer over a code element and review the resulting tooltip. You can also step through each line of code while the program is running by using the Visual Studio debugger. See How do I: Step with The debugger in Visual Studio? or Navigate through code with the debugger for more information. - To fill up the game board with icons, you need to call the
AssignIconsToSquares()
method as soon as the program starts. If you're using C#, add a statement just below the call to theInitializeComponent()
method in the Form1constructor, so your form calls your new method to set itself up before it's shown. Constructors are called when you create a new object, such as a class or struct. See Constructors (C# programming guide) or Use constructors and destructors in Visual Basic for more information.For Visual Basic, add theAssignIconsToSquares()
method call to theForm1_Load
method so that the code looks like the following. - Save your program and run it. It should show a form with random icons assigned to each label.
- Close your program, and then run it again. Notice that different icons are assigned to each label, as shown in the following picture.
Matching game with random iconsThe icons are visible now because you haven't hidden them. To hide them from the player, you can set each label's ForeColor property to the same color as its BackColor property.TipAnother way to hide controls like labels is to set their Visible property to False. - To hide the icons, stop the program and remove the comment marks for the commented line of code inside the
For Each
loop. - On the menu bar, choose the Save All button to save your program, and then run it. The icons seem to have disappeared—only a blue background appears. However, the icons are randomly assigned and are still there. Because the icons are the same color as the background, it hides them from the player. After all, it wouldn't be a very challenging game if the player could see all of the icons right away!
To continue or review
- To go to the next tutorial step, see Step 4: Add a click event handler to each label.
- To return to the previous tutorial step, see Step 2: Add a Random object and a list of icons.