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

时间:2013-05-11 18:29:00

标签: vb.net tabs textbox focus

以下代码在具有焦点时更改文本框的属性,并在失去焦点后还原更改。我在使用Enter,Leave,GotFocus,LostFocus事件时遇到问题,因为它们在单击或标签到文本框时会以不同的顺序出现。以下代码仅在文本框之间进行选项卡而不是单击时才按预期运行。如果我改变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

在'txtBoxes_ReminderOffFocus'

.LostFocus事件在文本框之间进行选项卡时按预期工作,而不是单击。

。当文本框之间点击时,保留事件按预期工作,而不是标签。

1 个答案:

答案 0 :(得分:0)

由于.LostFocus适用于标签而非点击而.Leave在点击而非标签时有效,您是否可以尝试设置txtBoxes_ReminderOffFocus来处理这两个事件?