none
Find key pressed in EventArgs or check next focusing component RRS feed

  • Question

  • I want to apply the following conditions on textbox1 leave event:

    1) if tab is pressed in textbox1 then the focus will be shifting to textbox2 so do nothing in textbox1 leave event.

    2) if textbox2 is clicked then do nothing in textbox1 leave event.

    3) else if textbox1 > textbox2 then display message "Textbox1 can't be greater" and focus on textbox1.

    I tried this but it needs a function to be raised for it.

    Dim look As Boolean

    Private Sub Textbox1_Leave(sender As Object, e As EventArgs) Handles Textbox1.Leave Dim Punch As KeyPressEventArgs = Textbox1.KeyPress look = Nothing If Textbox2.Focused Or Asc(Punch.KeyChar) = 9 Then look = True End If Select Case look Case False If Textbox1.Text <> "" And Textbox2.Text <> "" And Textbox1.Text > Textbox2.Text Then MsgBox("Textbox1 can't be greater") Textbox1.Focus() End If End Select End Sub

    I tried keypress event with leave event but it seems that keypress mean nothing along with leave.

    Thanks in advance.


    Habib Ur Rehman

    Sunday, October 18, 2020 3:48 PM

Answers

  • If TabIndex of controls is already set to make the <Tab> key work, then try something like this too:

    Private t1 As Timer = Nothing
    
    Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    
        t1?.Stop()
    
        t1 = New Timer With {.Interval = 111, .Enabled = True}
        AddHandler t1.Tick, AddressOf ValidateTextboxes
    
    End Sub
    
    Private Sub TextBox2_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter
    
        t1?.Stop()
    
    End Sub
    
    Private Sub ValidateTextboxes(sender As Object, e As EventArgs)
    
        CType(sender, Timer).Stop()
    
        MsgBox("Test")
    
        ' TODO: validate TextBox1 and TextBox2
    
    End Sub
      

    The timer (t1) can be also inserted in Form Designer.


    Sunday, October 18, 2020 7:14 PM

All replies

  • If TabIndex of controls is already set to make the <Tab> key work, then try something like this too:

    Private t1 As Timer = Nothing
    
    Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    
        t1?.Stop()
    
        t1 = New Timer With {.Interval = 111, .Enabled = True}
        AddHandler t1.Tick, AddressOf ValidateTextboxes
    
    End Sub
    
    Private Sub TextBox2_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter
    
        t1?.Stop()
    
    End Sub
    
    Private Sub ValidateTextboxes(sender As Object, e As EventArgs)
    
        CType(sender, Timer).Stop()
    
        MsgBox("Test")
    
        ' TODO: validate TextBox1 and TextBox2
    
    End Sub
      

    The timer (t1) can be also inserted in Form Designer.


    Sunday, October 18, 2020 7:14 PM
  • I tried keypress event with leave event but it seems that keypress mean nothing along with leave.

    Thanks in advance.


    Habib Ur Rehman

    Try the Textbox1.KEYDOWN Event and trap the tab key.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    I used KeyDown and LostFocus Events along with Leave event which seems normal.

    Then I noticed the error that key down isn't called when I clicked textbox3 and then clicked back textbox1 and pressed tab key. 

    Dim look As Boolean
    Dim kyprs As Boolean
    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Tab Then
            kyprs = True
        Else
            kyprs = False
        End If
    End Sub
    Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
            look = Nothing
        If TextBox2.Focused Or kyprs = True Then
            look = True
            kyprs = False
        End If
    End Sub
    Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
        Select Case look
            Case False
                If TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox1.Text > TextBox2.Text Then
                    MsgBox("Textbox1 should not be higher than TextBox2.", MessageBoxIcon.Warning)
                    TextBox1.Focus()
                End If
        End Select
    End Sub

    First I skipped Leave event and tried if LostFocus and KeyDown can work for me but they didn't worked as well also created new issue of adding additional code if Focus is lost by other windows(like Chrome, VS etc).

    The original problem is appeared when I added

        Msgbox(e.KeyValue)
    in KeyDown also in KeyPress Events. These both Events are triggered by every single key(I checked like 20 to 25 keys) of the keyboard but not by Tab key only.

    Habib Ur Rehman

    Monday, October 19, 2020 1:48 AM
  • If TabIndex of controls is already set to make the <Tab> key work, then try something like this too:

    Private t1 As Timer = Nothing
    ...............
    End Sub  

    The timer (t1) can be also inserted in Form Designer.

    Thanks Viorel.

    This solved the problem.


    Habib Ur Rehman

    Monday, October 19, 2020 2:07 AM