Home

Introduction to Applications Controls

   

Visual Control Addition

 

Introduction to the Client Area

On a form, the client area is the body of the form without the title bar, its borders and other sections we have not mentioned yet such as the menu, scroll bars, etc:

  
Client Area

Besides the form, every control also has a client area. The role of the client area is to specify the bounding section where the control can be accessed by other controls positioned on it. Based on this, a control can be visible only within the client area of its parent. Not all controls can be parent.

Introduction to Dynamic Control Creation

The objects used in a Windows application are defined in various assemblies. To use one of these controls, 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 declare a variable of type Button. Here is an example:

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:

public class Exercise : Form
{
    private Button btnSubmit;

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

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

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.

ApplicationTopic Applied: Introducing Controls

  1. Start Notepad
  2. Type the following:
    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;
        }
    }
    
  3. To save the file, on the main menu, click File -> Save
  4. Locate the Exercise1 folder from the root of the C: drive and display it in the top combo box
  5. Change the name of the file to "Introduction.cs"
  6. Click Save
  7. To launch the Command Prompt, click Start
  8. In the text box, type cmd and Press Enter
  9. To switch to the root directory, type CD\ and press Enter
  10. To switch to the folder that contains the file, type CD Exercise1 and press Enter
  11. To build the project:
    • If you have admin rights on the computer and if you had changed the path in the Environment Variables, type the following:
      csc /r:System.Windows.Forms.dll /target:winexe Exercise.cs
    • If you don't have admin rights on the computer and you had not changed the path in the Environment Variables, type the following:
      C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc /r:System.Windows.Forms.dll /target:winexe Exercise.cs
      
      
  12. Press Enter
  13. To execute, type Introduction and press Enter
     
    Introduction to Controls
  14. To close the form, click its system Close button

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:

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:

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.

ApplicationTopic Applied: Using a Partial Class

  1. On the main menu of Notepad, click File -> New
  2. Type the following:
    using System;
    using System.Windows.Forms;
    
    public class Numeric : System.Windows.Forms.TextBox
    {
        public Numeric()
        {
        }
    }
    
    public partial class Exercise
    {
        private Numeric txtBox;
    }
  3. To start a new file, on the main menu of Notepad, click File -> New
  4. When asked whether you want to save the file, lick Yes
  5. Make sure the Exercise1 folder from the root of the C: drive is displaying in the top combo box.
    Change the name of the file to "Exercise1.cs"
  6. Click Save
  7. In the empty document, type:
    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;
        }
    }
  8. On the main menu of Notepad, click File -> Open...
  9. When asked whether you want to save, click Yes
  10. Set the name to "Exercise2.cs" and click Save
  11. Return to the Command Prompt
  12. To build the project:
    • If you have admin rights on the computer and if you had changed the path in the Environment Variables, type the following:
      csc /r:System.Windows.Forms.dll /target:winexe Exercise.cs
    • If you don't have admin rights on the computer and you had not changed the path in the Environment Variables, type the following:
      C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc /r:System.Windows.Forms.dll /target:winexe Exercise.cs
      
      
  13. Press Enter
  14. To execute, type Exercise2 and press Enter
     
    Partial Classes
  15. To close the form, click its system Close button

Components Tracking on an Application

As you add components to 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:

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 class inheritance. Here is an example:

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

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

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:

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;
}
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;
    }
}
 

ApplicationTopic Applied: Ending the Lesson

  1. To close the Command Prompt, get to it and type exit
  2. Press Enter
 

Previous Copyright © 2011 FunctionX Next