4 Replies - 514 Views - Last Post: 21 March 2015 - 10:29 AM Rate Topic: -----

#1 Twisted Fang   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 07-October 14

Changing Label Text to Match Items in a Text File

Posted 19 March 2015 - 01:02 PM

Hello. I'm really stuck on a college project. I need to get it done by the end of next week, and wondered if I was missing something really simple.

All I want to be able to do is change the text of some labels to match the names of some items in a .txt file. The .txt file I'm reading has a list of items which all have different properties, which are separated by commas. For example:

item1,Red,Small,Shirt

I'm trying to do this by reading the line(s) from the .txt file, setting each item property as part of an array which is then used to change what the labels say.

To put it in more context, this is an application designed for collectors. Users can create a collection, add items to the collection and change the properties of each item in the collection.

I have a class called 'claItem'. It contains a byte (bytCollectionNum) which changes depending on the index you select in the first form (which simply displays a list of your collection names). The second form displays the items in the collection you chose, and then selecting an item from there will show the item's properties in the third form. Each collection has a file associated with it containing its items and item properties, whereas the collection names are all stored in one file known as "CollectionData.txt". For instance, if I were to select the second collection in the list on the first form, it will load items from the file "Collection1.txt" in the next form (I started from 0).


Here's the code I've been using so far:

Imports System.IO
Public Class frmViewItem
    Dim arrProperties(0) As claItem
    Private Sub frmViewItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each line In File.ReadAllLines("Collection" & claItem.bytCollectionNum & ".txt")
            Dim temp() = line.Split(",")

            arrProperties(arrProperties.Length - 1) = New claItem
            arrProperties(arrProperties.Length - 1).strProp1 = temp(0)
            arrProperties(arrProperties.Length - 1).strProp2 = temp(1)
            arrProperties(arrProperties.Length - 1).strProp3 = temp(2)
            arrProperties(arrProperties.Length - 1).strProp4 = temp(3)
            arrProperties(arrProperties.Length - 1).strProp5 = temp(4)
            arrProperties(arrProperties.Length - 1).strProp6 = temp(5)
            arrProperties(arrProperties.Length - 1).strProp7 = temp(6)
            arrProperties(arrProperties.Length - 1).strProp8 = temp(7)
            arrProperties(arrProperties.Length - 1).strProp9 = temp(8)
            arrProperties(arrProperties.Length - 1).strProp10 = temp(9)

            ReDim Preserve arrProperties(arrProperties.Length)

        Next

        ReDim Preserve arrProperties(arrProperties.Length - 2)

        ChangeLabels()

    End Sub

    Private Sub ChangeLabels()

        Me.Refresh()

        For x = 0 To arrProperties.Length - 1
                lblProperty1.Text = arrProperties(x).strProp1
                lblProperty2.Text = arrProperties(x).strProp2
                lblProperty3.Text = arrProperties(x).strProp3
                lblProperty4.Text = arrProperties(x).strProp4
                lblProperty5.Text = arrProperties(x).strProp5
                lblProperty6.Text = arrProperties(x).strProp6
                lblProperty7.Text = arrProperties(x).strProp7
                lblProperty8.Text = arrProperties(x).strProp8
                lblProperty9.Text = arrProperties(x).strProp9
                lblProperty10.Text = arrProperties(x).strProp10
        Next
    End Sub
End Class



I've used code similar to this for a previous project, and it worked fine, except it was adding items to a ListBox rather than updating label text. I can't remember why I needed the 'ReDim Preserve' though.

I hope I explained this well enough.

Is This A Good Question/Topic? 0
  • +

Replies To: Changing Label Text to Match Items in a Text File

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Changing Label Text to Match Items in a Text File

Posted 19 March 2015 - 01:13 PM

the length does not grow from each item added into an array. right now you are just over writing the same element each time

You dimensioned it to 0 so your array length is 1

everytime you do arrProperties.Length -1 you are just stating arrayproperties(0).strProp# = temp(#)

I would think you would dimension your array to the length of the temp array to ensure that you can fill each element. Then you should be able to just use the same element for both arrProperties() and temp() since they seem to not be based off of different sized arrays
Was This Post Helpful? 0
  • +
  • -

#3 Twisted Fang   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 07-October 14

Re: Changing Label Text to Match Items in a Text File

Posted 19 March 2015 - 02:14 PM

I changed it to
Dim arrProperties(9) As claItem
but it made no difference. If it helps, he code I used for my previous project was this:

Imports System.IO

Public Class Products
    Dim arrProducts(0) As claProduct
    Private Sub Products_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For Each line In File.ReadAllLines("ProductData.txt")
            Dim temp() = line.Split(",")

            arrProducts(arrProducts.Length - 1) = New claProduct
            arrProducts(arrProducts.Length - 1).bytProID = temp(0)
            arrProducts(arrProducts.Length - 1).strProName = temp(1)
            arrProducts(arrProducts.Length - 1).decPrice = temp(2)
            arrProducts(arrProducts.Length - 1).intQtyInStock = temp(3)
            arrProducts(arrProducts.Length - 1).strNotes = temp(4)
            arrProducts(arrProducts.Length - 1).strType = temp(5)

            ReDim Preserve arrProducts(arrProducts.Length)

        Next

        ReDim Preserve arrProducts(arrProducts.Length - 2)

    End Sub

    Private Sub cmbCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCategory.SelectedIndexChanged

        lstProducts.Items.Clear()

       
        For x = 0 To arrProducts.Length - 1
            If cmbCategory.Text = arrProducts(x).strType Then
                lstProducts.Items.Add(arrProducts(x).bytProID)
                lstProducts.Items.Add(arrProducts(x).strProName)
                lstProducts.Items.Add(arrProducts(x).decPrice)
                lstProducts.Items.Add(arrProducts(x).intQtyInStock)
                lstProducts.Items.Add(arrProducts(x).strNotes)
                lstProducts.Items.Add(arrProducts(x).strType)
                lstProducts.Items.Add("")
            End If
        Next

    End Sub
End Class



It works perfectly, so I just don't understand what I'm doing wrong.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Changing Label Text to Match Items in a Text File

Posted 19 March 2015 - 02:21 PM

Again you're passing the same information into the element index each time arrProducts.Length -1 is always the same value through out those 9 or 10 lines of code. Regardless of what you dimension it as.

So you dimensioned it to 9 which means length is 10 (0-9) so 10 -1 = 9 so you're always using arrProducts(9) = in every line of that code.

Put a breakpoint on line 34 and when the execution halts. Hover over the various variables and look at their value. You can even open the array to see its contents.
Was This Post Helpful? 0
  • +
  • -

#5 Twisted Fang   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 07-October 14

Re: Changing Label Text to Match Items in a Text File

Posted 21 March 2015 - 10:29 AM

Inserted a breakpoint, nothing happened. (Maybe I did it wrong although I have used breakpoints before)

I think I understand what you're saying, but then why did it work before? Is it because you HAVE to put label text in quote marks ("")? Seems to work fine with a ListBox, and I've been using the same method across the rest of my program as well, just with ListBoxes and not Labels.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1