Create a ActiveX user control (VB.NET2005)

 The problem with that example is that is does not register the control, and the example will only work for putting it in a web page.

I did get this to work. The .Net user control can be hosted in VB6, VBA web forms, but not, I found, all ActiveX containers. Here are the details for those interested in what I did.

1. Create a VB.NEt user control (VB 2005)
2. Put a button on the control
3. Set project property "Register for COM interop"
4. Modify the usercontrol1.vb as show below
BUILD VB.Net user control which will register the ActiveX control
5. In VB6, add control (assembly namespace.MyControl")
Code:
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.IO
Imports System.Reflection
Imports Microsoft.Win32
Imports System
Imports System.Threading
Imports System.Math

<ComClass(MyControl.ClassId, _
MyControl.InterfaceId, _
MyControl.EventsId)> _
Public Class MyControl
    ' MAKE SURE WE HAVE 1 PUBLIC SUB in this class

#Region "COM GUIDs"
    ' These GUIDs provide the COM identity for this class
    ' and its COM interfaces. If you change them, existing
    ' clients will no longer be able to access the class.
    'You should create your own 3 GUIDS using GuidGen
    Public Const ClassId As String = "8D6CC4E9-1AE1-4909-94AF-8A4CDC10C466"
    Public Const InterfaceId As String = "D901FC53-EEC2-4634-A1B5-BB4E41B24521"
    Public Const EventsId As String = "7458968F-F760-4f53-A2E4-30C1D8CD691B"
#End Region

#Region "REQUIRED FOR ACTIVEX"
    ' This function is called when registered (no need to change it)
    <ComRegisterFunction()> _
    Private Shared Sub ComRegister(ByVal t As Type)
        Dim keyName As String = "CLSID\\" & t.GUID.ToString("B")
        Dim key As RegistryKey = Registry.ClassesRoot.OpenSubKey(keyName, True)

        key.CreateSubKey("Control").Close()
        Dim subkey As RegistryKey = key.CreateSubKey("MiscStatus")
        subkey.SetValue("", "131201")
        subkey = key.CreateSubKey("TypeLib")
        Dim libid As Guid = Marshal.GetTypeLibGuidForAssembly(t.Assembly)
        subkey.SetValue("", libid.ToString("B"))
        subkey = key.CreateSubKey("Version")
        Dim ver As Version = t.Assembly.GetName().Version
        Dim version As String = String.Format("{0}.{1}", ver.Major, ver.Minor)
        If version = "0.0" Then version = "1.0"
        subkey.SetValue("", version)

    End Sub

    ' This is called when unregistering (no need to change it)
    <ComUnregisterFunction()> _
    Private Shared Sub ComUnregister(ByVal t As Type)
        ' Delete entire CLSID¥{clsid} subtree
        Dim keyName As String = "CLSID\\" & _
            t.GUID.ToString("B")
        Registry.ClassesRoot.DeleteSubKeyTree(keyName)
    End Sub

#End Region
    Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("OK")
    End Sub
End Class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值