- Access exercises (91)
- C# exercises (79)
- Excel exercises (278)
- Power Apps exercises (13)
- Power Automate exercises (18)
- Power BI exercises (139)
- Power Platform exercises ()
- Python exercises (28)
- Report Builder exercises (141)
- SQL exercises (198)
- SSAS exercises (51)
- SSIS exercises (46)
- SSRS exercises (99)
- VBA exercises (85)
- Visual Basic exercises (46)
VISUAL BASIC EXERCISES▼
VISUAL BASIC EXERCISES▼
- Drawing forms (2)
- Form events (1)
- Variables and data types (2)
- Enumerations and constants (2)
- Form validation (3)
- Menus and toolbars (2)
- Files and folders (1)
- Basic DataGridViews (1)
- Reacting to DataGridView events (3)
- Advanced DataGridViews (2)
- Creating classes (4)
- Coding in VB.NET (2)
- Variables and constants (3)
- Testing conditions (2)
- Passing arguments (3)
- Using arrays (2)
- Loops (2)
- Working with files (3)
- Lists (3)
- Creating properties (3)
Visual Basic | Creating classes exercise | Write classes to enable you to play Owl Pairs
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Right-click on your project in Solution Explorer and choose to add the form from the above folder (you can alternatively press SHIFT + ALT + A ).
When you set this to be your start-up form and run your application, you should see this:
You should see a form containing 18 greyed out owls, with only the Deal button enabled.
When you click on the Deal button, your system should randomly assign 9 owl pictures to get something like this:
Each time you "deal" the owls you'll get a different combination.
When you click on the Start button, you can start playing:
Here someone has already guessed 3 pairs, but the fourth guess at the bottom right is wrong, and the unmatched pair will revert back to grey in a second.
When a user has detected all of the pairs, your system should show a congratulatory message then close down the form. Your task is to add code to this form so that you can play pairs!
To start with, the 9 owl pictures that you'll need are in the folder above. To add them, right-click on your project to show its properties, click on the Resources tab (as below) then add your existing image files:
Select the menu option shown to add images to your project.
Visual Studio should add the images that you select, and give them names:
The images are added to a folder called Resources.
You can assign an image to a button in code like this:
' show an owl on a button
Dim b As Button = CType(sender, Button)
b.Image = My.Resources.owl1
In this example, we convert the sender of the event to a button, then change its Image property to be the resource called owl1.
You can use the Tag property of each button to keep track of which image is assigned to it (this property isn't used for any other purpose).
At some stage you'll need to loop over all the buttons, doing something to each. Here's one way to do this (this example would remove the image from every button):
' remove images from all buttons
Dim i As Integer
For i = 1 To 18
Dim b As Button = CType(Me.Controls("Button" + i.ToString), Button)
b.Image = Nothing
There's no right or wrong way to write the classes for this project, but the answer uses the following two classes:
|PairButton||Used to refer to each button|
|PairTurn||Use for a single turn (ie the action of choosing two pictures).|
After an unsuccessful guess, you'll want to hide the two buttons chosen. One command to wait a second to give your user time to view the choices they've made would be:
' if not a match, hide both after a bit of a wait
To ensure that you don't allocate more than two owls to any combination of buttons initially, the system uses this variable:
'holds unassigned buttons
OwlsLeft = "112233445566778899"
There are many more elegant ways to do this, without doubt, and you should feel free to use one of them if you can think of it!
And with all these hints - good luck!