Microsoft Knowledge Base Email Alertz

KBAlertz.com: LostFocus Event not Fired for Constituent Controls of a VB6 ActiveX Control Placed on a Windows Form

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 948475 - Last Review: January 29, 2008 - Revision: 1.4

LostFocus Event not Fired for Constituent Controls of a VB6 ActiveX Control Placed on a Windows Form

Source: Microsoft Support

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Action

You are using a Visual Basic 6.0 ActiveX control (OCX) containing constituent controls on a Microsoft .NET Windows Form.

Result

The LostFocus event of the constituent control is not fired when changing focus from the constituent control to a control on the Windows Form.

Cause

When exiting the constituent control, the AXHost wrapper which contains the ActiveX control never receives focus. Therefore the constituent control never lost focus and does not fire its LostFocus event.

Resolution



The following sample code demonstrates how to deactivate the ActiveX control during the AXHost Leave event, which allows the constituent control's LostFocus event to fire. In the sample code, axALostFocusCTL21 is the AxHost wrapper for the COM control which is placed on the Windows Form.


C# Sample Code
=============

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
   [ComImport(), Guid("00000113-0000-0000-C000-000000000046"),
   InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]

   public interface IOleInPlaceObject
   {
      [PreserveSig]
      int GetWindow([Out]out IntPtr hwnd);
      void ContextSensitiveHelp(int fEnterMode);
      void InPlaceDeactivate();

      [PreserveSig]
      int UIDeactivate(); 
      void SetObjectRects([In] IntPtr lprcPosRect, [In] IntPtr lprcClipRect);
      void ReactivateAndUndo(); 
   }

   public partial class Form1 : Form
   {
      AxHost axControl; 
      public Form1()
      {
         InitializeComponent();
         this.axALostFocusCTL21.Leave += new System.EventHandler(this.axALostFocusCTL21_Leave);
      }
   
      private void axALostFocusCTL21_Leave(object sender, System.EventArgs e)
      {
         axControl = (AxHost)sender;
         IOleInPlaceObject oipo = (IOleInPlaceObject)axControl.GetOcx(); 
        oipo.UIDeactivate();
      }

      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
         if(axControl != null)
         axControl.Dispose();
         if (this.axALostFocusCTL21 != null)
            this.axALostFocusCTL21.Dispose(); 
      } 
   }
}

Visual Basic .NET Sample Code
========================

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

   <ComImport(), Guid("00000113-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
   Public Interface IOleInPlaceObject 

      <PreserveSig()> _
      Sub GetWindow(<OutAttribute()> ByRef phwnd As IntPtr)
      Sub ContextSensitiveHelp(<InAttribute()> ByVal fEnterMode As Integer) 
      Sub InPlaceDeactivate()

      <PreserveSig()> _
      Sub UIDeactivate()
      Sub SetObjectRects(<InAttribute()> ByVal lprcPosRect As IntPtr, <InAttribute()> ByVal lprcClipRect As IntPtr)
      Sub ReactivateAndUndo()
   End Interface

   Public Class Form1
      Dim axControl As AxHost
      Private Sub axALostFocusCTL21_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
         axControl = CType(sender, AxHost)
         Dim oipo As IOleInPlaceObject = CType(axControl.GetOcx(), IOleInPlaceObject)
         oipo.UIDeactivate()
      End Sub

      Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
         If Not axControl Is Nothing Then
            axControl.Dispose()
         End If
         If Not AxALostFocusCTL21 Is Nothing Then
            AxALostFocusCTL21.Dispose()
         End If
      End Sub
     
      Public Sub New()
         ' This call is required by the Windows Form Designer.
         InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddHandler Me.AxALostFocusCTL21.Leave, AddressOf Me.axALostFocusCTL21_Leave
      End Sub
   End Class

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.

APPLIES TO
  • Microsoft Visual Studio .NET 2002 Academic Edition
  • Microsoft Visual Studio .NET 2002 Enterprise Architect
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition SP1
  • Microsoft Visual Studio .NET 2002 Enterprise Developer
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2003 Academic Edition
  • Microsoft Visual Studio .NET 2003 Enterprise Architect
  • Microsoft Visual Studio .NET 2003 Enterprise Developer
  • Microsoft Visual Studio .NET 2003 Professional Edition
  • Microsoft Visual Studio .NET 2003 Service Pack 1
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Service Pack 1
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Team Edition for Database Professionals
  • Microsoft Visual Studio 2005 Team Suite
Keywords: 
kbnomt kbrapidpub KB948475
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please