Solved

Listbox setfocus?  vb6

Posted on 2006-07-18
4
588 Views
Last Modified: 2008-02-01
Hi,

How to determine if a listbox has focus?

thanks.
0
Comment
Question by:RyanBank
  • 3
4 Comments
 
LVL 13

Expert Comment

by:jmundsack
Comment Utility
On a form, Me.ActiveControl returns a reference to the Control that has the focus.  So, if your ListBox was named lstFoo, this would work:

If Me.ActiveControl Is lstFoo Then
    MsgBox "lstFoo has the focus!", vbInformation
End If

If you're saying you want to do something when the ListBox GETS the focus, use that ListBox's "OnFocus" event:

Private Sub lstFoo_GotFocus()
    MsgBox "lstFoo just got the focus!", vbInformation
End Sub

HTH-Jon

0
 
LVL 13

Expert Comment

by:jmundsack
Comment Utility
(Note--it's GotFocus, as shown in the code sample, not OnFocus as I wrote in the description.  Sorry...)
0
 

Author Comment

by:RyanBank
Comment Utility
jmundsack,

Weird somethings missing when I click the test button, it doesn't work.

Private Sub CmdRemove_Click()
If Me.ActiveControl Is LstSunday Then
    MsgBox "lstFoo has the focus!", vbInformation
End If


End Sub
0
 
LVL 13

Accepted Solution

by:
jmundsack earned 500 total points
Comment Utility
When you click cmdRemove, the focus is changed to cmdRemove.

What you'll need to do is set a module-level variable to the "last selected control" and update it each time the focus changes.  Like...

Private mLastSelectedControl As Control

...

Private Sub lstFoo_GotFocus()
    Set mLastSelectedControl = lstFoo
End Sub

Private Sub lstBar_GotFocus()
    Set mLastSelectedControl = lstBar
End Sub

Private Sub CmdRemove_Click()
    If mLastSelectedControl Is lstFoo Then
        MsgBox "lstFoo has the focus!", vbInformation
    End If
End Sub
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now