WISE OWL EXERCISES
VISUAL C# EXERCISES
- Creating forms (4)
- Coding form events (1)
- Laying out your code (2)
- C# variables (4)
- Enumerations and constants (2)
- Conditions (2)
- Modular code (3)
- Arrays (2)
- Looping (2)
- Files and folders (3)
- Properties in C# (3)
- Using lists (3)
- Validating forms (6)
- Toolbars, menus and status bars (1)
- FileDialogs and StreamReaders (1)
- Debugging and trapping errors (1)
- Introduction to DataGridViews (1)
- DataGridView events (3)
- Complex DataGridViews (2)
- Creating classes (4)
- The form as a class (1)
- Data structures (6)
- Inheritance (5)
- Interfaces (2)
- Delegates and events (2)
- Writing LINQ (2)
- Advanced LINQ (2)
- Entity Frameworks (1)
- LINQ with Entity Frameworks (4)
- Grouping using LINQ (2)
- LINQ to SQL (2)
Visual C# | The form as a class exercise | Create classes to help with playing CountDown
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 can learn how to do this exercise if you attend the course listed below!
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.
In Channel 4's CountDown contestants choose 9 letters (made up of vowels and consonants); they then have to form words which are as long as possible from this selection. The aim of this exercise is to help with the letter selection process!
In a new or existing project, create a new form called frmCountdownLetters and add two buttons and a GroupBox control to get:
Delete the Text property of the GroupBox control, so that it appears as a rectangle only.
Before turning the page, have a think about how you might design a Letter class to hold each letter chosen.
The aim of this exercise is to be able to request vowels and consonants to create a series of 9 letters:
A game after 7 of the 9 letters have been requested.
When you finish, you should see this:
This message should pop up when you've chosen 9 letters.
Read on for how to make this all work!
Our aim will be to attach code to each button to create a new text box containing a random character of the correct type. For example, here's what the Vowel please, Carol button could run:
private void btnVowel_Click(object sender, EventArgs e)
// create a new letter
var nextLetter = new Letter();
// this is a vowel
nextLetter.IfVowel = true;
// add it to the form
// check how many letters there are
The CheckNumberLetters method should check the number of letters already added, and if this is 9 it should:
- Disable the two buttons (set their Enabled properties to False); and
- Display a message that the Countdown clock has started!
To make the code above work, you need to create a Letter class to contain the following properties and methods:
|IfVowel||Public||Write-only property||Whether this is a vowel or not.|
|Choice||Private||Read-only property||The randomly generated letter.|
|AddForm||Public||Method||Add a text box to our form.|
Note: this isn't necessarily the optimal solution, and you are welcome to implement your own class design if you prefer it to the one shown above.
First create the Letter class, and give it a variable called NumberLetters to hold the number of letters which have been processed. Create a constructor for the class, and use this to increase the value of NumberLetters by one whenever a new instance of the class is created.
Create the following fields and constants for your class:
// generate a random number seed
static Random r = new Random();
// the horizontal offset for the first letter
private const int initialGap = 24;
// the horizontal offset for all other letters
private const int gapBetweenLetters = 54;
private const string Vowels = "AEIOU";
private const string Consonants = "BCDFGHJKLMNPQRSTVWXYZ";
The random number seed has to be static because otherwise you'll get the same random number for each letter.
Create a Boolean field called ifVowel, and a public read-only property called IfVowel to expose this value.
Create a private read-only property called Choice which will return the next vowel or consonant, according to the value of the ifVowel field. You may find the following two bits of information useful:
- You can use r.Next(Minimum integer value, Maximum integer value) to return a random integer in a given range;
- You can use the method substring(n-1, 1) to return the nth character in a string.
Now it's time to create the all-important AddForm method. To do this, first create the method:
// lots of other lines between
public void AddForm(GroupBox grp)
This takes a group box as an argument because we'll add one textbox to this group box for each letter found.
Now add code to this method so that it:
- creates a new text box;
- adds it to the group box;
- sets various properties to format it; and
- sets the text in the box to equal the Choice property.
To save you spending ages working out the left position for each text box, use:
// set position from the left
int leftPosition = initialGap + (NumberLetters-1) *
The easiest way to do this by far is to add a text box to the form, then copy the generated designer code into your class and amend it.
Here's the sort of thing you should see after generating 9 letters in your game:
Don't worry about the fact that unusual letters will appear proportionally too often; nor that nothing happens after this message appears!
When everything is working OK, close your application and any open files.