Link to home
Create AccountLog in
Avatar of Kit_Kat111
Kit_Kat111

asked on

Learning - populating a combo box using a directory list

*need help quick!*

Trying to understand all of this, what I am trying to do is take a combo box (cmbDate) to display three sets of dates, when the user selects the date another combo box (cmbTime) is populated from a directory list.

However looking at books, online sources and old code but I find myself confused and in a rush to get this done. Please help! File1 is not declared, but what is this the name the file to open? Data is used incorrectly ('System.Data' is a namespace and cannot be used as an expression), and cmbTime.AddItem() is used incorrectly ('AddItem' is not a member of 'System.Windows.Forms.ComboBox')

Please help me fix these errors and help me understand the code more...thank you very much. Please note I do not want a dialog box and I am not using Access or SQL, just a listing from a folder.) Thanks again!


    Private Sub cmbDate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDate.SelectedIndexChanged

        If cmbDate.SelectedValue = "2/24/06" Then
            File1.Open("Z:\Queue Review Files\2-24\Cam 1")
            Do While Not File1.EOF
                Data = File1.LineInputString
                cmbTime.AddItem()
            Loop
            File1.Close()


        ElseIf cmbDate.SelectedValue = "2/27/06" Then

            File1.Open("Z:\Queue Review Files\2-27\Cam 1")
            Do While Not File1.EOF
                Data = File1.LineInputString
                cmbTime.AddItem()
            Loop

          Else cmbDate.SelectedValue = "2/28/06" Then

            File1.Open("Z:\Queue Review Files\2-28\Cam 1")
            Do While Not File1.EOF
                Data = File1.LineInputString
                cmbTime.AddItem()
            Loop

        End If



    End Sub
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

What is "Z:\Queue Review Files\2-24\Cam 1"?

Is it a File or a Folder?

Are you trying to add each line of the file to the ComobBox?

Or are you trying to add the name of each File in this Folder to the ComboBox?
Avatar of fruhj
fruhj

well you have a few things going on here.

I'll do what I can to help and I invite others to join me as I doubt I can do it all myself. And I just hit refresh and see that idle is helping also- you are in good hands!

additem is being used wrong. It'll take me a minute to dig up an example though...
Avatar of Kit_Kat111

ASKER

It is a folder, I am trying to add each file in that folder which is a jpg into the combo box.
Thanks I really need the help, I appreciate the assistance....thank you everyone!
Hey are you using visual studio 2005?

They've add a nice "how do I" section of help and theres a nice bit on adding items to a combo box.
you can find this on the start page left side, or from online help - the 'how do I' button on top.

from the vb how do I page, click windows applications (5th blue link down)
then scroll down under the heading "Listbox, combobox, and checkedlistbox controls")

for your add item,

you could try cmbtime.items.add(Data.tostring())
in particular the blue link: How to: Add and remove items from a windows forms combo box ...

there's also help on using files if you need assistance there.

okay I will look at the help files, and yes I am using VS 2005. I am still experimenting with my code, I recently didn't get any errors but the combo box didn't populate with the directory list.

Besides not working I also need to read different folder for each date chosen....argh

        Dim dir As New IO.DirectoryInfo("Z:\Queue Review Files\2-24")
        Dim diar1 As IO.FileInfo() = dir.GetFiles()
        Dim dra As IO.FileInfo

        If cmbDate.SelectedValue = "2/24/06" Then
            For Each dra In diar1
                cmbTime.Items.Add(dra)
            Next

        ElseIf cmbDate.SelectedValue = "2/27/06" Then
            For Each dra In diar1
                cmbTime.Items.Add(dra)
            Next

        ElseIf cmbDate.SelectedValue = "2/28/06" Then
            For Each dra In diar1
                cmbTime.Items.Add(dra)
            Next

        End If
Kit_Kat111,

you might try cmbTime.items.add(dra.tostring)

You can almost always add a .tostring to the end of almost everything - that way if dra is an object with other properites, you'll at least get the default converted to a string - might not be what you're looking for, but a good clue that you need to add a few more qualifiers (ie it might be dra.filename.tostring)

I suspect your multidates thing can be taken care of with some logic on your end
from what I'm seeing here - you might be able to replace the literal strings "D/MM/YY" with a variable that represents it and only need one statement.

another thing you can do (probably not the best way, but pointing it out for educational purposes)
the assumption here is that your code needs to pull new values for each date, and that the code to populate the combo is usable no matter what those values are
again, i'dbe more in favor of eliminating the case all together, but I think I'd need to see more of your code to help you with that.
Select Case cmbdate.selectedvalue
  Case "2/24/06"
     'do something specific to this date
   case "2/27/06"
     'do something specific..
   case else
     'put a default here -
end select
 for each dra in diar1
   cmbtime.items.add(dra)
next
Well I got it working BUT, I need to have more than one folder to access depending on the date chosen. I tried declaring more variables and opening a new dir and more  if else statement but I am stuck. HELP!!!!

        ' making a reference to a directory
        Dim dir As New IO.DirectoryInfo("C:\Projects\Darryl\Queue Review Files\2-24\Cam 1")  <<<<Needs to change
        Dim files As IO.FileInfo() = dir.GetFiles("*.jpg")
        Dim file As IO.FileInfo

        'If cmbDate.SelectedValue = "February 24, 2006" Then
        ' listing the names of all the files in the specified directory
        For Each file In files
            cmbTime.Items.Add(file)
        Next

        'End If
*Assuming you have WELL formed dates in cmbDate:

    Private Sub cmbDate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDate.SelectedIndexChanged
        Try
            Dim dt As New DateTime(cmbDate.SelectedValue)
            Dim dir As New IO.DirectoryInfo("C:\Projects\Darryl\Queue Review Files\" & dt.Month & "-" & dt.Day & "\Cam 1")
            Dim files As IO.FileInfo() = dir.GetFiles("*.jpg")

            cmbTime.BeginUpdate()
            cmbTime.Items.Clear()
            If files.Length > 0 Then
                cmbTime.Items.AddRange(files)
            End If
            cmbTime.EndUpdate()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Querying Files", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
COOL - Learn something new every day - especially when Idle Mind is in the room!
I didn't know about the combo.beginupdate & combo.endupdate!

- Jack
Your code looks good, especially that you are thinking ahead with EOF and error code.
But I am confused what are: dt.Month and dt.Day? Is that what you are assuming what is in the directory? All that is that directory are the jpgs.

I really appreciate all the help...ty!
Looking at the code below:

    Dim dt As New DateTime(cmbDate.SelectedValue)
    Dim dir As New IO.DirectoryInfo("C:\Projects\Darryl\Queue Review Files\" & dt.Month & "-" & dt.Day & "\Cam 1")

We take the selected value from cmbDate, "February 24, 2006" in your example, and build a DateTime instance from it:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclasstopic.asp

It looks like there is a pattern to the folders you need based on the date:

    "February 24, 2006" --> "C:\Projects\Darryl\Queue Review Files\2-24\Cam 1"

    "February 25, 2006" --> "C:\Projects\Darryl\Queue Review Files\2-25\Cam 1"

    "February 26, 2006" --> "C:\Projects\Darryl\Queue Review Files\2-26\Cam 1"

So second line of code builds a path to this folder using the info in the DateTime instance "dt" via the Month() and Day() properties.

Is this correct?  If there isn't a predictable pattern for each date then you need a different approach completely...
yes that is it...I thought that is what you meant after I looked after it more....You are so smart!

Remember I am learning and have no time...thank you so very much!!!

My only question is that I don't have Month or Date declared. There are only three folders 2-24, 2-27, 2-28.




You don't need to declare Month and Day as they are built-in properties of the DateTime class ("dt" in the code).
Idle, I wish you lived near Chicago!
Lol...thanx fruhj.  I'm actually in Colorado right now but will be moving to California in the fall.  Sunshine here I come...
well when I ran the program I got "1-1\Cam 1" path could not be found.

Chicago must be cold right now.....

Thanks guys for sticking around and helping me!!!
Change:

    Dim dir As New IO.DirectoryInfo("C:\Projects\Darryl\Queue Review Files\" & dt.Month & "-" & dt.Day & "\Cam 1")

To:

    Dim path As String = "C:\Projects\Darryl\Queue Review Files\" & dt.Month & "-" & dt.Day & "\Cam 1"
    MsgBox(path)
    Dim dir As New IO.DirectoryInfo(path)

and let us know what is being displayed in the MsgBox.
okay.....

first alert box says just
C:\Projects\Darryl\Queue Review Files\1-1\Cam 1

then second is that same path not found:
"C:\Projects\Darryl\Queue Review Files\1-1\Cam 1" path could not be found.


Sorry for the hassle..
It's no hassle...

The code produced what I thought it should.

Is this the correct directory?   Does this path ACTUALLY exist on the drive?

1-1 would mean "January 1"...not consistent with your dates.

What is returned by cmbDate.SelectedValue?

Try adding:

    MsgBox(cmbDate.SelectedValue)
yes the path exists and I did get it to pull up with the code above, but there are only three folders 2-24, 2-27 and 2-28 no other dates.

all cmbDate is a ddl of those three days in long format: February 24, 2006.

blank windows alert pop up and then the path not found again.

BTW, I am not Daryll, but rather Tina from Tennessee. ;)
My fault...I think it's bombing on the DateTime line.

Try this instead:

    Dim dt As DateTime = DateTime.ParseExact(cmbDate.SelectedValue, "MMMM d, yyyy", Nothing)
alert popup - String reference not set to an instance of a string. Parameter name: s

tyvm
I think we have remnants left in the code...can you post it please?

    Private Sub cmbDate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDate.SelectedIndexChanged
        Try
            Dim dt As DateTime = DateTime.ParseExact(cmbDate.SelectedValue, "MMMM d, yyyy", Nothing)
            Dim path As String = "C:\Projects\Darryl\Queue Review Files\" & dt.Month & "-" & dt.Day & "\Cam 1"
            MsgBox(cmbDate.SelectedValue)
            Dim dir As New IO.DirectoryInfo(path)
            Dim files As IO.FileInfo() = dir.GetFiles("*.jpg")

            cmbTime.BeginUpdate()
            cmbTime.Items.Clear()
            If files.Length > 0 Then
                cmbTime.Items.AddRange(files)
            End If
            cmbTime.EndUpdate()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Querying Files", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
YEAH!!! It worked...thanks for all your help. I will award the points, my gratitude is overflowing...thank you!

Since I am learning and have to get this completed ASAP I will have more questions....like my next step...argh.

TYVM!!!