The Next method returns the random number. It uses the Next() method of the Random object. The first line in the if statement creates a variable named randomNumber that contains a random number that corresponds to one of the items in the icons list.If the conversion does work, the statements in the if statement run. The second line is an if statement that checks to make sure the conversion worked.The first line converts the control variable to a label named iconLabel.The statements pull a random icon from the list. It runs the same statements for each of them. The AssignIconsToSquares() method iterates through each label control in the TableLayoutPanel. One of the lines is commented out on purpose. You can enter this code just below the code you added in the previous section. ' iconLabel.ForeColor = iconLabel.BackColor ' so an icon is pulled at random from the listįor Each control In TableLayoutPanel1.Controlsĭim randomNumber = random.Next(icons.Count) ''' Assign each icon from the list of icons to a random square iconLabel.ForeColor = iconLabel.BackColor Int randomNumber = random.Next(icons.Count) so an icon is pulled at random from the listįoreach (Control control in tableLayoutPanel1.Controls) / Assign each icon from the list of icons to a random square In the existing code, add the following code. The Visual Studio IDE displays the code module for Form1. Then select View > Code.Īs an alternative, select the F7 key or double-click Form1. Select Form1.cs if you're using C#, or Form1.vb if you're using Visual Basic. Your Matching Game project appears under Open recent. The first is a Random object that randomly chooses cells in the TableLayoutPanel. You use new statements to create two objects. In this section, you create a set of matching symbols for the game.Įach symbol is added to two random cells in the TableLayoutPanel on the form. If you haven't done that tutorial, go through that one first. This tutorial builds on the previous tutorial, Create a matching game application. Add event handlers that show icons to the labels.Add a Random object and a list of icons.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |