I have programmatically created a listview in n # of tabpages to display information gathered from an SQL database. Once created, i want the user to be able to select an item from any of the listview's. How can i reference the selected item?

This is the code that creates each of the tabpages

Private Sub ComboBoxEmail_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxEmail.SelectedIndexChanged
        If ComboBoxEmail.SelectedIndex > -1 Then
            ITConn.ConnectionString = ""
            ITConn.Open()

            Dim SearchCase1 As New SqlCommand("SELECT [Type] FROM Safety_Training WHERE Email = '" & ComboBoxEmail.Text & "' GROUP BY [Type]", ITConn)
            Dim Reader1 As SqlDataReader = SearchCase1.ExecuteReader
            SearchCase1.Dispose()

            Dim lst As New List(Of String)
            Dim switch As Boolean = False
            If Reader1.HasRows Then
                switch = True
                'Set the text for the first tab to the first training type
                TabTraining.TabPages.Clear()
                TabTraining.TabPages.Add("Training")
                Reader1.Read()
                TabTraining.TabPages(0).Text = Reader1(0)
                lst.Add(Reader1(0))

                'add new tabs and set the remaining texts to the other types
                While Reader1.Read
                    TabTraining.TabPages.Add(Reader1(0))
                    lst.Add(Reader1(0))
                End While
            Else
                TabTraining.TabPages.Clear()
                TabTraining.TabPages.Add("Training")
            End If

            Reader1.Close()

            If switch Then
                For i As Integer = 0 To lst.Count - 1
                    TabTraining.TabPages(i).Controls.Add(AddListView(lst.Item(i)))
                Next
            End If

            ITConn.Close()
        End If
    End Sub

This is the code that adds each listview to the tabpages

Function AddListView(ByVal Type As String) As ListView
        Dim lstview As New ListView

        lstview.Dock = DockStyle.Fill
        lstview.Columns.Add("Description")
        lstview.Columns.Add("Provider")
        lstview.Columns.Add("Date Taken")
        lstview.Columns.Add("Expiry Date")
        lstview.Columns(0).Width = 280
        lstview.Columns(1).Width = 180
        lstview.Columns(2).Width = 130
        lstview.Columns(3).Width = 130
        lstview.View = View.Details
        lstview.FullRowSelect = True
        lstview.MultiSelect = False
        lstview.Name = Type



        Dim SearchCase1 As New SqlCommand("SELECT * FROM Safety_Training WHERE Email = '" & ComboBoxEmail.Text & "' AND Type = '" & Type & "' AND NextTraining = 9999", ITConn)
        Dim Reader1 As SqlDataReader = SearchCase1.ExecuteReader
        SearchCase1.Dispose()
        If Reader1.HasRows Then
            Reader1.Read()
            Dim pt As Integer = Reader1(7)

            lstview.Items.Add(Reader1(3))
            lstview.Items(0).SubItems.Add(Reader1(4))
            If IsDBNull(Reader1(5)) Then
                lstview.Items(0).SubItems.Add("-")
            Else
                lstview.Items(0).SubItems.Add(Reader1(5))
            End If
            If IsDBNull(Reader1(6)) Then
                lstview.Items(0).SubItems.Add("-")
            Else
                lstview.Items(0).SubItems.Add(Reader1(6))
            End If

            Reader1.Close()

            If pt <> 0 Then
                AddListItem(lstview, pt)
            End If
        End If

        Return lstview
    End Function

This is the code that adds each listviewitem to each listview

Function AddListItem(ByVal lv As ListView, ByVal pt As Integer) As Boolean
        Dim lstviewitm As New ListViewItem

        Dim SearchCase1 As New SqlCommand("SELECT * FROM Safety_Training WHERE ID = " & pt, ITConn)
        Dim Reader1 As SqlDataReader = SearchCase1.ExecuteReader
        SearchCase1.Dispose()

        If Reader1.HasRows Then
            Reader1.Read()

            lstviewitm.Text = Reader1(3)
            lstviewitm.SubItems.Add(Reader1(4))
            lstviewitm.SubItems.Add(Reader1(5))
            lstviewitm.SubItems.Add(Reader1(6))

            lv.Items.Add(lstviewitm)
            Dim npt As Integer = Reader1(7)
            Reader1.Close()
            If npt <> 0 Then
                AddListItem(lv, npt)
            End If
        End If

        Return True
    End Function

All i want is to know that when a button control is clicked, which item is being selected in which listview?

a very simple example:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
		If lv.SelectedItems.Count = 0 Then
			Return 'nothing selected
		End If

		If lv.SelectedItems.Count = 1 Then
			Dim lstviewitm As ListViewItem = lv.SelectedItems(0)
                        MsgBox(lstviewitm.Text)
		Else 'more than one item selected
			For Each _item As ListViewItem In lv.SelectedItems
				Dim content As String = ""
				For Each subitem As ListViewItem.ListViewSubItem In _item.SubItems
					content += subitem.Text & vbNewLine
				Next
				MsgBox(String.Format("selected Item:{0}{1}{1}Subitems:{1}{2}", _item.Text, vbNewLine, content))
			Next
		End If
	End Sub
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.