Couple of Listview Questions

M

MarcJ

Hello,

Part one:

I'm currently working on my first major conversion from an existing VB6
application to a VB.Net app (actually it's a total re-write). I have a
listview with the view set to "detail". In VB6 I used the ItemClick
event to display information about each row when a user uses the up/down
arrow keys or clicks a row with the mouse. Now, in .net I use the click
event to handle the mouse clicks but can't find anything to handle the
up/down arrow keys. Am I going to have to write a custom function that
handles the keypress event or is there some other event that gets fired
when a row is selected?

Part two:

The listview "sponges" on the first column. I have no idea what the
real technical term for "sponging" is so I couldn't look it up. I'm
sure you know what I mean but just in case. When the listview has
focus, the user can start typing and the row selection will follow down
the list to where the text in the first column matches the text the user
typed. Now, what I would like to do is "sponge" on a column other than
the first one. I want to "sponge" on the second column.

Thanks for any help.

MarcJ
 
G

GeneT

Hi, MarcJ.

I don't know what you can do for the second part, but for the first
part, look at the SelectedIndexChanged event. If I'm not mistaken, it
gets fired whether it's a mouse or arrow key being used to navigate the
listbox.

Hope this helps!
 
C

Cor

Hi Marc,

The answer on the second one is very easy.

As far as I know can you not type in the second column (subitem) of a
listview, only the real item is editable.

Cor
 
M

MarcJ

Cor,

Does "sponging" go off the column being editable? I have no idea. I
just assumed it did a quick search from left to right of each text
element in the column and moved the selection to the first row that
matches the text the user typed. With that being said, I suppose I
could mimic that for a different column in a custom function but I was
hoping that there was someway to just tell the listview to check a
different column. BTW, nothing in my listview is editable.

Thanks,
Marc
 
C

Cor

Hi Marc,

The listview is a very difficult control to communicate about.

It has the items labeledit with what you can edit, what I call the item.

But that depends on the view which is default in the largeicon but when we
talking about it mostly in the details as with Explorer.

And when you are busy with columns, the documentation on MSDN is in my
opinion totally wrong in that. It says that it is a columnheadercollection
and goes on then about the columnheaders. The best to see some help about
listview columns is looking to the intellisence help or I think in the
objectbrowser.

Cor
 
M

MarcJ

Ok, the SelectedIndexChanged isn't working for me and I can't figure out
what's causing the error I'm getting.

my code:

Private Sub myList_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles myList.SelectedIndexChanged
myTextBox.Text = myList.SelectedItems.Item (0).SubItems(7).Text
End Sub

The first time I click on a row, everything works as expected. The
second time, I get the error:

Unhandled Exception: System.ArgumentOutOfRangeException: Specified
argument was out of the range of valid values.
Parameter name: index

Any ideas?

Thanks
 
G

GeneT

Hi, MarcJ.

If you simply want the value of the item selected in the listbox, you
can use something like this:

Private Sub myList_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles myList.SelectedIndexChanged

myTextBox.Text = myList.SelectedValue

End Sub

I'm not sure what you're doing with the SubItems but that could be where
your error is coming from.

Hope this helps!
 
M

MarcJ

Gene,

Thanks for the reply. I'm using a ListView and it doesn't have a
SelectedValue property.

Marc
 
G

GeneT

My bad, Marc. I was thinking of a ListBox. Try this code for your
ListView

Dim col As
System.Windows.Forms.ListView.SelectedListViewItemCollection

col = ListView1.SelectedItems

If col.Count > 0 Then
Messagebox.Show(col.Item(0).Text)
End If


"SelectedItems" actually returns a collection of items...probably useful
for multiselect. Apparently the SelectedIndexChanged event is fired more
than once when one of the items in the ListView is selected.

Hope this gets you back on track!
 
M

MarcJ

This worked like a charm! Thanks for your help.

I'm not sure I understand 100% why I have to do this for it to work
though. I guess since the SelectedIndexChanged event is firing more
than once, the second time thru there isn't actually anything selected
so I get the index is out of bounds. It looks like what it might be
doing is firing when the current selection is un-selected and before the
next item is selected. Between those two states, the listview doesn't
have anything selected yet. Then again, I could be totally off. :)

Thanks again.
 

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

Top