Solved

Listbox setfocus?  vb6

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

How to determine if a listbox has focus?

thanks.
0
Comment
Question by:RyanBank
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

728 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