5 Replies - 499 Views - Last Post: 24 March 2011 - 06:10 PM Rate Topic: -----

#1 crappyprogrammerboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-February 11

i can't get my listbox generate info from txt file, can u correct

Posted 23 March 2011 - 05:38 PM

The problem is that it only adds to the text file it's suppose to read from, but instead of adding 4 strings into my listbox. Can someone figure out what's wrong with my code, and provide me the correct syntax for it. This is in a sub after click of a button in vb.2008.


        'Check to make sure there is a file Commercials.txt, if not one will be created.
        If My.Computer.FileSystem.FileExists("Commercials.txt") Then
            FileContents = My.Computer.FileSystem.ReadAllText("Commercials.txt")
        Else
            'Case if no exiting "commercial.txt file exists, then we create a new one, with default settings.
            'Declaring parallel array in case there is no  "commercials.txt"
            Dim Electees() As String = {"Budweiser", "FedEx", "E*Trades", "Pepsi"}
            Dim AmountVote() As Integer = {0, 0, 0, 0}
            For i As Integer = 0 To 3
                My.Computer.FileSystem.WriteAllText("Commericals.txt", Electees(i).PadRight(15) & Space(5) & AmountVote(i) & ControlChars.NewLine, True)
            Next
        End If
        'Populate the combobox, so i can select a company to save a vote
        'NEED HELP
        FileContents = My.Computer.FileSystem.ReadAllText("Commercials.txt")
        NewLineIndex = FileContents.IndexOf(ControlChars.NewLine, numIndex)
        If (NewLineIndex < numIndex) Then
            temp = FileContents.Substring(numIndex, FileContents.Length - numIndex)
        Else
            temp = FileContents.Substring(numIndex, NewLineIndex - numIndex)
        End If
        For subscript As Integer = 0 To 3
            NewLineIndex = FileContents.IndexOf(ControlChars.NewLine, numIndex)
            CompanyAndVotes = FileContents.Substring(15, NewLineIndex - numIndex)

            items(subscript).Company = CompanyAndVotes.Substring(0, 15)
            items(subscript).Votes = Convert.ToInt32(CompanyAndVotes.Substring(20))

            CompaniesListBox.Items.Add(items(subscript).Company)
            numIndex = NewLineIndex + ControlChars.NewLine.Length
        Next
        CompaniesListBox.SelectedIndex = 0


This post has been edited by SixOfEleven: 23 March 2011 - 05:39 PM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: i can't get my listbox generate info from txt file, can u correct

#2 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: i can't get my listbox generate info from txt file, can u correct

Posted 23 March 2011 - 07:17 PM

Can't figure out what your problem means...

Can you elaborate a bit more?
Was This Post Helpful? 0
  • +
  • -

#3 crappyprogrammerboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-February 11

Re: i can't get my listbox generate info from txt file, can u correct

Posted 24 March 2011 - 02:10 PM

View PostElda, on 23 March 2011 - 07:17 PM, said:

Can't figure out what your problem means...

Can you elaborate a bit more?


Basically, I check if the text file exist, if not, then I create a default text file.
I got to that part, but I can't seem to get it to read properly, or I can't get the 4 strings
which are the company's name to add into listbox. that's basacally it.

I can't figure out why I can't get it to display. The code right there is basically in a subprocess after the button is clicked.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: i can't get my listbox generate info from txt file, can u correct

Posted 24 March 2011 - 02:29 PM

I dont see where you have created a new file! try to debug it to see if it really creates new file expected.
Was This Post Helpful? 0
  • +
  • -

#5 3P1C_F@!l  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 01-March 11

Re: i can't get my listbox generate info from txt file, can u correct

Posted 24 March 2011 - 02:58 PM

You should try using StreamWriter/StreamReader for this instead of My.Computer.FileSystem. Then you can use File.AppendText and it will automatically create the file if it isn't there. You will need to import System.IO to use StreamWriter and StreamReader.
Was This Post Helpful? 1
  • +
  • -

#6 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: i can't get my listbox generate info from txt file, can u correct

Posted 24 March 2011 - 06:10 PM

Look in here
http://forums.devx.com/showthread.php?t=156588
seems related.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1