Jump to content

C# Programming/The .NET Framework/Windows Forms

From Wikibooks, open books for an open world

System.Windows.Forms

[edit | edit source]

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 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.
  • DataGrid - data grid/table view

Form class

[edit | edit source]

The Form class (System.Windows.Forms.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.

Events

[edit | edit source]

An event is an action being taken by the program when a user or the computer makes an action (for example, a button is clicked, a mouse rolls over an image, etc.). An event handler is an object that determines what action should be taken when an event is triggered.

using System.Windows.Forms;
using System.Drawing;

public class ExampleForm : Form    // inherits from System.Windows.Forms.Form
{
    public ExampleForm()
    {
        this.Text = "I Love Wikibooks";           // specify title of the form
        this.Width = 300;                         // width of the window in pixels
        this.Height = 300;                        // height in pixels

        Button HelloButton = new Button();
        HelloButton.Location = new Point(20, 20); // the location of button in pixels
        HelloButton.Size = new Size(100, 30);     // the size of button in pixels
        HelloButton.Text = "Click me!";           // the text of button

        // When clicking the button, this event fires
        HelloButton.Click += new System.EventHandler(WhenHelloButtonClick);

        this.Controls.Add(HelloButton);
    }

    void WhenHelloButtonClick(object sender, System.EventArgs e)
    {
        MessageBox.Show("You clicked! Press OK to exit this message");
    }

    public static void Main()
    {
        Application.Run(new ExampleForm());       // display the form
    }
}

Controls

[edit | edit source]


The Windows Forms namespace has a lot of very interesting classes. One of the simplest and important is the Form class. A form is the key building block of any Windows application. It provides the visual frame that holds buttons, menus, icons and title bars together. Forms can be modal and modalless, owners and owned, parents and children. While forms could be created with a notepad, using a form editor like VS.NET, C# Builder or Sharp Develop makes development much faster. In this lesson, we will not be using an IDE. Instead, save the code below into a text file and compile with command line compiler.

using System.Windows.Forms;
using System.Drawing;

public class ExampleForm : Form    // inherits from System.Windows.Forms.Form
{
    public ExampleForm()
    {
        this.Text = "I Love Wikibooks";         // specify title of the form
        this.BackColor = Color.White;
        this.Width = 300;                       // width of the window in pixels
        this.Height = 300;                      // height in pixels

        // A Label
        Label TextLabel = new Label();
        TextLabel.Text = "One Label here!";
        TextLabel.Location = new Point(20, 20);
        TextLabel.Size = new Size(150, 30);
        TextLabel.Font = new Font("Arial", 12); // See! we can modify the font of text
        this.Controls.Add(TextLabel);           // adding the control to the form

        // A input text field
        TextBox Box = new TextBox();            // inherits from Control
        Box.Location = new Point(20, 60);       // then, it have Size and Location properties
        Box.Size = new Size(100, 30);
        this.Controls.Add(Box);                 // all class that inherit from Control can be added in a form
    }

    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new ExampleForm());     // display the form
    }
}