What code do I use to populate a listview, and how would I handle when a button is clicked, which item in the list view has been selected?
Cheers. :)
Printable View
What code do I use to populate a listview, and how would I handle when a button is clicked, which item in the list view has been selected?
Cheers. :)
Listbox1.Items.Add("Whatever")
Listbox1.SelectedValue
Quote:
Originally Posted by wild_bill
what wild_bill gave will work but thats just like a cumb of the whole pie...
I'll be back in a flash to give you a little more code that might be more helpful. :wave:
ok here some code... don't worry about some of the code because they are functons that i have created for the project that i'm working on. The code here will help you with the loading/adding of items to the listview... I'll post a second post with the handling which is selected.
VB Code:
Friend Sub LoadProjects() Dim cn As New OleDbConnection(GetOleDbConnection) Dim cmd As New OleDbCommand("SELECT colldev_projects.*, colldev_users.fname, colldev_users.lname FROM colldev_users INNER JOIN colldev_projects ON colldev_users.id = colldev_projects.manager_id") Dim dr As OleDbDataReader cmd.Connection = cn cmd.CommandTimeout = GetCommandTimeOut() cn.Open() If cn.State = ConnectionState.Open Then dr = cmd.ExecuteReader If dr.HasRows Then Me.lvProjects.SuspendLayout() Me.lvProjects.Items.Clear() Do Until Not dr.Read Application.DoEvents() Dim lvitm As New ListViewItem With lvitm If CBool(dr("inactive")) Then .BackColor = Color.Yellow End If .Tag = CStr(dr("projid")) .Text = CStr(dr("library_name")) With .SubItems .Add(CStr(dr("fname")) + " " + CStr(dr("lname"))) .Add(CStr(dr("start_date"))) .Add(CStr(dr("complete_date"))) .Add(Format(CDbl(dr("est_num_items")), "###,###,###,###,##0")) .Add(Format(CDbl(dr("proj_amt")), "###,###,###,###,##0.00")) .Add(CBool(dr("inactive")).ToString) .Add(CStr(dr("projid"))) .Add(CStr(dr("manager_id"))) End With End With Me.lvProjects.Items.Add(lvitm) lvitm = Nothing Loop Me.lvProjects.ResumeLayout() End If End If dr.Close() cn.Close() cmd.Dispose() cn.Dispose() dr = Nothing cmd = Nothing cn = Nothing End Sub
take notice to the My.lvProjects.SelectedItems(0).Tag...
by using the SelectedItems collection you get access to all items that would be selected in the listview. this is helpful for when have the listview allowed to have multiple selection. then just loop through the items in the selecteditems collection
VB Code:
Private Sub btnEdit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnEdit.Click '******************************************************************************************* Cursor = Cursors.WaitCursor Dim fcp As New frmCreateProject(CStr(Me.lvProjects.SelectedItems(0).Tag)) AddHandler fcp.ProjectSaved, AddressOf RefreshProjectView With fcp fcp.Text = "Edit Project" Cursor = Cursors.Default If .ShowDialog(Me) = DialogResult.OK Then Me.btnEdit.Enabled = False Me.btnDelete.Enabled = False Me.btnAct.Enabled = False Me.btnDeAct.Enabled = False Me.btnManager.Enabled = False Me.lvProjects.SelectedItems.Clear() End If End With fcp = Nothing End Sub
here is the code for looping through the collection
VB Code:
For Each itm As ListViewItem In Me.lvProjects.SelectedItems Application.DoEvents() MsgBox(itm.Tag) MsgBox(itm.Text) MsgBox(itm.SubItems(0).Text) '<-- this is the same as the previous line. MsgBox(itm.SubItems(1).Text) '<-- if you have subitems. '. '. '. Next
have a nice day :afrog: