Results 1 to 12 of 12

Thread: Press F1 in Textbox1 to do this

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Press F1 in Textbox1 to do this

    I am still trying to get my head around the use of functions in my applications so I may not be explaining this clearly but .... I will try my best to be clear as I possibly can.

    I have a form where I have 5 different text boxes. Lets say textbox 1 through 5. How do I write a function like the one below for use of all 5 controls.

    The catch is that I need to know which textbox is responsible for the key down. So that if I am in textbox1 and press F1 I will get a message specific to textbox1.

    Code:
            Dim intKey As Integer
            intKey = e.KeyCode
    
            Select Case intKey
                Case Keys.F1
                    MsgBox("F1 Pressed")
                Case Keys.F2
                    MsgBox("F2 Pressed")
                Case Keys.F3
                    MsgBox("F3 Pressed")
                Case Keys.F4
                    MsgBox("F4 Pressed")
                Case Keys.F5
                    MsgBox("F5 Pressed")
            End Select
    Thanks and I hope that makes sense.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Do you want a method for each text box? Like

    VB Code:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    2.         If e.KeyCode = Keys.F1 Then
    3.             MsgBox("F1 was pressed")
    4.         End If
    5.     End Sub
    6.  
    7.     Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
    8.         If e.KeyCode = Keys.F1 Then
    9.             MsgBox("F1 was pressed")
    10.         End If
    11.     End Sub

    Or one method to handle all text boxes, something like
    VB Code:
    1. Private Sub Several_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown
    2.         If sender Is TextBox1 Then
    3.             MsgBox("TextBox1")
    4.         End If
    5.         If sender Is TextBox2 Then
    6.             MsgBox("TextBox2")
    7.         End If
    8.     End Sub

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Looks like that is the keydown event. you can add more controls to the same event through the handles keyword. Notice at the end of the method declaration it has TextBox1-5. And the other parameter is sender which is the textbox the event is fired from.
    VB Code:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown, TextBox5.KeyDown
    2.         'sender is the one firing the event
    3.         Select Case e.KeyCode
    4.             Case Keys.F1
    5.                 MsgBox("F1 Pressed", , DirectCast(sender, TextBox).Name)
    6.             Case Keys.F2
    7.                 MsgBox("F2 Pressed", , DirectCast(sender, TextBox).Name)
    8.             Case Keys.F3
    9.                 MsgBox("F3 Pressed", , DirectCast(sender, TextBox).Name)
    10.             Case Keys.F4
    11.                 MsgBox("F4 Pressed", , DirectCast(sender, TextBox).Name)
    12.             Case Keys.F5
    13.                 MsgBox("F5 Pressed", , DirectCast(sender, TextBox).Name)
    14.         End Select
    15.     End Sub

    Looks like I'm a bit of a slow typer today.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Mike Hildner something like the second one you posted. I am not sure why the one Edneeis works but it seems to do the trick. However I have one more thing to do. I need to do an If Then or equivalant that can open the form based on these two pieces of known information.

    I know the key down event was directed to me from textbox1 and it was an F1 key that was pressed so I need to show a Help Form for textbox1.

    or

    I know the key down event was directed to me from textbox1 and it was an F2 key that was pressed so I need to show a Field Info Form for textbox1.

    And then do the same thing for textbox2 etc.
    Last edited by BukHix; Oct 8th, 2003 at 03:37 PM.

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Edneeis was smarter with the DirectCast business. Reading you last post, are you still having troubles? It looks like you have all the information you need.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    No I am still having problems with getting the textbox name into an argument.

    Maybe this will clear it up.

    Code:
        Private Sub TextBox1_KeyDown(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.KeyEventArgs) _
            Handles TextBox1.KeyDown, TextBox2.KeyDown
    
            Select Case e.KeyCode
                Case Keys.F1
                    MsgBox("F1 Pressed", , _
                        DirectCast(sender, TextBox).Name)
                    'If the sender is from textbox1 open Help Form for textbox1
                    'If the sender is from textbox2 open Help Form for textbox2
    
                Case Keys.F2
                    MsgBox("F2 Pressed", , _
                        DirectCast(sender, TextBox).Name)
                    'If the sender is from textbox1 open More Info for textbox1
                    'If the sender is from textbox2 open More Info for textbox2
    
            End Select
        End Sub

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    That's what DirectCast(sender, TextBox).Name does. If it's TextBox1 that returns TextBox1 etc.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Yup I get the textbox name in the messagebox but I need to evaluate the textbox name and this doesn't work.

    Code:
          Select Case e.KeyCode
                Case Keys.F1
                    MsgBox("F1 Pressed", , _
                        DirectCast(sender, TextBox).Name)
                    If DirectCast(sender, TextBox).Name = TextBox1 Then
                        ' do something here 
                    ElseIf DirectCast(sender, TextBox).Name = TextBox2 Then
                        ' do something else here 
                    End If

  9. #9
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    You've got some squiggly lines there don't you?

    Use quotes around the name you're checking against. The .Name property returns a string.

    If DirectCast(sender, TextBox).Name = "TextBox1" Then


    In your code you were attempting to assign the .Name property to the object TextBox1

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could join the two and use Mike's second post way instead of the name.
    VB Code:
    1. Select Case e.KeyCode
    2.             Case Keys.F1
    3.                 MsgBox("F1 Pressed", , _
    4.                     DirectCast(sender, TextBox).Name)
    5.                 If sender Is TextBox1 Then
    6.                     ' do something here
    7.                 ElseIf sender Is TextBox2 Then
    8.                     ' do something else here
    9.                 End If
    You really only need to DirectCast it if you want to refer to a property of sender (i.e. Name, Text...)

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    I finaly got it. Thank you both for getting me there. Just in case somebody else if following this thread and wants to see how it all looks put together. Here is the completed sample.
    Code:
      Public Sub capKeyDown_KeyDown(ByVal sender As Object, _
            ByVal e As KeyEventArgs)
    
            Select Case e.KeyCode
                Case Keys.F1
                    If sender Is TextBox1 Then
                        MsgBox("F1 in Textbox1 was pressed")
                    ElseIf sender Is TextBox2 Then
                        MsgBox("F1 in Textbox2 was pressed")
                    End If
                Case Keys.F2
                    If sender Is TextBox1 Then
                        MsgBox("F2 in Textbox1 was pressed")
                    ElseIf sender Is TextBox2 Then
                        MsgBox("F2 in textbox2 was pressed")
                    End If
            End Select
    
        End Sub
    
        Private Sub TextBox1_KeyDown(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.KeyEventArgs) _
            Handles TextBox1.KeyDown
    
            capKeyDown_KeyDown(TextBox1, e)
    
        End Sub
    
        Private Sub TextBox2_KeyDown(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.KeyEventArgs) _
            Handles TextBox2.KeyDown
    
            capKeyDown_KeyDown(TextBox2, e)
    
        End Sub
    I hope it is usefull to someone else.

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Alternatively you could just use:
    VB Code:
    1. Public Sub capKeyDown_KeyDown(ByVal sender As Object, _
    2.         ByVal e As KeyEventArgs) [b]Handles TextBox1.Keydown,Textbox2.KeyDown[/b]
    3.  
    4.         Select Case e.KeyCode
    5.             Case Keys.F1
    6.                 If sender Is TextBox1 Then
    7.                     MsgBox("F1 in Textbox1 was pressed")
    8.                 ElseIf sender Is TextBox2 Then
    9.                     MsgBox("F1 in Textbox2 was pressed")
    10.                 End If
    11.             Case Keys.F2
    12.                 If sender Is TextBox1 Then
    13.                     MsgBox("F2 in Textbox1 was pressed")
    14.                 ElseIf sender Is TextBox2 Then
    15.                     MsgBox("F2 in textbox2 was pressed")
    16.                 End If
    17.         End Select
    18.  
    19.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width