Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7521899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:21:46+00:00 2026-05-30T02:21:46+00:00

I put a command button on my VB6 form. I would like to make

  • 0

I put a command button on my VB6 form. I would like to make it so that if I click this button it will popup a message indicating which control most recently had FOCUS.

I know that if I press the command button it will cause the command button to receive focus. I am interested in finding out which control had focus just BEFORE the command button took the focus away from it. How would I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:21:48+00:00Added an answer on May 30, 2026 at 2:21 am

    Subclass the button.
    Handle WM_SETFOCUS.
    Act accordingly.

    Example.

    Form Form1:

    Option Explicit
    
    Private Sub cmdCleverButton_Click()
      MsgBox cmdCleverButton.Tag
    End Sub
    
    Private Sub Form_Load()
      modCleverButtonSublass.SubclassCleverButton cmdCleverButton, Me
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      modCleverButtonSublass.UnsubclassCleverButton
    End Sub
    

    Module modCleverButtonSublass

    Option Explicit
    
    Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const GWL_WNDPROC As Long = -4&
    Private Const WM_SETFOCUS As Long = &H7&
    
    
    Private m_PrevWndProc As Long
    Private m_Button As CommandButton
    Private m_Form As Form
    
    
    Public Sub SubclassCleverButton(ByVal b As CommandButton, ByVal ParentForm As Form)
      If Not m_Button Is Nothing Then Err.Raise 5, , "Already subslassed."
    
      Set m_Button = b
      Set m_Form = ParentForm
      m_PrevWndProc = SetWindowLong(m_Button.hwnd, GWL_WNDPROC, AddressOf SubclassCallback)
    End Sub
    
    Public Sub UnsubclassCleverButton()
      If m_Button Is Nothing Then Err.Raise 5, , "Subclass first."
    
      SetWindowLong m_Button.hwnd, GWL_WNDPROC, m_PrevWndProc
      Set m_Form = Nothing
      Set m_Button = Nothing
    End Sub
    
    
    Private Function SubclassCallback(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
      If uMsg = WM_SETFOCUS Then
        Dim c As Control
    
        Set c = FindByHwnd(m_Form, wParam)
        If c Is Nothing Then
          m_Button.Tag = vbNullString
        Else
          m_Button.Tag = c.Name
        End If
      End If
    
      SubclassCallback = CallWindowProc(m_PrevWndProc, hwnd, uMsg, wParam, lParam)
    End Function
    
    Private Function FindByHwnd(ByVal Parent As Form, ByVal hwnd As Long) As Control
      Dim c As Control
    
      For Each c In Parent.Controls
        If c.hwnd = hwnd Then
          Set FindByHwnd = c
          Exit Function
        End If
      Next
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This application needs to send a motor move command when a GUI button or
Please explain this curl command: curl --digest \ -u{username}:{password} \ -v \ -X PUT
I am trying to code this from scratch, so that when a button clicked
I now know that to make a widget with tkinter with a command parameter
I have used same command button Click event for both Insert, Update functionalities. I'm
Basically I've got an update query that runs when you click a submit button.
Please, help me in: how to put a double command in the cmd ,
I got an out put on my Windows command line when I execute the
I have a command, for example: echo "word1 word2" . I want to put
Put it another way: what code have you written that cannot fail. I'm interested

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.