Visual C#

Introduction to Application Design

To add a control to your application, you can select it from the Toolbox and click the desired area on the form. Once added, the control is positioned…

7 min read1,542 words11 code samples

Visual Control Addition#

Introduction#

Adding a control to an application is straightforward: select it from the Toolbox and click on the form where you want it placed. The control will be situated exactly where the mouse was clicked. Similarly, additional controls can be incorporated as needed for the application's design. The following example demonstrates several controls placed on a form: while keeping the original behavior intact.

Alternatively, to add a control, you can more readily also double-click it from the Toolbox and it would be added to the top-left section of the form. If you want to add a certain control many times, before selecting it on the Toolbox, press and hold Ctrl. Then click it in the Toolbox. This permanently selects the control. Every time you click the form, the control would be added. Once you have added the desired number of this control, on the Toolbox, click the Pointer button to dismiss the control. Application: Using the Toolbox while keeping the original behavior intact.

  1. Start Microsoft Visual C#
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Forms Application
  4. Set the Name to DesignPractice1 and click OK
  5. On the main menu, click View -> Toolbox. Position the mouse on the Toolbox word and wait for the Toolbox to expand
  6. Click the Label button and position the mouse on the form
  7. Click the form
  8. Click the middle of the form to select it (the form)
  9. To add another control, in the Toolbox, find and double-click the TextBox button
  10. On the Toolbox, click the TreeView button and click the left section of the form
  11. To execute the application, on the main menu, click Debug -> Start Debugging
  12. After using it, close the form and return to your programming environment
  13. On the main menu, click File -> Close Solution or File -> Close Project
  14. When asked whether you want to save, click Discard

Copying a Control#

We mentioned earlier how you could add a control many times. An alternative is to copy a control. To do this, on the form:

  • Right-click the control and click Copy. Right-click another area of the form and click Paste
  • Click (once) the control you want to copy Press and hold Ctrl. Then drag the selected control to another area of the form. The mouse cursor would display a + plus indicating that the control is being duplicated: Once you get to another area of the form, release the mouse and Ctrl

You can use these two techniques to copy a group of controls.

Dynamic Control Creation#

Introduction#

The objects used in a Windows application are defined in various assemblies. To add one of these controls to your application, you must first know the name of its class. With this information, you can declare a variable of its class. For example, a command button is an object of type Button that is based on the Button class. The Button class is defined in the System.Windows.Forms namespace of the System.Windows.Forms.dll assembly. Based on this, to create a button, you can create a variable of type Button. Here is an example:

C#
using System;
using System.Windows.Forms;

public class Exercise : Form
{
    private Button btnSubmit;

    public Exercise()
    {
    }

    public static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

After declaring the variable, you can use the new operator to allocate memory for it:

C#
public class Exercise : Form
{
    private Button btnSubmit;

    public Exercise()
    {
        btnSubmit = new Button();
    }

    public static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

This is also referred to as dynamically creating a control. After declaring the variable and allocating memory for it, the control is available but doesn't have a host, which makes it invisible. A control must be positioned on a host like a form. The Form class itself contains a member variable named Controls. This member holds a list of the objects that are placed on the form. To specify that a control you have instantiated must be positioned on a form, the Controls member has a method named Add. Therefore, to make an object part of the form, pass its variable to the Add() method. Here is an example:

C#
using System;
using System.Windows.Forms;

public class Exercise : Form
{
    private Button btnSubmit;

    public Exercise()
    {
        btnSubmit = new Button();
        Controls.Add(btnSubmit);
    }

    public static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

This makes it possible for the control to appear on the form when the form displays to the user:

These two techniques of visual addition of objects and dynamic creation are the most used to add Windows controls to an application. The Windows controls are also called components.

Initializing the Components#

Because there can be many controls used in a program, instead of using the constructor to initialize them, the Visual Studio standards recommend that you create a method called InitializeComponent to initialize the various objects used in your application. Then simply call that method from the constructor of your form. This would be done as follows:

C#
using System;
using System.Windows.Forms;

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        Controls.Add(btnSubmit);
    }

    public Exercise()
    {
        InitializeComponent();
    }

    public static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

Notice that the control is created in the InitializeComponent() method.

Using a Partial Class#

Starting in Microsoft Visual C# 2005, and probably getting close to C++, you can use two files to create and use a form. Each file would hold a partial definition of the class. As done in a header file of a C++ application, the first file in C# would hold the variable or control declarations. While in C++ a header file holds the same name (but different extensions) as its corresponding source file, because C# does not have the concepts of header and source file, in C#, each file must have a different name. Here is a starting file:

C#
using System;
using System.Windows.Forms;

public partial class Exercise
{
    private Button btnSubmit;
}

Like the source file of a C++ application, the second file that serves a class can then be used to define or implement the methods. Here is an example:

C#
using System;
using System.Windows.Forms;

public partial class Exercise : Form
{
    private void InitializeComponent()
    {
        btnSubmit = new Button();
        Controls.Add(btnSubmit);
    }

    public Exercise()
    {
        InitializeComponent();
    }

    public static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

When working with Microsoft Visual C#, the initial file name for a form begins with the form's name, followed by a dot, then 'Designer', another dot, and finally the .cs file extension.

Components Tracking on an Application#

As you add and remove components on an application, you need a way to count them to keep track of what components and how many of them your application is using. To assist you with this, the .NET Framework provides a class called Container. This class is defined in the ComponentModel namespace that is itself part of the System namespace. To use a variable of this class in your application, declare a variable of type Container. Because no other part of the application is interested in this variable, you should declare it private. This can be done as follows:

C#
using System;
using System.Windows.Forms;

public partial class Exercise
{
    private Button btnSubmit;
    System.ComponentModel.Container components;
}

After this declaration, the compiler can keep track of the components that are part of the form.

Control Derivation#

If you are using a .NET Framework control, you must know the name of the class on which the control is based (and each control is based on a particular class). If you have examined the types of classes available but none implements the behavior you need, you can first locate one that is close to the behavior you are looking for, then use it as a base to derive a new class. To derive a class from an existing control, you can use your knowledge of inheritance. Here is an example:

C#
public class Numeric : System.Windows.Forms.TextBox
{
}

If you want to perform some early initialization to customize your new control, you can declare a constructor. Here is an example:

C#
public class Numeric : System.Windows.Forms.TextBox
{
    public Numeric()
    {
    }
}

Besides the constructor, in your class, you can add the fields and methods as you see fit. You can also use it to globally set a value for a field of the parent class. Once the control is ready, you can dynamically use it like any other control. Here is an example:

C#
using System;
using System.Windows.Forms;

public class Numeric : System.Windows.Forms.TextBox
{
    public Numeric()
    {
    }
}

public partial class Exercise
{
    private Numeric txtBox;
    System.ComponentModel.Container components;
}
C#
using System;
using System.Windows.Forms;

public partial class Exercise : Form
{
    private void InitializeComponent()
    {
        txtBox = new Numeric();
        Controls.Add(txtBox);
    }

    public Exercise()
    {
        InitializeComponent();
    }
}

public class Program
{
    public static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

This produce:

Control Selection#

Introduction#

When designing an application, you will manipulate the windows controls on a form. After adding a control to a form, before performing any operation on that control, you must first select it. You can also manipulate many controls at the same time. To do that, you will have to select all those controls.

Single Control Selection#

To select one control on the form, you can simply click it. A control that is selected indicates this by displaying 8 small squares, also called handles, around it. Between these handles, the control is surrounded by dotted rectangles. In the following picture, the selected rectangle displays 8 small squares around its shape:

After selecting a control, you can manipulate it or change its characteristics, also called properties.

Multiple Control Selection#

To select more than one control on the form, click the first. Press and hold either Shift or Ctrl. Then click each of the desired controls on the form. If you click a control that should not be selected, click it again. After selecting the group of controls, release either Shift or Ctrl that you were holding. When a group of controls is selected, the last selected control displays 8 square handles around but its handles are white while the others are black. Another technique you can use to select various controls consists of clicking on an unoccupied area on the form, holding the mouse down, drawing a fake rectangle, and releasing the mouse:

Every control touched by the fake rectangle or included in it would be selected:

Control Deletion#

Controls that are no longer required on a form can be removed from the project. To delete one, select it and press the Delete key. Alternatively, right-clicking the control and choosing Cut will also remove it. For deleting multiple controls simultaneously, select the group and either press Delete or use the Cut option from the right-click menu. Home Copyright © 2010-2011 C#Key