The text of this article is not in this database — only its details are. Read it on the old site: Using your C# Components in Visual Basic through COM
1 Comment
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.

Vidya BhatPosted Mar 6, 2010, 3:43 AM
Hello Mike, We have a VB application that hosts VB.Net (framework 2.0) user controls via VC++ 6.0 type libraries. In the VB.Net user control we have a data grid view with text box, combo box and check box. When hosted to VB form via VC++ type libraries, we are not able to edit the text box. (there are other problems too, like combo drop down appears in some ohter place of the form, check box gets checked only sometimes etc.) The same dot net user control works fine when hosted onto VB directly, without using these VC++ type libraries. The following is the interface of the type library [ uuid (9EF896B1-AB19-11d3-9A8E-0050DA3393D8), version (1.0), dual, nonextensible, oleautomation ] interface ICustomControl : IDispatch { // Property: CallBack [ helpstring("Set/get a reference to the control's CallBack object, through which it will send events.")] [propget] HRESULT CallBack([out, retval] ICallBack** ); [propputref] HRESULT CallBack([in] ICallBack* icbPipe); // Property: hWnd [ helpstring("Returns the control's window handle.")] [propget] HRESULT hWnd([out, retval] long* ); // Property: Visible [helpstring("Show/hide control.")] [propget] HRESULT Visible([out, retval] VARIANT_BOOL* ); [propput] HRESULT Visible([in] VARIANT_BOOL ); // Property: Locked [ helpstring("Enable/disable data editing. Setting locked will set the control in a \"read-only\" mode.")] [propget] HRESULT Locked([out, retval] VARIANT_BOOL* ); [propput] HRESULT Locked([in] VARIANT_BOOL fLock); // Function: Initialize [ helpstring("Initialize user control with data to be shown.") ] HRESULT Initialize([in] VARIANT vInData); // Function: Refresh [ helpstring("Force repaint of control.")] HRESULT Refresh(); // Function: SetFocus [ helpstring("Set focus in control.")] HRESULT SetFocus(); } The above interface is implemented in dot net user control (because VB is expecting this) The following is the user control code. Imports System.Runtime.InteropServices Imports ABCTypeLib Imports System.Windows.Forms Imports System.Drawing Imports Microsoft.VisualBasic.Compatibility Public Class ucCustomSetup Inherits System.Windows.Forms.UserControl Implements AnsurTypeLib.ICustomControl <DllImport("user32.dll")> _ Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr End Function Dim intAdded As Integer Private _objCallBack As ICallBack Private _objConfigData As IConfigData Private mfInitializing As Boolean Private _testId As Long Private _character As String Private cmbWorkflow As ComboBox Private dtbShortlist As DataTable Public WithEvents cmbFunctionArea As System.Windows.Forms.ComboBox ''------------------------------------------------------------------------------- ' Function : SendChangeEvent ' Synopsis: Sends a change event to ansur. '------------------------------------------------------------------------------- Private Sub SendChangeEvent() On Error Resume Next If Not (mfInitializing Or (_objCallBack Is Nothing)) Then _objCallBack.SendEvent(AnsurTypeLib.CallBackConstants.CALLBACK_CHANGE) End If End Sub Public Property CallBack() As AnsurTypeLib.ICallBack Implements AnsurTypeLib.ICustomControl.CallBack Get CallBack = _objCallBack End Get Set(ByVal value As AnsurTypeLib.ICallBack) _objCallBack = value End Set End Property Public ReadOnly Property hWnd() As Integer Implements AnsurTypeLib.ICustomControl.hWnd Get 'hWnd = MyBase.Handle.ToInt32() hWnd = MyBase.Handle.ToString() End Get End Property Public Sub Initialize(ByVal vInData As Object) Implements AnsurTypeLib.ICustomControl.Initialize mfInitializing = True PopulateGrid() End Sub Public Property Locked() As Boolean Implements AnsurTypeLib.ICustomControl.Locked Get Return False End Get Set(ByVal value As Boolean) DataGridView1.ReadOnly = value End Set End Property Public Sub Refresh1() Implements AnsurTypeLib.ICustomControl.Refresh MyBase.Refresh() End Sub Public Sub SetFocus() Implements AnsurTypeLib.ICustomControl.SetFocus DataGridView1.Focus() End Sub Public Property Visible1() As Boolean Implements AnsurTypeLib.ICustomControl.Visible Get Visible1 = MyBase.Visible End Get Set(ByVal value As Boolean) MyBase.Visible = value End Set End Property Private Sub ucCustomSetup_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize Dim X, dx As Single Dim Y, dy As Single 'Resize unit page. AxXHelpText1.SetBounds(VB6.TwipsToPixelsX(X), VB6.TwipsToPixelsY(Y + dy), VB6.TwipsToPixelsX(dx), VB6.TwipsToPixelsY(0)) End Sub Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick SendChangeEvent() End Sub Private Sub DataGrid1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGrid1.KeyPress, DataGridView1.KeyPress SendChangeEvent() End Sub Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate End Sub Private Sub DataGridView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick SendChangeEvent() End Sub End Class Please help us. - Vidya