Designing a User Interface in Visual C#
In Visual C#, the most rapid and convenient way to create a user interface (UI) for a Windows application is to use the Windows Forms Designer and Toolbox.
Note
This article discusses only Windows Forms technology. You can also use Visual C# to create a Windows Presentation Foundation (WPF) application. For more information, see Introduction to Windows Presentation Foundation and How to: Create a New WPF Application Project.
You can also use Visual C# to create console applications (similar to MS-DOS programs) that have a simple text-based UI. For more information, see How to: Create a C# Console Application.
There are three basic steps in creating user interfaces for Windows Forms applications:
Adding controls to the design surface.
Setting initial properties for the controls.
Writing handlers for specified events.
Although you can also create your UI by writing your own code, designers enable you to do this work much faster than is possible by manual coding.
Adding Controls
In the designer, you use the mouse to drag controls, such as buttons and text boxes, onto a design surface that represents the form. The following illustration shows a combo box that has been dragged from the Toolbox onto a form in the Windows Forms Designer.
As you work visually, the designer translates your actions into C# source code and writes them into a project file that is named <name>.designer.cs where <name> is the name that you gave to the form. When your application runs, that source code will position and size your UI elements so that they appear just as they do on the design surface. For more information, see Windows Forms Designer.
Setting Properties
After you add a control to your form, you can use the Properties window to set its properties, such as background color and default text. The values that you specify in the Properties window are the initial values that will be assigned to that property when the control is created at run time. In many cases, those values can be accessed or changed programmatically at run time by getting or setting the property on the instance of the control class in your application. The Properties window is useful at design time because it enables you to browse all the properties, events, and methods supported on a control. For more information, see Properties Window.
Handling Events
Programs with graphical user interfaces are primarily event-driven. They wait until a user does something such as entering text into a text box, clicking a button, or changing a selection in a list box. When that occurs, the control, which is just an instance of a .NET Framework class, sends an event to your application. You have the option of handling an event by writing a special method in your application that will be called when the event is received.
You can use the Properties window to specify which events you want to handle in your code. Select a control in the designer and click the Events button, with the lightning bolt icon, on the Properties window toolbar to see its events. The following diagram shows the events button.
When you add an event handler through the Properties window, the designer will automatically write the empty method body for you, and you must write the code to make the method do something useful. Most controls generate many events, but most of the time an application will only have to handle several of them, or even only one. For example, you probably have to handle a button's Click event, but you do not have to handle its Paint event unless you want to customize its appearance in some advanced way. Each control has a default event handler. You can create the default event handler by double-clicking the control in the designer. This creates the event handler and opens the Code Editor so that you can write code to handle the event.
Next Steps
For more information about Windows Forms user interfaces, see the following topics:
In the .NET Framework class library, System.Windows.Forms and related namespaces contain the classes used in Windows Forms development.
See Also
Tasks
How to: Create a New Visual C# Express Application
Other Resources
Adding Multimedia to an Application
Creating and Using Bitmaps and Icons