Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

VS 2010 Assigning array to ComboBox SelectedValue and Text Files

$
0
0
Hey there folks!

I have been desperately trying to uncover exactly what I have to do to make this process happen, and I'm dead set on minimizing unnecessary lines of code because I will be printing out the pages later.

I have a ComboBox with a collection of items generated through the properties window instead of code. These items are categories for sorting purposes, and when selected, will display the associated files in a ListBox (which will allow for further selection). The files have been generated by someone other than myself, but they follow very strict guidelines for how each line in the file is laid out. The file names themselves are just incredibly different from one another and max out at 8 characters long, but one of the lines inside each file reads exactly like one of the options from the ComboBox (for sorting purposes obviously). For example, one of the items in the Combo Box is Midwest Families. A number of the text files have Midwest Families (written exactly as mine) on Line 2 of the document; however, they all have different file names that basically indicate Midwest (i.e. mdwst, midwest, midwst, mdwest, etc.). I copied all of those file names into another text file, allowing each their own line, and labelled them similar to how they were before, but all under one name. So the Midwest ones are listed as MidCat.txt (and the others follow the same example sorting into a total of 7 categories) and is inside a sub directory of the actual files.

Now for the part I am having trouble with:

I could go into the code and manually enter everything, but it looks horrible and I know it's completely unnecessary. What I would like to do is set the file name of each file I created as an array or something, and associated each directly with the item listed in the combobox. I've been desperately trying to avoid setting each file individually to each item on the list, but my attempts to solve this with combobox.valuemember, arrays, and a variety of things, just continues to generate errors. These are examples of the code I have written so far, and it's pathetic honestly because I scrap things the moment I get errors I cannot fathom how to fix.

Code:

   
Private Function GetTextForOutput(ByVal cstrFilePath As String) As String

        Dim sb As New System.Text.StringBuilder()
        Dim sr As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(cstrFilePath)

        If sr.Peek() >= 0 Then
            sb.Append(sr.ReadLine(cstrSearch))
        End If
        sr.Close()

        Return sb.ToString

    End Function



    Private Sub cboCard_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCard.SelectedIndexChanged
        Dim cstrSearch = cboCard.SelectedText.ToString

        If cboCard.SelectedText <> "" Then

            'For Each strFileName As String In IO.Directory.GetFiles(cstrDirectory, "*.txt" And "CAT", IO.SearchOption.AllDirectories)
            'Dim lines As String() = File.ReadAllLines(strFileName)


            'Next

        End If

    End Sub

To explain the existence of the function: I would later like to pull specific lines out of individual text files based on which item the user selected from the ListBox, and display them elswhere. The function is my attempt at not having to write the same bits of code for almost a hundred files. Right now though, the biggest issue I am having is having no clue how to associate the longer items in the cboCard you see there without literally generating a line in the ComboBox event that says:

Code:

Dim strTemp As String = "Midwest Families"
      If cboCard.Item.ToString = strTemp Then
              Dim strDirectory As String = "C:\yadayada\MidCat.txt"
              Dim strText As String = File.ReadAllText(strDirectory)
              ListBox1.Items.Add(strText)
      End If

I want to to avoid this because I'm literally going to have to generate that same line of code 7 times for each of the other file names I have for the other categories.

Hopefully what I said made sense, because honestly I am totally lost at this point and growing increasingly frustrated that I can't wrote a few lines of code that can handle ALL of the interactions between the category files and combo box. I'd really like it where a function or loop or something can take the input from the combo box, compare the longer names the user sees and relate them to the category names, then open that file and display the contents in the listbox.

Any ideas or do I actually have to write individual lines for every category?

Viewing all articles
Browse latest Browse all 27514

Trending Articles