VB.NET: Setting Focus To A Listview On Keydown

aceman817

Senior member
Jul 15, 2001
204
0
0
I have been working on a search program recently and couldn't figure out how to set focus to my listview. I have a text box where the query is entered and if the user presses either the pageup, pagedown, arrow up, or arrow down keys I want it to set focus to the first row. I once got something similar to this done is VB6, but .NET is puzzling me. Any help is greatly appreciated.

Thanks,
AL
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Can't you ask the form to handle the keypress event and put in your code to set focus in the event handler? To add an event handler to the form's keypress event, put this code in the page constructor:

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.form_keypress);

And add a form_keypress function like this:

private void form_keypress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Add code here to handle keypress
}

Sorry my sample code is in C#. I didn't install VB .NET support, just C#.
 

aceman817

Senior member
Jul 15, 2001
204
0
0
thanks for the quick reply. i can't seem to create a keypress event because it says that "they don't have the same signature" or something like that. i basically need to:

1. Capture keystrokes such as pageup, pagedown
2. Set focus to the listview's 1st column

--AL
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
What is the signature of your event handler function? How did you declare the function?
 

tkdkid

Senior member
Oct 13, 2000
956
0
0
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Select Case e.KeyCode
Case Keys.Up, Keys.Down, Keys.PageUp, Keys.PageDown
ListView1.Focus()
ListView1.Items(0).Selected = True
End Select
End Sub