我有一個奇怪的問題。我用例行程式來檢查焦點;和......它停止作業。我不知道如何或為什么。
該例程基本上檢查活動控制元件是否是您正在檢查的控制元件,如果是,則回傳 true(因此我們可以處理不是的情況)。
然而......它最近開始一直回傳 false (我們沒有改變任何東西,我們只注意到一些欄位審計開始回傳奇怪的值)。即使控制元件已獲得焦點,并且表單上沒有其他控制元件,或者僅打開一個表單,并且表單顯然具有焦點。
有誰知道這可能是如何或為什么會這樣?這讓我很困惑。如您所見,我有一個測驗欄位,我們在其中運行一個初始化...并且值清楚地匹配,名稱,值,比較的每個欄位,它仍然不回傳 true。
我究竟做錯了什么?
編輯:忘記添加代碼。整個事情是這樣的:
' I call it from here:
' Inside form, any control, say `PurchaseCostBox`
Private Sub PurchaseCostBox_AfterUpdate()
' Check if the focus is had
If VerifyFocus(Me.PurchaseCostBox) Then
' Save more field info.
Debug.Print Me.PurchaseCostBox.SelStart
Debug.Print Me.PurchaseCostBox.SelLen
Debug.Print Me.PurchaseCostBox.Value
Else
' Do limited stuff
Debug.Print Me.PurchaseCostBox.Value
End if
End Sub
Public Function VerifyFocus(ByRef ctlWithFocus As Control) As Boolean
Dim FrmParent As Form
Dim ctlCurrentFocus As Control
On Error Resume Next
' Determine parent form for control
' Verify focus of parent form
Set FrmParent = Screen.ActiveForm
' Verify focus of control on form
Set ctlCurrentFocus = FrmParent.ActiveControl
If Not ctlCurrentFocus Is ctlWithFocus Then
ctlWithFocus.SetFocus
DoEvents
End If
' Even adding the below line does not return true:
ctlWithFocus.SetFocus
' Return true if the control currently has the focus
VerifyFocus = FrmParent.ActiveControl Is ctlWithFocus
' Discard any errors
Err.Clear
End Function
我也試過這個:
Public Function VerifyFocus(ByRef ctlWithFocus As Control) As Boolean
On Error Resume Next
' Return true if the control currently has the focus
VerifyFocus = Screen.ActiveControl Is ctlWithFocus
' Discard any errors
Err.Clear
End Function
Neither work any more...and I'm floundering.
uj5u.com熱心網友回復:
好吧,結果證明這是完全出乎意料的事情,并且與焦點完全無關。
事實證明,我稱之為的一種方法是通過使用Control.Properties.Parent.Form
. 雖然這確實回傳了正確的形式,但它也使上述VerifyFocus
例程永遠不會回傳 true(即使它沒有被使用)。我不知道為什么。我真的,在這一點上,不在乎。但我會把它留在這里讓其他人找到。
重構我的GetTopForm
例程讓我能夠集中注意力。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455713.html