• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

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
0
Kit_Kat111
Asked:
Kit_Kat111
  • 12
  • 10
  • 5
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
 
fruhjCommented:
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...
0
 
Kit_Kat111Author Commented:
It is a folder, I am trying to add each file in that folder which is a jpg into the combo box.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Kit_Kat111Author Commented:
Thanks I really need the help, I appreciate the assistance....thank you everyone!
0
 
fruhjCommented:
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.

0
 
Kit_Kat111Author Commented:
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
0
 
fruhjCommented:
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
0
 
Kit_Kat111Author Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*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
0
 
fruhjCommented:
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
0
 
Kit_Kat111Author Commented:
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!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
 
Kit_Kat111Author Commented:
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.




0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You don't need to declare Month and Day as they are built-in properties of the DateTime class ("dt" in the code).
0
 
fruhjCommented:
Idle, I wish you lived near Chicago!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...thanx fruhj.  I'm actually in Colorado right now but will be moving to California in the fall.  Sunshine here I come...
0
 
Kit_Kat111Author Commented:
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!!!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
Kit_Kat111Author Commented:
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..
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
0
 
Kit_Kat111Author Commented:
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. ;)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
0
 
Kit_Kat111Author Commented:
alert popup - String reference not set to an instance of a string. Parameter name: s

tyvm
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think we have remnants left in the code...can you post it please?
0
 
Kit_Kat111Author Commented:

    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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think cmbDate.SelectedValue is returning an empty string...

Try this line instead:

    Dim dt As DateTime = DateTime.ParseExact(cmbDate.Items(cmbDate.SelectedIndex), "MMMM d, yyyy", Nothing)

and take out the MsgBox() line.
0
 
Kit_Kat111Author Commented:
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!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 12
  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now