These codes work fine to display data from listview to textbox.

TextBox1.Text = ListView1.Items.Item(ListView1.FocusedItem.Index).SubItems(0).Text

Is there any alternative codes for this purpose?

Recommended Answers

All 5 Replies

TextBox1.Text = ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text
        TextBox1.Text = ListView1.Items.Item(ListView1.FocusedItem.Index).Text
        TextBox1.Text = ListView1.Items(ListView1.FocusedItem.Index).Text
        TextBox1.Text = ListView1.FocusedItem.SubItems(0).Text
        TextBox1.Text = ListView1.FocusedItem.Text

And just fyi if it's actually the selected item you're looking for(which is sometimes different than focused item) then you can use:

TextBox1.Text = ListView1.Items.Item(ListView1.SelectedIndices(0)).SubItems(0).Text
        TextBox1.Text = ListView1.Items.Item(ListView1.SelectedItems(0).Index).SubItems(0).Text
        TextBox1.Text = ListView1.Items.Item(ListView1.SelectedIndices(0)).Text
        TextBox1.Text = ListView1.Items.Item(ListView1.SelectedItems(0).Index).Text
        TextBox1.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(0).Text
        TextBox1.Text = ListView1.Items(ListView1.SelectedItems(0).Index).SubItems(0).Text
        TextBox1.Text = ListView1.Items(ListView1.SelectedIndices(0)).Text
        TextBox1.Text = ListView1.Items(ListView1.SelectedItems(0).Index).Text
        TextBox1.Text = ListView1.SelectedItems(0).SubItems(0).Text
        TextBox1.Text = ListView1.SelectedItems(0).Text

Just for the hell of it I listed all the possibilities(most anyway), though I think the last one makes the most sense.

TextBox1.Text = ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text
        TextBox1.Text = ListView1.Items.Item(ListView1.FocusedItem.Index).Text
        TextBox1.Text = ListView1.Items(ListView1.FocusedItem.Index).Text
        TextBox1.Text = ListView1.FocusedItem.SubItems(0).Text
        TextBox1.Text = ListView1.FocusedItem.Text

And just fyi if it's actually the selected item you're looking for(which is sometimes different than focused item) then you can use:

TextBox1.Text = ListView1.Items.Item(ListView1.SelectedIndices(0)).SubItems(0).Text
        TextBox1.Text = ListView1.Items.Item(ListView1.SelectedItems(0).Index).SubItems(0).Text
        TextBox1.Text = ListView1.Items.Item(ListView1.SelectedIndices(0)).Text
        TextBox1.Text = ListView1.Items.Item(ListView1.SelectedItems(0).Index).Text
        TextBox1.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(0).Text
        TextBox1.Text = ListView1.Items(ListView1.SelectedItems(0).Index).SubItems(0).Text
        TextBox1.Text = ListView1.Items(ListView1.SelectedIndices(0)).Text
        TextBox1.Text = ListView1.Items(ListView1.SelectedItems(0).Index).Text
        TextBox1.Text = ListView1.SelectedItems(0).SubItems(0).Text
        TextBox1.Text = ListView1.SelectedItems(0).Text

Just for the hell of it I listed all the possibilities(most anyway), though I think the last one makes the most sense.

Dear Sir,

Unfortunately no method from above do not work.
When I move cursor in listview then shows following error message

InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index

There are four columns in listview

Please help again

Ok so I assume you're handling the ListView1.SelectedIndexChanged event? This event fires twice when you change selection, once to deselect the current selection and then again to select the new selection. So the first time firing there will be no selected items. In any event, you'd need to test for no selected items anyway. So, something like this...

If ListView1.SelectedItems.Count = 0 Then
            TextBox1.Text = ""
        Else
            TextBox1.Text = ListView1.SelectedItems(0).Text
        End If

Ok so I assume you're handling the ListView1.SelectedIndexChanged event? This event fires twice when you change selection, once to deselect the current selection and then again to select the new selection. So the first time firing there will be no selected items. In any event, you'd need to test for no selected items anyway. So, something like this...

If ListView1.SelectedItems.Count = 0 Then
            TextBox1.Text = ""
        Else
            TextBox1.Text = ListView1.SelectedItems(0).Text
        End If

Dear Sir,

In this way your codes work fine but if list box has more than two columns then what shold do

If ListView1.SelectedItems.Count = 0 Then
TextBox1.Text = ""

Else
TextBox1.Text = ListView1.SelectedItems(0).Text
TextBox2.Text = ???????????
TextBox3.Text = ???????????

End If

The other columns in a listview are subitems of the main item. So it would be:

TextBox1.Text = ListView1.SelectedItems(0).Text
TextBox2.Text = ListView1.SelectedItems(0).SubItems(1).Text
TextBox3.Text = ListView1.SelectedItems(0).SubItems(2).Text
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.