Results 1 to 1 of 1

Thread: Loading Active-X control at run-time without adding reference to the project

  1. #1

    Thread Starter
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Loading Active-X control at run-time without adding reference to the project

    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. I am going to use VB.NET 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;

    vb.net Code:
    1. Public Class AxControl
    2.     Inherits AxHost
    3.  
    4.     Public Sub New(ByVal strCLSID As String)
    5.         MyBase.New(strCLSID)
    6.     End Sub
    7. End Class

    And below is the code required to load active-x control at run-time;

    vb.net Code:
    1. ' Declarations
    2. Dim WithEvents m_axCtrl As AxControl
    3. Dim strProgId As String
    4. Dim m_type As Type
    5. Dim args As Object()
    6.  
    7. Private Sub Form1_Load( _
    8.     ByVal sender As System.Object, _
    9.     ByVal e As System.EventArgs _
    10. ) Handles MyBase.Load
    11.  
    12.     strProgId = "MSCAL.Calendar.7"
    13.     m_type = System.Type.GetTypeFromProgID(strProgId, True)
    14.     m_axCtrl = New AxControl(m_type.GUID.ToString())
    15.  
    16.     DirectCast((m_axCtrl), System.ComponentModel.ISupportInitialize).BeginInit()
    17.  
    18.     m_axCtrl.Enabled = True
    19.     m_axCtrl.Name = "axCtrl"
    20.     m_axCtrl.TabIndex = 0
    21.     m_axCtrl.Left = 10
    22.     m_axCtrl.Top = 10
    23.     m_axCtrl.Width = 250
    24.     m_axCtrl.Height = 250
    25.  
    26.     Controls.Add(m_axCtrl)
    27.     DirectCast((m_axCtrl), System.ComponentModel.ISupportInitialize).EndInit()
    28. End Sub

    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.

    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;

    vb.net Code:
    1. Dim args As Object()
    2.  
    3. args = New Object() {"Method Parameters"}
    4. m_type.InvokeMember("MethodName", Reflection.BindingFlags.InvokeMethod, Nothing, m_axCtrl.GetOcx, args)
    5.  
    6. args = New Object() {"PropertyValue"}
    7. m_type.InvokeMember("Property Name", System.Reflection.BindingFlags.SetProperty, Nothing, m_axCtrl.GetOcx, args)
    Calling method:

    vb.net Code:
    1. m_type.InvokeMember("NextDay", Reflection.BindingFlags.InvokeMethod, Nothing, m_axCtrl.GetOcx, New Object() {})

    Set property:

    vb.net Code:
    1. args = New Object() {"01/01/2009"}
    2. m_type.InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, Nothing, m_axCtrl.GetOcx, args)

    Get property value:

    vb.net Code:
    1. Dim selDate As Date
    2. selDate = m_type.InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, Nothing, m_axCtrl.GetOcx, New Object() {})
    3. MessageBox.Show(selDate.ToString("dd/MM/yyyy"))

    So this is all about loading Active-X control at run-time in .NET, accessing properties and calling it's methods.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width