以下代码在文本框获得焦点时更改其属性,并在失去焦点后恢复更改。我在使用 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 事件在文本框之间切换时按预期工作,而不是单击。
.Leave 事件在文本框之间单击时按预期工作,而不是选项卡。