Listview set cursor position for arrows

S

Shane

I have a text box select items in a listview as text is being typed
into the text box. I get the select bar to move correctly in the
Listview control.

If the user presses the down or up key in the text box, I want to move
to the selected item in the listview box.

The down or up arrow does set focus to the listview box and the
selected item switches from the grey of being unfocused to the blue of
focus. So far, so good.

Pressing the down arrow again moves to the second entry in the
listview instead of the entry below the currently highlighted entry.
Susbsequent cursor movements work.

Obviously, the selected table index that I'm using is not working
correctly. What do I need to do to fix it?

Private Sub txtLoc_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtLoc.TextChanged
Dim boolFound As Boolean
Dim i As Integer
Dim intLen As Integer
Dim Item As ListViewItem
Dim strLoc As String

strLoc = txtLoc.Text.Trim ' Get the string
If strLoc = "" Or strLoc = Nothing Then Exit Sub
intLen = strLoc.Length ' Length of substring

For i = 0 To lvw.Items.Count - 1
Item = lvw.Items(i) ' Get the item
Try
If boolFound = False And Item.Text.Substring(0,intLen)
= strLoc Then
lvw.Items(i).Selected = True
lvw.Items(i).EnsureVisible()
boolFound = True
Else
lvw.Items(i).Selected = False
End If
Catch ex As Exception
lvw.Items(i).Selected = False
End Try
Next
End Sub

Private Sub txtLoc_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles txtLoc.KeyUp
If e.KeyData = Keys.Down Then lvw.Focus()
If e.KeyData = Keys.Up Then lvw.Focus()
End Sub

Thanks

PS. This is a resubmission. I didn't get an answer three months ago.
 
S

SStory

hmm...
Shane,

Where is your keydown code ?

Appears that you should set SelectedIndex+=1 for that or -=1 in other
direction

Otherwise something is resetting the listview (making it refresh, losing
current position--becomes 0) and then pressing is thus item (1) which is the
2nd element.

What do you think?

Please show me the keydown code.


I like your name,

Shane
 
S

Shane

I don't have any keydown code. If you click on an entry with the
mouse, the arrow keys work correctly. I can't find an equivalent of a
"SelectedIndex" property that I can manually set in the code that
Microsoft obviously sets on a mouse click.

I could probably kludge together a solution. The original code that I
posted will highlight the appropriate entry in the listview box. I
could save the index of the matching entry; there is at most one. Call
another routine that uses "SendKeys.Send" with {Home} to start at the
top then sends a "{Down}" multiple times until I find a match.

I would HATE to do the kludge, when I know that there has to be a
property that I can set or a method that I can call.

Shane
 
S

Shane

I found the answer on my own. I'm posting the answer in case any other
poor slob runs into this problem.

lvw.Items(i).Focused = True
 
S

SStory

Appears to me that you are never telling the listview to move down. You are
just setting it to have focus....

Am I missing something?

If you trap a keyup in textbox and want that to occur in the listview then
you must somehow tell it to move down once...

I tried this...
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Down Then
'assuming no multiselect
For i As Integer = 0 To ListView1.Items.Count - 1
If ListView1.Items(i).Selected AndAlso i <>
ListView1.Items.Count - 1 Then
ListView1.Items(i).Selected = False
ListView1.Items(i + 1).Selected = True
ListView1.Focus()
e.Handled = True
Exit For
End If
Next
ElseIf e.KeyCode = Keys.Up Then
'do similar to above

End If
End Sub

it selects items and moves down when you click down arrow while in text
box--also switching focus to the listview. Is that what you wanted?

I see what you are talking about... there isn't a selectedindex property for
ListView.

Apparently you have to iterate it.

Sorry I couldn't help you more.

Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top