【问题标题】:Textbox Focus - Click versus Tab Using VB文本框焦点 - 单击与使用 VB 的选项卡
【发布时间】:2013-05-14 04:14:58
【问题描述】:

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

【问题讨论】:

标签: vb.net tabs textbox focus


【解决方案1】:

由于.LostFocus 适用于tab而不是点击,.Leave 适用于点击而不是tab,你可以尝试设置txtBoxes_ReminderOffFocus来处理这两个事件吗?

【讨论】:

  • Clicking/Tabbing 触发 .Leave 和 .LostFocus 事件,但它们遵循不同的顺序,如此处“备注”下所示:msdn.microsoft.com/en-us/library/…
  • 我想我不清楚什么不起作用。 LostFocus 和 Leave 以彼此不同的顺序发生,但在这两种情况下都发生在 Enter 和 GotFocus 事件之后,并且您不处理其他事件。所以我不确定你遇到了什么不同。
猜你喜欢
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多