Solved

Listbox setfocus?  vb6

Posted on 2006-07-18
4
599 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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

776 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