|
-
Oct 8th, 2003, 01:32 PM
#1
Thread Starter
Hyperactive Member
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.
-
Oct 8th, 2003, 01:54 PM
#2
Frenzied Member
Do you want a method for each text box? Like
VB Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.F1 Then
MsgBox("F1 was pressed")
End If
End Sub
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.F1 Then
MsgBox("F1 was pressed")
End If
End Sub
Or one method to handle all text boxes, something like
VB Code:
Private Sub Several_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown
If sender Is TextBox1 Then
MsgBox("TextBox1")
End If
If sender Is TextBox2 Then
MsgBox("TextBox2")
End If
End Sub
-
Oct 8th, 2003, 01:55 PM
#3
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:
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
'sender is the one firing the event
Select Case e.KeyCode
Case Keys.F1
MsgBox("F1 Pressed", , DirectCast(sender, TextBox).Name)
Case Keys.F2
MsgBox("F2 Pressed", , DirectCast(sender, TextBox).Name)
Case Keys.F3
MsgBox("F3 Pressed", , DirectCast(sender, TextBox).Name)
Case Keys.F4
MsgBox("F4 Pressed", , DirectCast(sender, TextBox).Name)
Case Keys.F5
MsgBox("F5 Pressed", , DirectCast(sender, TextBox).Name)
End Select
End Sub
Looks like I'm a bit of a slow typer today.
-
Oct 8th, 2003, 03:13 PM
#4
Thread Starter
Hyperactive Member
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.
-
Oct 8th, 2003, 03:27 PM
#5
Frenzied Member
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.
-
Oct 8th, 2003, 03:36 PM
#6
Thread Starter
Hyperactive Member
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
-
Oct 8th, 2003, 03:48 PM
#7
Frenzied Member
That's what DirectCast(sender, TextBox).Name does. If it's TextBox1 that returns TextBox1 etc.
-
Oct 8th, 2003, 04:00 PM
#8
Thread Starter
Hyperactive Member
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
-
Oct 8th, 2003, 04:14 PM
#9
Frenzied Member
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
-
Oct 8th, 2003, 04:42 PM
#10
You could join the two and use Mike's second post way instead of the name.
VB Code:
Select Case e.KeyCode
Case Keys.F1
MsgBox("F1 Pressed", , _
DirectCast(sender, TextBox).Name)
If sender Is TextBox1 Then
' do something here
ElseIf sender Is TextBox2 Then
' do something else here
End If
You really only need to DirectCast it if you want to refer to a property of sender (i.e. Name, Text...)
-
Oct 9th, 2003, 10:42 AM
#11
Thread Starter
Hyperactive Member
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.
-
Oct 9th, 2003, 11:14 AM
#12
Alternatively you could just use:
VB Code:
Public Sub capKeyDown_KeyDown(ByVal sender As Object, _
ByVal e As KeyEventArgs) [b]Handles TextBox1.Keydown,Textbox2.KeyDown[/b]
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|