Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Learning - populating a combo box using a directory list

Posted on 2006-03-21
27
Medium Priority
?
338 Views
Last Modified: 2010-04-23
*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
Comment
Question by:Kit_Kat111
  • 12
  • 10
  • 5
27 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16254002
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
 
LVL 12

Expert Comment

by:fruhj
ID: 16254024
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
 

Author Comment

by:Kit_Kat111
ID: 16254044
It is a folder, I am trying to add each file in that folder which is a jpg into the combo box.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kit_Kat111
ID: 16254052
Thanks I really need the help, I appreciate the assistance....thank you everyone!
0
 
LVL 12

Expert Comment

by:fruhj
ID: 16254068
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
 

Author Comment

by:Kit_Kat111
ID: 16254145
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
 
LVL 12

Expert Comment

by:fruhj
ID: 16254182
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
 

Author Comment

by:Kit_Kat111
ID: 16257267
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16257529
*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
 
LVL 12

Expert Comment

by:fruhj
ID: 16258180
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
 

Author Comment

by:Kit_Kat111
ID: 16258212
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16258334
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
 

Author Comment

by:Kit_Kat111
ID: 16258530
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16258583
You don't need to declare Month and Day as they are built-in properties of the DateTime class ("dt" in the code).
0
 
LVL 12

Expert Comment

by:fruhj
ID: 16258597
Idle, I wish you lived near Chicago!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16258630
Lol...thanx fruhj.  I'm actually in Colorado right now but will be moving to California in the fall.  Sunshine here I come...
0
 

Author Comment

by:Kit_Kat111
ID: 16258667
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16258697
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
 

Author Comment

by:Kit_Kat111
ID: 16258775
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16258817
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
 

Author Comment

by:Kit_Kat111
ID: 16258859
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16259034
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
 

Author Comment

by:Kit_Kat111
ID: 16259106
alert popup - String reference not set to an instance of a string. Parameter name: s

tyvm
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16259187
I think we have remnants left in the code...can you post it please?
0
 

Author Comment

by:Kit_Kat111
ID: 16259258

    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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16259454
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
 

Author Comment

by:Kit_Kat111
ID: 16259535
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question