0
votes

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. 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.

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'

.LostFocus event works as expected when tabbing between textboxes, not clicking.

.Leave event works as expected when clicking between textboxes, not tabbing.

1

1 Answers

0
votes

Since .LostFocus works for tabbing and not clicking and .Leave works when clicking and not tabbing, can you try setting txtBoxes_ReminderOffFocus to handle both events?