.Net Runtime Library for Delphi
Close
Place .Net Controls on VCL Form

Microsoft .NET controls give developers the leverage they need to build modular solutions. A developer can design custom controls from a myriad of existing controls and embed the custom control into a Windows Form or another control. With some additional code, one can also embed those controls in a Web Form. Using the host control container called TClrContainer will allow you to place .net controls on delphi VCL forms. The TClrContainer Control is a container control to host a group of .net controls. It is used to group collections of .Net controls. This control type is defined in the namespace CNClrLib.Comp .

Creating a ClrContainer

We can create a ClrContainer Control using the Forms designer at design-time or using the TClrContainer class in code at run-time.

Design Time

To create a Panel Control at design-time, you can drag and drop a Panel Control from the Toolbox to a Form in Delphi IDE. After you dragging and dropping a ClrContainer Control to the Form, the control looks like Figure 1. Once a ClrContainer is on the form, you can move it around and resize it using the mouse and set its properties and events.

Figure 1

Run-time

Creating a ClrContainer Control at run-time is merely a work of creating an instance of the TClrContainer class, setting its properties and adding the TClrContainer to the form controls. The first step to create a dynamic ClrContainer is to create an instance of the TClrContainer class. The following code snippet creates a TClrContainer Control object.

Delphi
.... var dynamicCtrl: TClrContainer; begin dynamicCtrl := TClrContainer.Create(Self); ....
 

The next step is to add the ClrContainer to a form so it becomes a part of the form. To do so, set the parent property of the control to the form. The following code snippet adds a ClrContainer Control to the current form.
Delphi
dynamicCtrl.Parent := Self; //Self is the form dynamicCtrl.Name := 'ClrContainer1'; dynamicCtrl.Align := alClient; dynamicCtrl.TabOrder := 0;



 
 

Adding .Net Controls to a ClrContainer

Adding .net controls to the ClrContainer can be done at runtime. Use the overload method AddClrControl to add the instance of the .net control created to the container control. The following code snipper demonstrates how an instance of a .Net DatagridView control can be added to the ClrContainer.

Delphi
///Create Instance of the DatagridView control and set it properties FDataGridView := CoDataGridView.CreateInstance; FDataGridView.Dock := DockStyle_Fill; FDataGridView.AutoGenerateColumns := True; ///Temporarily suspends the layout logic for the control. ClrContainer1.SuspendLayout; //Add the DatagridView control to the host control container ClrContainer1.AddClrControl(FDataGridView); //Resumes usual layout logic. ClrContainer1.ResumeLayout();













 

Example

The example below demonstrates how to load data from the database, bind it to a DatagridView control which is added to the ClrContainer and display the gridview result on the VCL form.

Delphi
unit Examples; {$WARN SYMBOL_PLATFORM OFF} interface uses System.SysUtils, System.Variants, System.Classes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, CNClrLib.Windows, CNClrLib.Enums, Vcl.ExtCtrls, CNClrLib.Data, CNClrLib.Comp; type TForm1 = class(TForm) Panel2: TPanel; btnLoadData: TButton; ClrContainer1: TClrContainer; procedure FormShow(Sender: TObject); procedure btnLoadDataClick(Sender: TObject); private FDataGridView: _DataGridView; FBindingSource: _BindingSource; public { Public declarations } procedure LoadData; procedure AddHostControl; end; var Form1: TForm1; implementation {$R *.dfm} uses CNClrLib.Core; { TForm1 } /// <summary> /// Create and instance of the .Net DataGridView control and the .Net BindingSource component /// Set the Properties of the .Net DataGridView control and assign the DataSource property to the .Net BindingSource component /// Add the .Net DatagridView control to the ClrContainer /// </summary> procedure TForm1.AddHostControl; begin ///Create Instance of the DatagridView control and set it properties FDataGridView := CoDataGridView.CreateInstance; FDataGridView.Dock := DockStyle_Fill; FDataGridView.AutoGenerateColumns := True; ///Create Instance of the BindingSource component and assign the component to the Datasource property of the DataGridView control FBindingSource:= CoBindingSource.CreateInstance; FDataGridView.DataSource := FBindingSource.Unwrap; ///Temporarily suspends the layout logic for the control. ClrContainer1.SuspendLayout; //Add the DatagridView control to the host control container ClrContainer1.AddClrControl(FDataGridView); //Resumes usual layout logic. ClrContainer1.ResumeLayout(); end; /// <summary> /// Click event which loads the data from database and display on the DataGridView control /// </summary> procedure TForm1.btnLoadDataClick(Sender: TObject); begin LoadData; end; /// <summary> /// On Form show event will call the AddHostControl method /// </summary> procedure TForm1.FormShow(Sender: TObject); begin AddHostControl; end; /// <summary> /// Load data using ADO.Net and bind the data retrieved from the database to the .Net DataGridView control using the bindingsource /// </summary> procedure TForm1.LoadData; var conStr, SQL: String; adapter: _SqlDataAdapter; commandBuilder: _SqlCommandBuilder; table: _DataTable; begin try conStr := 'Data Source=MyDataSource; Initial Catalog=MyDatabase; Integrated Security=true;'; SQL := 'Select * from My_Table'; adapter := CoSqlDataAdapter.CreateInstance(SQL, conStr); commandBuilder := CoSqlCommandBuilder.CreateInstance(adapter); table := CoDataTable.CreateInstance; table.Locale := CoCultureInfoHelper.CreateInstance.InvariantCulture; adapter.Fill_3(table); FBindingSource.DataSource := table.Unwrap; except on E: Exception do ShowMessage(E.Message); end; end; end.



















Form at Design Time
Form at Run-Time