Windows Forms Controls and Development: A Practical Approach

ToolStrip Control

The ToolStrip control lets you create a toolbar with the professional and consistent visual style of Microsoft Office. ToolStrip controls are containers for ToolStripItems.

The ToolStrip contains the following items:

  • ToolStripButton: A button that can contain text or an image.
  • ToolStripLabel: A label that displays text, images, and hyperlinks.
  • ToolStripSplitButton: Represents a combination of standard buttons, a kind of menu.
  • ToolStripDropDownButton: Represents a list box where the user can select a single item.
  • ToolStripSeparator: A line separator, allowing you to separate groups.
  • ToolStripComboBox: A combo box.
  • ToolStripTextBox: A text box that allows you to display and receive user data.
  • ToolStripProgressBar: A progress bar, designed to provide feedback to the user.

MenuStrip Control

The MenuStrip control allows you to set custom controls which can be activated by users. Menus give the user easy and fast access to the application. The control derives from ToolStrip, and MenuStrip can contain all items in the ToolStrip. The main objective of the MenuStrip container is to hold ToolStripMenuItems. ToolStripMenuItems are the controls that provide a visual representation for the menu items. ToolStripMenuItems may appear as text, an image, or both.

StatusStrip Control

Commonly known as the status bar, the StatusStrip control is used to display information about objects on the form and provide feedback on processes that are occurring in the form. StatusStrip is a container that can hold other controls. In it, you can add StatusLabel, ProgressBar, DropDownButton, and SplitButton.

Adding StatusStrip on a Form:

  • Select the Form in the designer, and go to the Toolbox (View > Toolbox).
  • In the All Windows Forms tab, double-click on the StatusStrip control.
  • Observe that the control was added at the bottom of the form.
  • To change the control properties, right-click on the control and select Properties.
  • To change the position of the control, use the Dock property.

Windows Forms

Windows Forms are the foundation for many Microsoft Windows applications and can be configured to provide a variety of options for the user interface (UI). The developer can create forms of various sizes and formats and customize them according to the user’s needs.

Windows Forms provides a container that hosts controls and allows the developer to present applications in a familiar and consistent way to the user.

Forms can receive data from user input and display data to the user through the controls.

Most projects require more than one Form. You can add and configure Forms at design time, or you can create instances of Forms via code at runtime.

To add a new Form:

  • With a Windows Forms Application selected, go to the Project menu, and click Add New Item.
  • The Add New Item window opens.
  • In the Categories window, click the item Windows Forms.
  • In the Templates window, select the item Windows Form.
  • At the bottom of the window, change the Name property to the desired name of the Windows Form.
  • Click the Add button.

Windows Form Properties

The appearance of the user interface is an important part of the application. A bad interface becomes a problem for the user. The appearance of the user interface can be modified using the properties of the Windows Form.

Windows Forms contains a variety of properties that can customize the look of the Form. These properties can be changed in the Properties Window (menu View / Properties Window).

Here are some properties of the Form:

  • Name: Used for reference in the Form code.
  • Text: The title of the Form.
  • FormBorderStyle: Determines how the edge of the Form is rendered, and also defines how the Form behaves at runtime.
  • WindowState: Defines in which state (normal, minimized, or maximized) the Form first appears.
  • IsMdiContainer: Determines whether the form is an MDI container.
  • StartPosition: Determines the position (e.g., centered) of the Form when it is displayed.

What is an MDI Form?

MDI (Multiple Document Interface) is a programming interface to create a Microsoft Windows application that enables users to work with multiple documents at once. When a Form has the property IsMdiContainer marked as True, this means that the Form is the container (parent) for its children, i.e., the Forms which are instantiated with a Form as a parent will appear in the body of this Form.

Startup Form

The Startup Form is the form that is initialized when the Windows Forms application runs.

To set the Startup Form:

  • Locate the Program.cs file in the Windows Forms Application and double-click to view the code.
  • Locate the Main method, and then locate the line: Application.Run(new Form1()); where Form1 is the name of the Form that is initialized when the application is run.
  • Change Form1 to the name of the Form that should be initialized when the application is run.

Displaying a Windows Form

There are two ways to display a Form using the Show and ShowDialog methods.

  • Show: The user can continue working in any part of the application while the form is displayed.
  • ShowDialog: The user cannot continue working with the rest of the application until they close or hide the Form.

Windows Forms Applications

Windows Forms Applications are applications that run locally on the user’s computer. The developer can use Visual Studio 2008 to create user interface applications using Windows Forms.

Creating a Windows Forms application:

  • With a solution selected, go to the File menu, click New, and then select Project.
  • The New Project window appears.
  • In the Project types window, click on the item Visual C#.
  • In the Templates window, select the item Windows Forms Application.
  • At the bottom of the window, change the Name property to the desired name of the Windows Forms application and also change the Location property, stating the location where the Windows Forms application will be saved.
  • Click the OK button.

Exception Handling

Exceptions are unexpected events that interrupt the program. For example, if an application expects the user to enter a numerical value in a given field, and instead the user enters a letter, while trying to save the value in the database, it generates an exception because the database was expecting a number and the user typed a letter, causing an unexpected event.

To handle events using C#, you use the try-catch statement. The try-catch statement consists of using a try block, followed by one or more catch clauses. The try block contains the program code where the exception might occur. In the context where the user enters letters instead of numbers, it is in the try block that the program tries to save the number in the database. If the user enters letters instead of numbers, the program leaves the try block and goes to the catch clauses. The catch clauses handle the exceptions.

In the code of the function below, the content is treated where the user must enter a number. If the user types something other than numbers, an error is raised and the exception is handled. In the try block of the function below, the value is converted to an integer. If the value is not a valid number and generates an error, the function returns zero to handle the exception.

Container Controls

Container controls are controls that serve as containers for other custom controls. In other words, they are container controls that can be used to host other controls. Containers allow you to create subdivisions of controls on the Form, or they can be used to provide a variety of different layouts.

When a container contains other controls, changing the properties of the container can also affect the controls. For example, if a Form has a Panel container containing various controls and the Enable property of the Panel is changed to false, all controls contained within the Panel will be disabled.

The Toolbox provides a collection of objects that lets you draw and edit interfaces through the drag-and-drop mechanism.

Among the containers are:

  • GroupBox: A container containing an edge and title, which appears as a subdivision of the Form. Unlike the Panel, GroupBox does not contain a scroll bar.
  • Panel: Creates a subsection of the Form that can host other controls. The Panel may be indistinguishable from the rest of the form in which it is contained.
  • TabControl: Allows grouping a set of controls into separate folders (tabs).

Visual Studio .NET Solutions

Solutions are used to group projects that are related.

The solution also contains information about the dependencies of the projects that are used in the construction process (build).

Layered Development

An application developed using the concept of development in layers is an application developed to have several logical layers.

A major goal of development in separate layers is to separate the part that is displayed to the user (presentation screens) from the part that handles calculations and validations (business rules), and to keep these separate from the part that deals with the data source (data access).

The Three Layers

Presentation Layer:

  • Handles interaction between the user and the software.
  • Displays information to the user.
  • Receives user data.

Business Layer:

  • Performs calculations based on data entered.
  • Validates information.
  • Determines which data source should be used.

Data Layer:

  • Interacts with data sources.
  • Handles Stored Procedures.
  • Manages Data Warehousing.

Benefits of Using Three-Tier Architecture

  • Organization: The application is better organized.
  • Facility: Facilitates the development of applications.
  • Reusability: Avoids duplication of code (before, any business rule was built on the presentation screen – on each screen, the business rule had to be rebuilt).
  • Flexibility: Various application layers (Web and Desktop) can use the same business layer.

The Three Layers in Detail

Presentation Layer

It is in this layer that applications are implemented that allow for interaction between the user and the software. These applications can be a website, a desktop application, or a console application.

Business Layer

It is in this layer that projects implement business logic and transfer objects.

  • Business: The project where methods are implemented for querying, inserting, changing, deleting, and other methods related to the business logic.
  • Transfer objects: The project where objects that pass between the presentation layer and business layer are implemented.

Data Layer

It is in this layer that projects for data access are implemented. The data access project is responsible for interaction with the data source. It is in this project that the connection to the database is opened (and closed). Also, it is in this layer that the operations requested by the business layer are persisted in the database.

API = Application Programming Interface is a set of routines and standards set by software to use its features for application programs.

The .NET Framework is a platform for developing and implementing systems and applications. From the viewpoint of programmers, the .NET Framework is like an operating system. It is through it that all functions necessary to operate programs under any operating system are invoked.