In VB 6 days we were using VBControlExtender for loading active-x controls at run-time, but in .NET it's totally different. When we drop any active-x control on a windows form, Visual Studio add 'Ax' as suffix to the control name. .NET uses AxHost class behind the scenes to interact with active-x controls. AxHost wraps active-x controls and exposes them as fully featured Windows Forms controls. I will show you how to load 'Microsoft Calendar Control 11.0' control at run-time, how to set it's properties and call it's methods/functions.
By the way, VB.NET is my first choice of programming so I will use it for this post.
First we have to create a class which will inherit from AxHost class. It will also create a constructor for this newly created class which will accept Class GUID as a parameter. So the code goes like this;
And below is the code required to load active-x control at run-time;
Public Class AxControl
Inherits AxHost
Public Sub New(ByVal strCLSID As String)
MyBase.New(strCLSID)
End Sub
End Class
Above code gets the instance of the type from the class name "MSCAL.Calendar.7" which will be used to get the GUID of the active-x. Using this GUID we create the instance of the active-x control. Next we need to initialize the control. BeginInit method signals the control object that initializing is starting. After this we set some required control properties and end the initialization.
' Declarations
Dim WithEvents m_axCtrl As AxControl
Dim strProgId As String
Dim m_type As Type
Dim args As Object()
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
strProgId = "MSCAL.Calendar.7"
m_type = System.Type.GetTypeFromProgID(strProgId, True)
m_axCtrl = New AxControl(m_type.GUID.ToString())
DirectCast((m_axCtrl), System.ComponentModel.ISupportInitialize).BeginInit()
m_axCtrl.Enabled = True
m_axCtrl.Name = "axCtrl"
m_axCtrl.TabIndex = 0
m_axCtrl.Left = 10
m_axCtrl.Top = 10
m_axCtrl.Width = 250
m_axCtrl.Height = 250
Controls.Add(m_axCtrl)
DirectCast((m_axCtrl), System.ComponentModel.ISupportInitialize).EndInit()
End Sub
So this few lines of code will load the active-x control at run-time. Now, I will show you how to set properties and call methods.
For calling methods and setting properties syntax is like this;
Call method:
Dim args As Object()
args = New Object() {"Method Parameters"}
m_type.InvokeMember("MethodName", Reflection.BindingFlags.InvokeMethod, Nothing, m_axCtrl.GetOcx, args)
args = New Object() {"PropertyValue"}
m_type.InvokeMember("Property Name", System.Reflection.BindingFlags.SetProperty, Nothing, m_axCtrl.GetOcx, args)
Set property:
m_type.InvokeMember("NextDay", Reflection.BindingFlags.InvokeMethod, Nothing, m_axCtrl.GetOcx, New Object() {})
Get property value:
args = New Object() {"01/01/2009"}
m_type.InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, Nothing, m_axCtrl.GetOcx, args)
So this is all about loading Active-X control at run-time in .NET, accessing properties and calling it's methods.
Dim selDate As Date
selDate = m_type.InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, Nothing, m_axCtrl.GetOcx, New Object() {})
MessageBox.Show(selDate.ToString("dd/MM/yyyy"))