3 Replies - 1232 Views - Last Post: 16 February 2014 - 06:51 PM Rate Topic: -----

#1 zhb12810  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-February 14

Remove items from listbox on doubleclick that's populated from a q

Posted 16 February 2014 - 01:47 PM

Hi, I'm struggling with removing items from a listbox that have been added to a treeview that are the result of a OleDB query.

The below code retrieves a list of requirements from a an Access DB when a treenode is clicked and returns the values to a listbox.

Private Sub tvProgress_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles tvProgress.AfterSelect

        Dim da As New OleDb.OleDbDataAdapter("", "")
        Dim dt As New DataTable
        Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.aClients & ""
        Dim n As Integer

        For n = 0 To UBound(AllDetails)

            If tvProgress.Nodes.Count = 0 Then Exit Sub



            If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = e.Node.Text Then

                Dim eSearch As String = AllDetails(n).uCode
                Dim fSearch As String = AllDetails(n).uOps

                da.SelectCommand.Connection.ConnectionString = conn
                da.SelectCommand.CommandText = "SELECT Documents.DocName FROM Documents WHERE (Documents.UnitCode = ?) AND (Documents.OpName = ?) AND Documents.Required = True ORDER BY DocName"
                da.SelectCommand.Parameters.AddWithValue("@p1", eSearch)
                da.SelectCommand.Parameters.AddWithValue("@p2", fSearch)
                da.Fill(dt)
                dt.Rows.Add("Add Additional Requirement")
                lstRequired.DataSource = dt
                lstRequired.DisplayMember = "DocName"
                lstRequired.Refresh()

                Dim dl As DataTable = CType(lstRequired.DataSource, DataTable)
                Using sR = New IO.StreamReader(tFiles & "ProgExcluded.txt")
                    While (sR.Peek() > -1)
                        Dim rows() = dl.Select("DocName = '" + sR.ReadLine + "'")
                        For Each row In rows
                            row.Delete()
                        Next
                        dl.AcceptChanges()
                    End While
                End Using


                If Not My.Computer.FileSystem.DirectoryExists(AllDetails(n).uPath) Then

                    lstFiles.Items.Add("No Files Found!")
                    lstFiles.Enabled = False
                End If
            End If

        Next

        Exit Sub


    End Sub


What I'm struggling to work out is with the doubleclick event of the listbox. IS it possible to remove the item from the list in the listbox once double clicked and when the relevant node is selected again have it not show in the query list

Private Sub lstRequired_DoubleClick(sender As Object, e As EventArgs) Handles lstRequired.DoubleClick

        Dim Copy2 = tempMail & tvProgress.SelectedNode.Parent.Text & "\" & tvProgress.SelectedNode.Text & "\" & lstRequired.Text
        Dim Copy1 = rPath & lstRequired.Text

        If File.Exists(Copy2) Then
            MsgBox("File already added. Please edit from the view above", MsgBoxStyle.OkOnly, "Lynx Control Panel")
            Exit Sub
        End If

        If lstRequired.Text = "Add Additional Requirement" Then
            ReportSelect.Show()
            Exit Sub
        End If

        If File.Exists(Copy1) Then
            File.Copy(Copy1, Copy2)

            tvProgress.SelectedNode.Nodes.Add(lstRequired.Text)
        Else
            MsgBox("This file no longer exists in your Lynx Repository. Please select another", MsgBoxStyle.OkOnly, "Lynx Control Panel")
            Exit Sub
        End If
        

    End Sub


Any guidance would be gratefully received

Is This A Good Question/Topic? 0
  • +

Replies To: Remove items from listbox on doubleclick that's populated from a q

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5150
  • View blog
  • Posts: 13,837
  • Joined: 18-April 07

Re: Remove items from listbox on doubleclick that's populated from a q

Posted 16 February 2014 - 05:14 PM

Well sure, if you have removed it from the listbox and don't want it to show in the treeview when you select the node again, make sure that upon removal from the listbox that you also make the necessary changes to either the underlying database table (aka Documents) if you want the change to be permanent or remove it from the data table (aka dt) before you once again bind it to your listbox.

Hopefully that makes sense. :)
Was This Post Helpful? 0
  • +
  • -

#3 thecoat  Icon User is offline

  • D.I.C Addict

Reputation: 153
  • View blog
  • Posts: 537
  • Joined: 07-December 13

Re: Remove items from listbox on doubleclick that's populated from a q

Posted 16 February 2014 - 05:17 PM

View Postzhb12810, on 16 February 2014 - 01:47 PM, said:

What I'm struggling to work out is with the doubleclick event of the listbox. IS it possible to remove the item from the list in the listbox once double clicked and when the relevant node is selected again have it not show in the query list


I'm not sure I'm completely following what you are trying to accomplish. Reading your description of the issue, it sounds as if the listbox source data is the database, but looking at the code it looks like you are getting the info from the file system. I'll just mention a couple of things that may be helpful/applicable.

Removing the double clicked item in a listbox should be as simple as the following:

Private Sub lstRequired_DoubleClick(sender As Object, e As EventArgs) Handles lstRequired.DoubleClick
'other operations
lstRequired.Items.Remove(lstRequired.SelectedItem)
'other operations
End Sub



Having it not show up afterward is a matter of either actually removing it from the source (file system or database), or adding it to a global list that is excluded when the list is populated. In most cases I'd think you'd want to remove the element from the actual source although you may want to delay this operation until the user commits to an action at which point maintaining a list in memory would really be appropriate.

Lastly both TreeNodes and ListViewItems have a property called tag which you can assign any object to, meaning it can be a standard datatype or a custom object. So in either you could store a path & file string, or a database key. So once you get a reference to the item you want to work with if you've assigned a value to the tag property you can retrieve it.
Was This Post Helpful? 1
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1727
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Remove items from listbox on doubleclick that's populated from a q

Posted 16 February 2014 - 06:51 PM

This is exactly what I was thinking. You either need to remove it from the data source (file system or database) or ensure that it is omitted from the query results. But like thecoat I'm not sure I'm fully following your requirements.

This post has been edited by CharlieMay: 16 February 2014 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1