|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Designing a User Interface (Visual C#)
In Visual C#, the most rapid and convenient way to create your user interface (UI) is to do so visually, using the Windows Forms Designer and Toolbox. There are three basic steps in creating all user interfaces:
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 more rapidly than is possible by manual coding.
You can also use Visual C# to create console applications that have a simple text-based UI. For more information, see.
In the designer, you use the mouse to drag , such as buttons and text boxes, onto a design surface that represents your form. The following illustration shows a combo box that has been dragged from the Toolbox window 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 called <name>.designer.cs where <name> is the name 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.
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 simply 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 simply 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.
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 happens, 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 it is up to you to write the code to make the method do something useful. Most controls generate a large number of events, but in most cases an application will only need to handle a few of them, or even only one. For example, you probably need to handle a button's Click event, but you do not need to handle its Paint event unless you want to customize its appearance in some advanced way.
For more information about Windows Forms user interfaces, see the following topics:
In the .NET Framework class library,and related namespaces contain the classes used in Windows Forms development.