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 (2)
- The form as a class (1)
- Data structures (4)
- Inheritance (4)
- Interfaces (2)
- Delegates and events (2)
- Writing LINQ (1)
- Advanced LINQ (3)
- LINQ to SQL models (1)
- Writing LINQ to SQL (2)
- Grouping using LINQ (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.
The answer to the exercise will be included and explained 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 CountDown (Channel 4, about 3.10pm) there is a letters game. 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. We'll help with the letter selection process!
In a new or existing project, create a new from called frmCountdownLetters.
Open the file called frmCountdownLetters.Designer.cs. Replace the entire code with the contents of the text file in the above folder, then change the namespace in the first line to the name of your project.
When you've amended Program.cs, you should be able to run this application:
The idea is that you click on a button, and a letter of the appropriate type appears.
Before turning the page, have a think about how you might design a Letter class to hold each letter chosen.
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 will run:
private void btnVowel_Click(object sender, EventArgs e)
// create a new letter
Letter 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 desgn 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 increasse 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:
Don't worry about the fact that unusual letters will appear proportionally too often.
When everything is working OK, close your application and any open files!