EXERCISE TOPIC▼
C# 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# | Validating forms exercise | Use the Validating event to check inputs
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 on the relevant Wise Owl classroom training course (sadly for the moment only in the UK).
Software ==> | Visual C# (79 exercises) |
Version ==> | Any version of C# |
Topic ==> | Validating forms (6 exercises) |
Level ==> | Average difficulty |
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.
This exercise asks you to validate form input. First add the existing form called frmNewHeart from the folder shown above into your project:

Call the ValidateChildren method when you click on the Apply button, but don't do anything else yet at this stage
Now attach code to the Validating event of the txtName textbox, to display a message if it has been left blank:

What your MessageBox could show
Do the same thing for the txtAge field, but for this check also that the age entered is a whole number:

You could use a try / catch clause or the TryParse method to do this
Everyone lies about their age: add code to the txtAge textbox's Validated event to increase the age after it has passed validation:

Make sure you tell the user that you don't trust him/her!
When everything's working OK, close the form down.