שתף באמצעות


adding a subitem to a specific colum in listview vb.net

Question

Tuesday, September 16, 2014 9:07 PM

hi every one ,

so i have a listview1 control that looks like this

how do i add a subitem to the Players column for example

i use

NewServer.SubItems.Add(recievedGM)

but it just adds to the next empty column

All replies (5)

Tuesday, September 16, 2014 9:45 PM ✅Answered

Hi,

 You can`t set the 4th SubItem`s Text unless you have added an Item (row) to the listview first and add Strings for each SubItem in that Item (row) even if they are empty strings. For example this will add the Item and SubItems to your listview and set the Player columns text to "Player 1".

        Dim lvi As New ListViewItem("ID 10645")
        lvi.SubItems.AddRange(New String() {"", "", "", "Player 1", ""})
        ListView1.Items.Add(lvi)

 

 After you have added strings to all the SubItems then you can set the text of any of the SubItems. For example, after running the above code you could then set the Max Players column like this.

ListView1.Items(0).SubItems(5).Text = "4 players"

If you say it can`t be done then i`ll try it


Wednesday, September 17, 2014 10:55 AM ✅Answered

Hi,

 You can use something like this to get the first selected Item`s text. It also shows how to get a SubItem`s text from the selected Item.

        If ListView1.SelectedItems.Count > 0 Then
            Dim si As ListViewItem = ListView1.SelectedItems(0) 'gets the first selected listviewitem

            MessageBox.Show(si.Text) 'shows the item`s text

            MessageBox.Show(si.SubItems(1).Text) 'shows the first subitems text
        End If

If you say it can`t be done then i`ll try it


Tuesday, September 16, 2014 9:53 PM

thanks for the info


Tuesday, September 16, 2014 9:58 PM

thanks for the info

You`re Welcome.   8)

If you say it can`t be done then i`ll try it


Wednesday, September 17, 2014 9:55 AM

so i finished the list "thanks to you"

but i want to know what the user selected from the list and get the ServerID from the selection

so let's say i have a list like this

server ID | Name

10 | x1

15 | x2

1651 | x5 

... etc

i have full row select as true

but i want to get the ID of the server that user selected