blah blah blah is here! blah blah » Close

up1down
link

Yesterday I asked about navigating to the selected listview items that are URLs.

http://www.debugging.com/bug/23702

And http://www.debugging.com/vulpes gave me the answer, thanks. Now I need to go on this application and I need to navigate to the items other than the first column. Is this possible? If so, how can I do that?

Thanks.

last answered one year ago

1 answers

link

Yes, you need to access the SubItems collection of the currently selected item.

Curiously, the first item in this collection (at index 0) is the item in the first column of the listview itself (not its first subitem!) so you need to start at index 1 to access the subitems themselves.

So, the following line will get the text of the second column in the listview (i.e. the first subitem) of the selected item:

string text = listView1.SelectedItems[0].SubItems[1].Text;


EDIT - see comments below

Well, you can highlight the whole line when an item or subitem is clicked by setting the listView's FullRowSelect property to true.

However, there's no way to tell from the SelectedIndexChanged eventhandler which column was clicked.

By handling the MouseDown event instead, it's possible to deduce from the mouse's position at the time which row and column was clicked. See this thread for some sample code.

However, you'd be better IMO using a DataGridView rather than a ListView if you want to easily identify whether a particular 'cell' has been clicked.

ademmeda
108

So, does that mean I cannot have both first item and first subitem clickable? With the code you've given, when I click first item, the webbrowser navigates to the first subitem. This is ok but not the exact thing I needed. I have an URL in the first column and I have another URL as its subitem and I need them both clickable. Thanks again.

vulpes
17279

Please see my edit above.

ademmeda
108

Well, I am really a beginner and I am trying to figure out the best approach and I started with a listbox, then upgraded to a listview now you are suggesting the datagridview and I guess, it will be my solution. I will see what it can do and if it is what I need. Actually, in short I need a table with URLs and information related to them that is editable and that can be saved. And thought listview would be the way to go since I don't have any experience. By the way, I checked the same thread just before asking this question here :) Thanks again, see you.

Feedback