Hi. I have a program that detects all the folders that are open and displays each one in a ListBox item.

2014-12-25_15_49_20-DLAUBEFN_(Running)_-_Microsoft_Visual_Studio.png

What I want it to do is if a certain folder is closed, I want it to start a batch file and close itself. I know how to make it do these things but there's a problem. Here's my code:

Imports Shell32
Public Class Form1
    Dim open As Integer
    Dim Lb As New ListBox
    Dim WithEvents RoutineTimer As New Timer
    Private Sub Form1_Load() Handles MyBase.Load
        Lb.Parent = Me
        Lb.Dock = DockStyle.Fill
        Me.Text = "XPLHHNSJKHF 1.0"
        RoutineTimer.Interval = 3000
        RoutineTimer.Start()
    End Sub
    Sub GetOpenedFolder()
        Dim MShell As New Shell
        Dim SFV As ShellFolderView
        Lb.Items.Clear()
        On Error Resume Next
        For Each o In MShell.Windows
            If TypeName(o.document) <> "HTMLDocument" Then
                SFV = o.document
                If SFV.Folder.Items.Count > 0 Then
                    Lb.Items.Add(TrimPath(CType(SFV.Folder.Items(0),  _
                                 ShellFolderItem).Path))
                End If
            End If
            'Scroll through all items in a listbox
            Dim itemText As String = Lb.ToString 'Get item string
            If itemText.Contains("C:\Users\David\Documents\My Documents\Computer Programs\David\KOAGCS") Then 'Check to see if item has our search var
                open = 1
            End If
            MsgBox(open)
        Next
    End Sub

    Sub Timer_Job() Handles RoutineTimer.Tick
        GetOpenedFolder()
    End Sub

    Function TrimPath(ByRef s As String) As String
        Return s.Remove(InStrRev(s, "\"))
    End Function

End Class

The problem is the If command for when we're checking the item isn't working. I don't know why. But it's not detecting it, and not setting the open variable to 1 like it should.

2014-12-25_16_02_48-DLAUBEFN_(Running)_-_Microsoft_Visual_Studio.png

Can someone help me? Thanks.

David

Recommended Answers

All 5 Replies

Your problem is in this section.

'Scroll through all items in a listbox
Dim itemText As String = Lb.ToString 'Get item string
If itemText.Contains("C:\Users\David\Documents\My >Documents\Computer Programs\David\KOAGCS") Then 'Check to see if item has our >search var
open = 1
End If
MsgBox(open)

You must need some modification. It should be like

 'Scroll through all items in a listbox
  If Lb.Items.Contains("C:\Users\David\Documents\My Documents\Computer Programs\David\KOAGCS") Then 'Check to see if item has our search var
    MsgBox("Open")
  End If

and do this job after compleation of For loop, not in the loop.

It still doesn't work. :/ I don't know why, it's not detecting the Folder. It's stupid.

I did a modification in your codes, you can try it.

Imports Shell32
Public Class Form1
    Dim open As Integer
    Dim Lb As New ListBox
    Dim WithEvents RoutineTimer As New Timer
    Private Sub Form1_Load() Handles MyBase.Load
        Lb.Parent = Me
        Lb.Dock = DockStyle.Fill
        Me.Text = "XPLHHNSJKHF 1.0"
        RoutineTimer.Interval = 3000
        RoutineTimer.Start()
    End Sub
    Sub GetOpenedFolder()
        Dim MShell As New Shell
        Dim SFV As ShellFolderView
        Lb.Items.Clear()
        On Error Resume Next
        For Each o In MShell.Windows
            If TypeName(o.document) <> "HTMLDocument" Then
                SFV = o.document
                If SFV.Folder.Items.Count > 0 Then
                    Lb.Items.Add(TrimPath(CType(SFV.Folder.Items(0),  _
                                 ShellFolderItem).Path))
                End If
            End If
        Next

        For i As Integer = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i) = "C:\Users\David\Documents\My Documents\Computer Programs\David\KOAGCS" Then
                MessageBox.Show("Open")
                ListBox1.SelectedIndex = i
                Exit For
            End If
        Next
    End Sub
    Sub Timer_Job() Handles RoutineTimer.Tick
        GetOpenedFolder()
    End Sub
    Function TrimPath(ByRef s As String) As String
        Return s.Remove(InStrRev(s, "\"))
    End Function
End Class

Hope it can help you.

I found out why it was doing anything. It doesn't read the spaces between the folders in the path. So I'm going to do a replace string command like this:

ListBox1.Items.Contains = Replace(ListBox1.Items.Contains, " ", "")

or something like that in the code somewhere. I'll tell you if it works.

Nevermind. I realized this whole reason why was because I forgot to put the "\" after the file name in the If statement. XD I'm so stupid. I've been trying to figure that out for months cx Thanks for your help though, it looks a lot neater now.

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.