Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Software ==> | Visual C# (55 exercises) |
Topic ==> | The form as a class (1 exercise) |
Level ==> | Average difficulty |
Subject ==> | C# training |
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.
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
nextLetter.AddForm(groupBox1);
// check how many letters there are
CheckNumberLetters();
}
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:
Name | Scope | Type | Notes |
---|---|---|---|
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:
using System.Windows.Forms;
// 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) *
gapBetweenLetters;
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.
You can find other training resources for the subject of this exercise here:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.