473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Listview set cursor position for arrows

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.
Nov 20 '05 #1
4 4909
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
"Shane" <sh***********@yahoo.com> wrote in message
news:4b*************************@posting.google.co m...
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.

Nov 20 '05 #2
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
Nov 20 '05 #3
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
Nov 20 '05 #4
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
"Shane" <sh***********@yahoo.com> wrote in message
news:4b**************************@posting.google.c om...
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

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Richard | last post by:
Hi. I use a ListView to display data in tabular form. Each ListView row corresponds to a data record. The ListView Item of the record is the record key or code. Each SubItem in that row...
10
by: Bill H | last post by:
I used a bit of JS on a page that is fairly long and didn't like the way that <A HREF='#' ONCLICK=\"... refreshed the page to the top. Removing the HREF (<A ONCLICK=\"...) solved my...
2
by: Peter Steele | last post by:
I have a form containing a ListView with four columns, creating a multi-cell table. I want to have an event which can determine which cell in the table is under the cursor when a double-click is...
2
by: Michael C | last post by:
Hi all, When I try to use the GetItemAt (e.X, e.Y) method in a ListView MouseDown() event, it only works some of the time. If I right-click on an open area of the ListView, I get null back as...
0
by: cyrille | last post by:
Hello from example from web i did a little code to avoiding columnHeader resize. this code seems to work well, but when I put a 'normal' ListView on the same Form than my overrided ListView it...
1
by: jrhoads23 | last post by:
Hello, I subclassed my own ListView which supports column sorting. It automatically draws the up/down sort arrows in the column header. The arrows that are used are drawn by me. I noticed that...
5
by: Matt Michael | last post by:
Hi, I'm having a problem working with a listview in an application I'm writing. Currently, whenever the selectedindex changed event fires, it loads up a htmledit control, and other labels on the...
2
by: Matt Michael | last post by:
Hi, I'm having a problem working with a listview in an application I'm writing. Currently, whenever the selectedindex changed event fires, it loads up a htmledit control, and other labels on the...
1
by: Sim | last post by:
Hello NG, I try to use drag and drop function between two list views. For this I found following code: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.