Is there an alternative?

securonic

Active member
Joined
Aug 29, 2010
Messages
34
Location
Midlands Uk
Programming Experience
Beginner
hi everyone.

i have been playing about with the listview control and and been looking at ways of selecting parts of the rows and displaying them in a text box. i have used the following code to do just that but find it will only work if i use a try statement when i decide to reselect a row in the listview.

Private Sub ListView1_SelectedIndexChanged_UsingItems(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ListView1.SelectedIndexChanged

TextBox3.Clear()
Try
TextBox3.Text = ListView1.SelectedItems(0).SubItems(1).Text

Catch exception As Exception

End Try

End Sub

any suggestions?

thanks
 
The ListView tends to get over-used. If you want to display a table of data then the ListView is useful if you want the grouping functionality it provides or you want to be able to display other views too. If you don't need those features then the ListView is probably the wrong choice. More likely you should be using a DataGridView, which is actually a grid control, which the ListView is not.

As for the question, this statement:
it will only work if i use a try statement
is not correct. The reason that you were having an issue is that you were getting the first selected item whether there were any items selected or not. If there weren't then an exception was thrown. Doing something and then cleaning up if it doesn't work should always be your last option. Your first option should always be to check whether you can do it first and then only do it if you can. In your case, that would look like this:
VB.NET:
If ListView1.SelectedItems.Count > 0 Then
    TextBox3.Text = ListView1.SelectedItems(0).SubItems(1).Text
End If
That way you don't try to get the first selected item if no items are selected.

kulrom's code works in a similar fashion in that it loops through all the selected items. If there are no items selected then the code never enters the loop. The potential issue with that code is that, if multiple items are selected, you'll end up writing all of them to the TextBox, replacing the previous value each time, so you'd end up with the last value. Most likely you'd be handling things differently if you could select multiple items though, so that shouldn;t be an issue.
 
hey jmcilhinney,

thanks for the explanation there! funnily enough i was in the middle of working out where i went wrong with that and how my code differed to Kulroms so your reply was exactly what i needed. i knew using the 'try' was a bad way of dealing with the problem because i kept seeing exceptions generated in the output window even though it was doing what i expected. it just seemed very likely that perhaps it wasn't the best way of dealing with it, hence the thread.. you see i found the code on the web initially but couldn't find any info about the detail of what it actually did. looking up the code on MSDN it seemed like the right way to do the job. i assumed that the first enumerator refered to the row selection and that manipulating that as a variable i would be able to perform a row selection which copied it to the text box... kinda half way maybe. do you have any tips or books i could get hold of so that i have some sort of reference to use? or does all this come down to plain experience through trial and error?

Thanks for your time though, it really is very helpful.
only ever seems to be you guys who answer my threads
 
Back
Top