Results 1 to 9 of 9

Thread: [RESOLVED] Need help with a checked list box

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] Need help with a checked list box

    Hi, i'm kinda of a newbie in programming and i'm trying to make a program that searches for a specific file in a specific file, so when the program loads it "writes" in checked list box the names of the drives installed to the computer, but what i want to do is: when the user checks the box, i want it to search for the file in that drive, but i don't know how to do it...

    I'm using:

    Code:
    Imports System.IO
    Imports System.Collections.ObjectModel
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            srDrives.Enabled = False
            Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
            srDrives.Items.AddRange(allDrives)
        End Sub
    It determines the names of the installed drives...
    Code:
        Private Sub srCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles srCheck.CheckedChanged
            If srCheck.CheckState = CheckState.Checked Then
                srList.Items.AddRange(Directory.GetFiles("C:\", srCombo.Text, SearchOption.AllDirectories))
            End If
    And this one searches for the file in C:\

    Any ideas on how to make it search in the other drives using the checked listbox?

    THANKS

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Need help with a checked list box

    I can't see why you would want to use a CheckedListBox for this and it doesn't look like you are, or at least it looks like you're not using the functionality of the check boxes. It looks like you're using a completely separate CheckBox control.

    Also, do you really want to add every single file on a drive into your list? It could be hundreds or even thousands of files. Apart from the time it would take to create such a list, what use could it be to the user being so long?

    Please provide a FULL and CLEAR description of WHAT you're trying to achieve and WHY and we can provide the most informed advice possible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Need help with a checked list box

    What i want to do, is simple a program that searches for a file and then, if the user wants to, run the selected/found file, how can i do that?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Need help with a checked list box

    Gee, I'm glad I emphasised the words FULL and CLEAR because we may have got just a couple of words otherwise. You say you want to search for a file but what does "search" mean? It could mean different things in different situations.

    You said in your original post that you want to list every file there was on a drive? Do you still want to do that? If not then what files do you want to list? Just the files in a specific folder but not in the subfolders? Just files of a particular type? Some other filter? How will the user let your app know what files to list? Etc., etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Need help with a checked list box

    Lol, thanks for the sarcasm, jk.
    What i want to do is a search program, similar to windows search file program in windows xp.

    It would have a textbox for you to write the name of what you are looking for, you select the drive you want the program to search in and result would be displayed in a listbox.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Need help with a checked list box

    OK, so then, you might want a CheckedListBox to show the drives available and the user could check the ones they want searched. You'd also provide a TextBox where the user could enter a name filter. You'd then provide a Button that they would click to initiate the search. Finally, you'd need a ListBox or, more likely, a ListView to display the results. Does that sound like the sort of thing you'd want?

    If you're going to search an entire drive then you can't use Directory.GetFiles the way you did in your first post. On the C: drive at least, and maybe on others, the search will encounter at least one folder that it cannot access and an exception will be thrown. You'll have to create your own recursive search function so that you can handle exceptions on inaccessible folders and continue.

    You should start by reading this.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Need help with a checked list box

    Yes, i started reading that, but i didn't quite understand it... I have everything you listed above, all i'm missing is the recursive search function, i just don't know how to combine that with selecting the drive and the recursive search... can you give me a hint? pleasee

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Need help with a checked list box

    Try this:

    1. Create a new WinForms app.
    2. Add a ListBox, two Buttons and a ListView.
    3. Set the View of the ListView to Details and add two columns.
    4. Add a BackgroundWorker.
    5. Set the WorkerReportsProgress and WorkerSupportsCancellation properties to True.
    6. Add the following code:
    Code:
    Imports System.IO
    Imports System.ComponentModel
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, _
                               ByVal e As EventArgs) Handles MyBase.Load
            'Display a list of available drives in the ListBox.
            Me.ListBox1.DataSource = DriveInfo.GetDrives()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As Object, _
                                  ByVal e As EventArgs) Handles Button1.Click
            'Get the selected drive path.
            Dim drive As String = Me.ListBox1.Text
    
            'Start searching the drive for files asynchronously.
            Me.BackgroundWorker1.RunWorkerAsync(drive)
        End Sub
    
        Private Sub Button2_Click(ByVal sender As Object, _
                                  ByVal e As EventArgs) Handles Button2.Click
            'Cancel the asynchronous search.
            Me.BackgroundWorker1.CancelAsync()
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
                                             ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim drive As String = CStr(e.Argument)
            Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
    
            'Initiate the recursive file search.
            'If GetFiles returns False it means the search was cancelled.
            e.Cancel = Not Me.GetFiles(drive, worker)
        End Sub
    
        Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
                                                      ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
            'Get the items passed from the secondary thread.
            Dim items As ListViewItem() = DirectCast(e.UserState, ListViewItem())
    
            'Add the items to the list.
            Me.ListView1.Items.AddRange(items)
        End Sub
    
        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
                                                         ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            If e.Cancelled Then
                'The search was cancelled before completing.
                MessageBox.Show("Operation cancelled")
            Else
                'The search completed normally.
                MessageBox.Show("Operation complete")
            End If
        End Sub
    
        Private Function GetFiles(ByVal folder As String, ByVal worker As BackgroundWorker) As Boolean
            Dim cancelled As Boolean = False
    
            If worker.CancellationPending Then
                'The user has requested that the search be cancelled.
                cancelled = True
            Else
                Try
                    'Get the files in the current folder.
                    Dim files As String() = Directory.GetFiles(folder)
                    Dim upperBound As Integer = files.GetUpperBound(0)
                    Dim items(upperBound) As ListViewItem
                    Dim file As String
    
                    'Create a ListViewItem for each file.
                    For index As Integer = 0 To upperBound
                        file = files(index)
                        items(index) = New ListViewItem(New String() {Path.GetFileName(file), _
                                                                      Path.GetDirectoryName(file)})
                    Next
    
                    'Pass the items to the UI thread to be displayed.
                    worker.ReportProgress(0, items)
    
                    'Search each subfolder.
                    For Each subfolder As String In Directory.GetDirectories(folder)
                        'This is the recursive call, i.e. the method calls itself.
                        If Not Me.GetFiles(subfolder, worker) Then
                            'The user has requested that the search be cancelled.
                            cancelled = True
                            Exit For
                        End If
                    Next
                Catch ex As Exception
                    'The folder is inaccessible.  Ignore and continue.
                End Try
            End If
    
            'The sub-search completed if it was not cancelled.
            Return Not cancelled
        End Function
    
    End Class
    7. Run the project.
    8. Select a drive from the ListBox and click the first Button.
    9. Sit back and watch the ListView fill with file names and the folders that contain them.
    10. Click the second Button to cancel the search.

    You can now study that code, including stepping through it while it's running. Once you understand it you can modify it as required.

    Note that this code uses a BackgroundWorker to do the actual searching to keep the UI thread free to respond to user interaction.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Need help with a checked list box

    Thanks very much for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width