Solved

Listbox setfocus?  vb6

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 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…

792 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