[英]Textbox Focus - Click versus Tab Using VB
下面的代码在具有焦点的情况下更改文本框的属性,并在失去焦点后还原其更改。 我在使用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事件将按预期工作。
由于.LostFocus
可用于制表和不单击,而.Leave
可在单击和不制表时,您可以尝试设置txtBoxes_ReminderOffFocus
来处理这两个事件吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.