Export (0) Print
Expand All

Designing a User Interface (Visual C#)

In Visual C#, you can use either the Windows Form Designer or the Windows Presentation Foundation (WPF) Designer to quickly and conveniently create user interfaces. For information to help you decide what type of application to build, see Overview of Windows-based Applications. There are three basic steps in creating 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 manually writing your own code, designers enable you to do this work much faster.

NoteNote:

You can also use Visual C# to create console applications that have a simple text-based UI. For more information, see Creating Console Applications (Visual C#).

In either designer, you use the mouse to drag controls, which are components with visual representation such as buttons and text boxes, onto a design surface. The following illustration shows a combo box that has been dragged from the Toolbox window onto a form in the Windows Forms Designer.

Windows Forms Designer and Toolbox

As you work visually, the Windows Forms 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. Similarly, the WPF designer translates actions on the design surface into Extensible Application Markup Language (XAML) code and writes it into a project file that is named Window.xaml. When your application runs, that source code (Windows Form) or XAML (WPF) 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 or Windows Presentation Foundation in Visual Studio.

After you add a control to the design surface, you can use the Properties window to set its properties, such as background color and default text.

In the Windows Form designer, 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 the WPF designer, the values that you specify in the Properties window are stored as attributes in the window's XAML file.

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.

Programs with graphical user interfaces are primarily event-driven. They wait until a user does something such as typing 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 icon shows the events button.

Events Button in the Properties Window

When you add an event handler through the Properties window, the designer automatically writes the empty method body. You must write the code to make the method do something useful. Most controls generate many events, but frequently an application will only have to handle some 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 SizeChanged event unless you want to do something when the size of the button changes.

For more information about how to build Windows Forms or Windows Presentation Foundation user interfaces, see the following topics:

In the .NET Framework class library, the System.Windows.Forms namespace contains the classes used in Windows Forms applications and the System.Windows.Controls namespace contains the classes used in WPF applications.

Community Additions

ADD
Show:
© 2014 Microsoft