![]() |
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. Use the new operator to allocate memory for the control:
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:
open System; open System.Windows.Forms; let btnShow:Button = new Button(); let Starter:Form = new Form(); Starter.Controls.Add(btnShow); Application.Run(Starter);
This makes it possible for the control to appear on the form when the form displays to the user:

|
|
||
| Home | Copyright © 2010 FunctionX, Inc. | |
|
|
||