简体   繁体   English

文本框焦点-使用VB单击与选项卡

[英]Textbox Focus - Click versus Tab Using VB

The following code changes the properties of a textbox when it has focus and reverts the changes after losing focus. 下面的代码在具有焦点的情况下更改文本框的属性,并在失去焦点后还原其更改。 I'm having trouble with using the Enter, Leave, GotFocus, LostFocus events because they occur in different orders when clicking or tabbing to a textbox. 我在使用Enter,Leave,GotFocus,LostFocus事件时遇到麻烦,因为在单击或切换到文本框时,它们以不同的顺序发生。 The following code only acts as expected when tabbing between textboxes but not clicking. 下面的代码仅在在文本框之间切换而不单击时按预期方式起作用。 If I change txtBoxes_ReminderOffFocus to handle the Leave event instead of LostFocus, then it works as expected when clicking between textboxes but not tabbing. 如果我将txtBoxes_ReminderOffFocus更改为处理Leave事件而不是LostFocus,则在文本框之间单击而不单击制表符时,它将按预期工作。

Private Sub txtBoxes_ReminderOnFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHrsWkd.Enter, txtPayRate.Enter, txtFedTaxRate.Enter, txtStTaxRate.Enter
    If sender.Text = "(Numeric Value)" Or sender.Text = "(Percentage)" Then
        Debug.Print("New textbox focused onto.")        'Sets up textboxes for standard input, if they have initial reminder. Check control.enter event for more on focus orderings.
        sender.Clear()
        sender.TextAlign = HorizontalAlignment.Left
        sender.ForeColor = Color.Black
    End If
End Sub

Private Sub txtBoxes_ReminderOffFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHrsWkd.LostFocus, txtPayRate.LostFocus, txtFedTaxRate.LostFocus, txtStTaxRate.LostFocus
    If sender.Text = "" Then
        Debug.Print("A textbox has lost focus.")
        sender.ForeColor = Color.Gray               'if textbox is empty, fills in initial "numeric value" or "percentage" reminder.
        sender.TextAlign = HorizontalAlignment.Right
        If sender Is txtHrsWkd Or sender Is txtPayRate Then
            sender.Text = "(Numeric Value)"
        Else
            sender.Text = "(Percentage)"
        End If
    End If
End Sub

In 'txtBoxes_ReminderOffFocus' 在“ txtBoxes_ReminderOffFocus”中

.LostFocus event works as expected when tabbing between textboxes, not clicking. 当在文本框之间切换而不单击时,.LostFocus事件将按预期工作。

.Leave event works as expected when clicking between textboxes, not tabbing. 在文本框之间单击而不是在选项卡之间单击时,.Leave事件将按预期工作。

由于.LostFocus可用于制表和不单击,而.Leave可在单击和不制表时,您可以尝试设置txtBoxes_ReminderOffFocus来处理这两个事件吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM