1 Replies - 1318 Views - Last Post: 07 December 2012 - 02:33 PM Rate Topic: -----

#1 CEBII  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-December 12

Reading from a Sequential Access File to separate textboxes

Posted 07 December 2012 - 01:57 PM

I am trying to build a program that will allow the user to select plant from a listbox, causing an associated .txt file to be read, broken down, and its parts copied to various textboxes.

I am getting an error that I cannot figure out. When I select a plant from the list, the MsgBox else event triggers and my fields are not filled in. It doesn't seem to be recognizing the filename because the message box unknown field just starts reading off each letter, one by one, of the file name.

I have tested to see if it can read the file by just dumping all the file contents into one box, instead of splitting it up, and that works, but not at all what I want it to do. I have tried to rewrite using StreamReader instead, but keep coming back to this since so far it makes more sense to me.

I have given an example of data from one of the .txt files after my code.

Any help would be appreciated.

    Private Sub lstPlants_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPlants.SelectedValueChanged

        Dim plant As String

        Dim infofile As String

        lstPlants.Items.Add("Bee Balm")
        lstPlants.Items.Add("Black Eyed Susan")
        lstPlants.Items.Add("Bleeding Heart")
        lstPlants.Items.Add("Garden Phlox")
        lstPlants.Items.Add("Maltese Cross")
        lstPlants.Items.Add("Purple Coneflower")
        lstPlants.Items.Add("Siberian Iris")
        lstPlants.Items.Add("Strawberry Foxglove")
        lstPlants.Items.Add("Sunflower")

        plant = lstPlants.SelectedItem
        'plantImage = lstPlants.SelectedIndex & ".jpg"

        infofile = plant & ".txt"

        'Dim infofile As String = System.IO.Path.Combine(plant & ".txt")

        If My.Computer.FileSystem.FileExists(infofile) Then
            Dim info() As String = System.IO.File.ReadAllLines(infofile)

            For Each line As String In infofile
                If Trim(line) <> "" Then
                    Dim fields() As String = line.Split(":")
                    Select Case UCase(fields(0))
                        Case "COMMON"
                            lblCommonName.Text = fields(1)
                        Case "LATIN"
                            lblLatinName.Text = fields(1)
                        Case "DESCRIPTION"
                            lblDescription.Text = fields(1)
                        Case "COLOR"
                            lblColor.Text = fields(1)
                        Case "HEIGHT"
                            lblHeight.Text = fields(1)
                        Case "WIDTH"
                            lblWidth.Text = fields(1)
                        Case "ZONE"
                            lblZone.Text = fields(1)
                        Case "MOISTURE"
                            lblMoisture.Text = fields(1)
                        Case "LIGHT"
                            lblSun.Text = fields(1)
                        Case "CULTIVATION"
                            lblCultivate.Text = fields(1)
                        Case "PROPAGATION"
                            lblPropagation.Text = fields(1)
                        Case "DISEASE"
                            lblDiseases.Text = fields(1)
                        Case Else
                            MsgBox("Unknown field " & fields(0))
                    End Select
                End If
            Next
        End If

    End Sub




bee balm.txt file data:

COMMON: Bee Balm
LATIN: Monarda didyma
DESCRIPTION: Bee balm is a lovely perennial with bright flowers on sturdy stems that grow from fast creeping runners. The pointed oval leaves give Earl Grey tea its distinctive aroma and flavor.
COLOR: Tight heads of tubular red flowers are surrounded by a whorl of colored leafy bracts (modified leaves).
FLOWERING: Summer
HEIGHT: 2-4 feet
SPREAD: 2-3 feet
TEMP:
ZONE: 4-8
MOISTURE: Evenly moist, humus-rich soil. If plants dry out, the lower foliage will be shed.
LIGHT: Full sun to partial shade.
CULTIVATION: Plants spread quickly; divide every two to three years.
PROPAGATION: Divide in spring or fall. Sow seed indoors or outdoors in spring.
DISEASE: Powdery mildew causes white blotches on the foliage and may cover the entire plant. Thin the stems for good air circulation. Cut affected plants to the ground.
LANDSCAPE: Plant in formal or informal gardens. Bee balm's lovely flowers add brilliant color to the summer garden and are favored by hummingbirds.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a Sequential Access File to separate textboxes

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Reading from a Sequential Access File to separate textboxes

Posted 07 December 2012 - 02:33 PM

Doing it this way (text file, no objects, no properties) is just WAY too hard.

If you would take an OOP approach to this and make a Plant class with various properties you could just serialize/deserialize. At the same time you could decouple the GUI from the data.

You could fill your combobox with instances of the Plant class and when one is selected you can take that object to populate the GUI. No arrays... No hard coded processing dependent on the exact structure of the file so it can evolve over time and upgrades without much maintenance or code breakage.

Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1