C Sharp Programming/The .NET Framework/Windows Forms

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] System.Windows.Forms namespace

To create a Windows desktop application we use the library represented by System.Windows.Forms namespace. Some commonly used classes in this namespace include:

  • Control - generic class from which other useful classes, like Form, TextBox and others listed below , are derived
  • Form - this is the base class for the program window. All other controls are placed directly onto a Form or indirectly on another container (like TabPage or TabControl) that ultimately resides on the Form. When automatically created in Visual Studio, it is usually subclassed as "Form1".
  • Button - a clickable button
  • TextBox - a singleline or multiline textbox that can be used for displaying and/or inputting text
  • RichTextBox - an extended TextBox that can display styled text, e.g. with parts of the text colored or with a specified font. RichTextBox can also display generalized RTF document, including embedded images.
  • Label - simple control allowing display of a single line of unstyled text, often used for various captions and titles
  • ListBox - control displaying multiple items (lines of text) with ability to select an item and to scroll through it
  • ComboBox - similar to ListBox but resembling a dropdown menu
  • TabControl and TabPage - used to group controls in a tabbed interface (much like tabbed interface in Visual Studio or Mozilla Firefox). A TabControl contains a collection of TabPage objects.

[edit] Form class

The Form class (System.Windows.Form) is a particularly important part of that namespace because the form is the key graphical building block of Windows applications. It provides the visual frame that holds buttons, menus, icons, and title bars together. Integrated development environments (IDEs) like Visual C# and SharpDevelop can help create graphical applications, but it is important to know how to do so manually:

using System.Windows.Forms;
public class ExampleForm : Form    // inherits from System.Windows.Forms.Form
{
   public static void Main()
   {
       ExampleForm wikibooksForm = new ExampleForm();
       wikibooksForm.Text = "I Love Wikibooks";// specify title of the form
       wikibooksForm.Width = 400;              // width of the window in pixels
       wikibooksForm.Height = 300;             // height in pixels
       Application.Run(wikibooksForm);         // display the form
   }
}

The example above creates a simple Window with the text "I Love Wikibooks" in the title bar. Custom form classes like the example above inherit from the System.Windows.Forms.Form class. Setting any of the properties Text, Width, and Height is optional. Your program will compile and run successfully if you comment these lines out, but they allow us to add extra control to our form.

[edit] Events

Event is a action being taken by the program when,say, for example, a Button is clicked. Event handler is a object that handles the event, that is, what action should be taken when an event is fired.

[edit] Controls