4-5
Project Design, Forms, Buttons
Beginning Visual C# Express
4. Project Design, Forms, Buttons
Review and Preview
You have now learned the parts of a Visual C# Express project and the three steps involved in building a project:
Do you have some ideas of projects you would like to build using Visual C# Express? If so, great. Beginning with this class, you will start to develop your own programming skills. In each class to come, you will learn some new features of the Visual C# Express environment, some new controls, and elements of the C# language. In this class, you will learn about project design, the form and button controls, and build a complete project.
Project Design
You are about to start developing projects using Visual C# Express. We will give you projects to build and maybe you will have ideas for your own projects. Either way, it’s fun and exciting to see ideas end up as computer programs. But before starting a project, it’s a good idea to spend a little time thinking about what you are trying to do. This idea of proper project design will save you lots of time and result in a far better project.
Proper project design is not really difficult. The main idea is to create a project that is easy to use, easy to understand, and free of errors. That makes sense, doesn’t it? Spend some time thinking about everything you want your project to do. What information does the program need? What information does the computer determine? Decide what controls you need to use to provide these sets of information. Design a nice user interface (interface concerns placement of controls on the form). Consider appearance and ease of use. Make the interface consistent with other Windows applications, if possible. Familiarity is good in Windows based projects, like those developed using Visual C# Express.
Make the C# code in your event methods readable and easy to understand. This will make the job of making later changes (and you will make changes) much easier. Follow accepted programming rules - you will learn these rules as you learn more about C#. Make sure there are no errors in your project. This may seem like an obvious statement, but many programs are not error-free. The Windows operating system has many errors floating around!
The importance of these few statements about project design might not make a lot of sense right now, but they will. The simple idea is to make a useful, clearly written, error-free project that is easy to use and easy to change. Planning carefully and planning ahead helps you achieve this goal. For each project built in this course, we will attempt to give you some insight into the project design process. We will always try to explain why we do what we do in building a project. And, we will always try to list all the considerations we make.
Saving a Visual C# Express Project
In Class 1, you learned how to open, run, and close a previously-saved Visual C# Express project, but we never talked about how a project is saved for future use. Now that you are starting to build your own projects, you need to see how to save them. It’s really quite easy. We will use the Save All button in the Visual C# Express toolbar. Look for a button that looks like several floppy disks. (With writeable CD’s so cheap, how much longer do you think people will know what a floppy disk looks like? – many new machines don’t even have a floppy disk drive!)
Save All Toolbar Button
Clicking on this button will have different results, depending on when you click it.
If you are working on a project that has never been saved, the following window will appear:
This window asks where you want to save your project. In the Name box, enter the name of the folder to save your project in. Location should show the directory your project folder will be in. You can Browse to an existing location or create a new directory by checking the indicated box. For this class, we suggest saving each of your project folders in the same directory. For the course notes, all project folders are saved in the \BeginVCSE\BVCSE Projects folder. We suggest you save your projects in a folder you create.
If you are working on a project that has been saved previously and you click on the Save All button, Visual C# Express saves all your files in the same location without asking any questions. Sometimes, you may want to ‘force’ a save. For example, if you are making lots of changes, you might occasionally like to save your work prior to running the project. Do this by clicking the Save All button at any time. And, always make sure to save your project before running it or before leaving Visual C# Express.
On-Line Help
Many times, while working in the Visual C# Express environment, you will have a question about something. You may wonder what a particular control does, what a particular property is for, what events a control has, or what a particular term in C# means. A great way to get help when you’re stuck is to ask someone who knows the answer. People are usually happy to help you - they like the idea of helping you learn. You could also try to find the answer in a book and there are lots of Visual C# Express books out there! Or, another great way to get help is to use the Visual C# Express On-Line Help system.
Most Windows applications, including Visual C# Express, have help files available for your use. To access the Visual C# Express help system, click the Help item in the main menu, then Contents. At that point, you can search for the topic you need help on or scroll through all the topics. The Visual C# Express help system is just like all other Windows help systems. If you’ve ever used any on-line help system, using the system in Visual C# Express should be easy. If you’ve never used an on-line help system, ask someone for help. They’re pretty easy to use. Or, click on Start on your Windows task bar, then choose Help. You can use that on-line help system to learn about how to use an on-line help system!
A great new feature of Visual C# Express is the idea of Dynamic Help. Once selected from the Help menu, this window appears: (on my screen, it appears as a tab under the Properties window):
This window will always displays topics deemed relevant to the current task at hand. The window above shows topics related to the Windows form. Once controls are placed on a form, as you click from control to control, the contents of this window will change to topics appropriate to the selected (active) control. For help on control properties and events, choose the ‘Members’ topic. This will give you an exhaustive list of both the properties and events associated with a particular control. You will use this feature a lot – it saves a lot of time searching through other documentation.
Another great feature about the Visual C# Express on-line help system is that it is ‘context sensitive.’ What does this mean? Well, let’s try it. Start Visual C# Express and start a new project. Go to the properties window. Scroll down the window displaying the form properties and click on the word BackColor. The word is highlighted. Press the <F1> key. A screen of information about the Form.BackColor property appears:
The help system has intelligence. It knows that since you highlighted the word BackColor, then pressed <F1> (<F1> has always been the key to press when you need help), you are asking for help about BackColor. Anytime you press <F1> while working in Visual C# Express, the program will look at where you are working and try to determine, based on context, what you are asking for help about. It looks at things like highlighted words in the properties window or position of the cursor in the code window. As you work with Visual C# Express, you will find you will use ‘context-sensitive’ help a lot. Many times, you can get quick answers to questions you might have. Get used to relying on the Visual C# Express on-line help system for assistance.
That’s enough new material about the Visual C# Express environment. Now, let’s look, in detail, at two important controls: the form itself and the button. Then we’ll start our study of the C# language and build a complete project.
The Form Control
We have seen that the form is the central control in the development of a Visual C# Express project. Without a form, there can be no project! Let’s look at some important properties and events for the form control. The form appears when you begin a new project.
Icon
Text
Properties
Like all controls, the form has many (over 40) properties. Fortunately, we only have to know about some of them. The properties we will be concerned with are:
Property Description
Name Name used to identify form. In this course, we will always use the default Form1 for the name.
Text Text that appears in the title bar of form.
BackColor Background color of form.
Icon Reference to icon that appears in title bar of form (we’ll look at creating icons in Class 7).
Width Width of the form in pixels.
Height Height of form in pixels.
FormBorderStyle Form can either be sizable (can resize using the mouse) or fixed size.
StartPosition Determines location of form on computer screen when application begins (we usually use a value of CenterScreen).
The form is primarily a ‘container’ for other controls. Being a container means many controls (the button control, studied next, is an exception) placed on the form will share the BackColor property. To change this behavior, select the desired control (after it is placed on the form) and change the color.
Example
To gain familiarity with these properties, start Visual C# Express and start a new project with just a form. Set the Height and Width property values (listed under Size in the properties window) and see their effect on form size. Resize the form and notice how those values are changed in the properties window. Set the Text property. Pick a new background color using the selection techniques discussed in Class 3. Try centering the form by changing the StartPosition property. To see the effect of the BorderStyle property, set a value (either Fixed Single or Sizable; these are the only values we’ll use in this course) and run the project. Yes, you can run a project with just a form as a control! Try resizing the form in each case. Note the difference. Stop this example project.
Events
The form does support events. That is, it can respond to some user interactions. We will only be concerned with three form events in this course:
Event Description
Click Event executed when user clicks on the form with the mouse.
Load Event executed when the form first loads into the computer’s memory. This is a good place to set initial values for various properties and other project values.
FormClosing Event called when the project is ending. This is a good place to ‘clean up’ your project.
Recall, to create any corresponding event method, make the form the active control, choose Events in the properties window, then double-click the name of the event. The event method will appear in the code window. To view an ‘already-created’ method in the code window, use the Methods List drop-down box at the top of the code window.
Typical Use of Form Control
For each control in this, and following chapters, we will provide information for how that control is typically used. The usual design steps for a Form control are:
Ø Set the Text property to a meaningful title.
Ø Set the StartPosition property (in this course, this property will almost always be set to CenterScreen)
Ø Set the FormBorderStyle to some value. In this course, we will mostly use FixedSingle forms.
Ø Write any needed initialization code in the form’s Load event.
Ø Write any needed finalization code in the form’s FormClosing event.
Button Control
The button is one of the more widely used Visual C# Express controls. Buttons are used to start, pause, or end particular processes. The button is selected from the toolbox. It appears as:
In Toolbox: On Form (default properties):
A few useful properties for the button are:
Name Name used to identify button. Three letter prefix for button names is btn.
Text Text (caption) that appears on the button.
TextAlign How the caption text is aligned on the button.
Font Sets style, size, and type of caption text.
BackColor Background color of button.
ForeColor Color of text on button.
Left Distance from left side of form to left side of button (referred to by X in properties window).
Top Distance from top side of form to top side of button (referred to by Y in p...
Brambur