none
C# Events to COM Interop RRS feed

  • Question

  • Hi I have this C# code

        [ComVisible(true)]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        [Guid("20B4B529-5FE0-49ED-A225-AD376E48DA5A")]
        public interface ITextBoxEvents
        {
            [DispId(1)] void GotFocus();
        }
    
        [ComVisible(true)]  
        [ClassInterface(ClassInterfaceType.None)]    
        [ComSourceInterfaces(typeof(ITextBoxEvents))]
        [Guid("59504737-9BEF-4AC9-ACEA-9E6192C4F446")]
        public class TextBoxEvents 
        {  
    
            [ComVisible(false)] public delegate void GotFocusEvent();
            public event GotFocusEvent GotFocus;
    
            public void OnGotFocus(object s, RoutedEventArgs e) => RaiseGotFocusEvent(); 
    
            public void RaiseGotFocusEvent() => GotFocus?.Invoke();
    
        }  


    and this vb6 code

    Private WithEvents m_oTextBoxEvents As IfxNetControls.TextBoxEvents
    ...
    Private Sub UserControl_Initialize()
        ...
        Set m_oTextBoxEvents = New IfxNetControls.TextBoxEvents
    End Sub
    ...
    Private Sub m_oTextBoxEvents_GotFocus()
        Debug.Print "GotFocus"
    End Sub

    I can step the code in C# such that it tries to execute GotFocus. But the GotFocus delegate is null.

    I had assumed that by adding the event handler in vb6 I was effectively subscribing to that GotFocus event. But obviously not. Do I need something concrete in the C# to fire the event to vb6.

    Many thx

    Thursday, October 25, 2018 5:18 PM

Answers

  • So I was being dumb. I was subscribing to a new eventsource rather than the eventsource wqhich was actually raising event

    Private m_oTextBox as ITextBox
    Private WithEvents m_oTextBoxEvents As IfxNetControls.TextBoxEvents
    ...
    Private Sub UserControl_Initialize()
        ...
        Set m_oTextBox = ... instantiate textbox here
        Set m_oTextBoxEvents = m_oTextBox.Events
    End Sub
    ...
    Private Sub m_oTextBoxEvents_GotFocus()
        Debug.Print "GotFocus"
    End Sub



    • Edited by Simon Woods Friday, October 26, 2018 7:31 AM Include code sample
    • Marked as answer by Simon Woods Friday, October 26, 2018 7:31 AM
    Friday, October 26, 2018 7:28 AM

All replies

  • Hi,

    thanks for posting here.

    Since your case is more related to c# code, I will move it to c# forum for better support.

    Your understanding and cooperation will be grateful.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 26, 2018 5:53 AM
    Moderator
  • So I was being dumb. I was subscribing to a new eventsource rather than the eventsource wqhich was actually raising event

    Private m_oTextBox as ITextBox
    Private WithEvents m_oTextBoxEvents As IfxNetControls.TextBoxEvents
    ...
    Private Sub UserControl_Initialize()
        ...
        Set m_oTextBox = ... instantiate textbox here
        Set m_oTextBoxEvents = m_oTextBox.Events
    End Sub
    ...
    Private Sub m_oTextBoxEvents_GotFocus()
        Debug.Print "GotFocus"
    End Sub



    • Edited by Simon Woods Friday, October 26, 2018 7:31 AM Include code sample
    • Marked as answer by Simon Woods Friday, October 26, 2018 7:31 AM
    Friday, October 26, 2018 7:28 AM