When you create a new project, the project begins with a single, blank form. You can then add controls to this form and set the properties of the form and controls so they look and work the way you want.
The design of the Invoice Total form
Before I show you how to add controls to a form and set the properties of the form and controls, I want to describe the Invoice Total form that I’ll use as an example throughout this chapter and the next chapter. This form is presented in figure 2-3. As you can see, the form consists of ten controls: four text boxes, four labels, and two buttons.
The Invoice Total form lets the user enter a subtotal into the first text box, and then calculates the discount percent, discount amount, and total for that order when the user clicks the Calculate button. For this simple application, the discount percent is based upon the amount of the subtotal, and the results of the calculation are displayed in read-only text box controls.
After the results of the calculation are displayed, the user can enter a different subtotal and click the Calculate button again to perform another calculation. This cycle continues until the user clicks the Close button in the upper right corner of the form or clicks the Exit button. Then, the form is closed and the application ends.
This application also provides keystroke options for users who prefer using the keyboard to the mouse. In particular, the user can activate the Calculate button by pressing the Enter key and the Exit button by pressing the Esc key. The user can also activate the Calculate button by pressing Alt+C and the Exit button by pressing Alt+X.
In the early days of computing, it was a common practice to sketch the user interface for an application on paper before developing the application. That’s because a programmer had to enter the code that defined the user interface, andthe task of writing this code would have been error prone if the interface wasn’t planned out first. As you’ll see in this chapter, however, the Form Designer makes it easy to design a form at the same time that you implement it. Because of that, you usually don’t need to sketch the layout of a form before you design it in Visual Studio.
As you use the Form Designer, Visual Studio automatically generates the C# code that’s needed to define the form and its controls. In other words, the form that you see in the Form Designer is just a visual representation of the form that the C# code is going to display later on. Then, all you have to do is write the C# code that gives the form its functionality, and you’ll learn how to do that in the next chapter.
- A text box is used to get the subtotal from the user. Read-only text boxes are used to display the discount percent, discount amount, and total. And label controls are used to identify the values that are in the text boxes on the form.
- After entering a subtotal, the user can click the Calculate button to calculate the discount percent, discount amount, and total. Alternatively, the user can press the Enter key to perform the calculation.
- To calculate another invoice total, the user can enter another subtotal and then click the Calculate button or press the Enter key again.
- To close the form and end the application, the user can click the Close button in the upper right corner of the form or click the Exit button. Alternatively, the user can press the Esc key to exit from the form.
- The user can press Alt+C to activate the Calculate button or Alt+X to activate the Exit button. On most systems, the letters that activate these buttons aren’t underlined until the user presses the Alt key.
Three types of controls
- A label displays text on a form
- A text box lets the user enter text on a form
- A button initiates form processing when clicked
This article is an excerpt from Murach C# 2015 by Anne Boehm and Joel Murach
Reproduced with permission from Murach Publishing