?
Solved

Listbox setfocus?  vb6

Posted on 2006-07-18
4
Medium Priority
?
682 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
ID: 17129631
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
ID: 17129635
(Note--it's GotFocus, as shown in the code sample, not OnFocus as I wrote in the description.  Sorry...)
0
 

Author Comment

by:RyanBank
ID: 17129672
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 2000 total points
ID: 17129701
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

862 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