5 Replies - 435 Views - Last Post: 14 February 2013 - 04:26 PM Rate Topic: -----

#1 BFox549  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 13

Need Help! VB Noob. Placing .Txt lines in ListBox

Posted 12 February 2013 - 09:13 PM

Hey guys, I was wondering if there were any coders that are members on this site. I'm in an introductory Visual Basic class and there is a part of my lab that is confusing me.

Here is the part of the lab that's confusing:
"ReadData button: First it should clear ListBox1. Then it should open a file named data.txt which you can assume has 3 lines in it. Each line is assume to have a name, a space, and then a number on it. Place just the names in the ListBox in backwards order and place the sum of the 3 numbers from the file into the textbox."

I made the data.txt file. I just dont know how to go about placing just the names in the listbox, let alone backwards, and then placing the sum in there too.

Then I have another button to do:
"ReadDataFormat: This does the mostly the same work as the ReadData button, but its output is formatted. First it should clear ListBox1. Then it should open a file named data.txt which you can assume has 3 lines in it. Each line is assume to have a item name, a space, and then a number on it. Place into ListBox1 3 lines each of the form
"itemname" costs "amount"
where itemname and amount are values from the text file. You are required to use a format string and the String.Format function. item names can be as big as 15 characters and amounts are fractional numbers that could be as large as 9999.99. The numbers should be formatted as currency."

So yea... help lol

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help! VB Noob. Placing .Txt lines in ListBox

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8393
  • View blog
  • Posts: 31,213
  • Joined: 12-June 08

Re: Need Help! VB Noob. Placing .Txt lines in ListBox

Posted 12 February 2013 - 09:30 PM

Quote

Hey guys, I was wondering if there were any coders that are members on this site.

Naw.. no coders here.

Quote

I made the data.txt file. I just dont know how to go about placing just the names in the listbox, let alone backwards, and then placing the sum in there too.

Investigate streamreader. You can sequentially read in strings from a file... then, perhaps, as you read them put them into a string collection... and then use a for loop to read that collection from back to front and insert into the listbox.. or go further and make the string collection be the datasource for the listbox.

http://msdn.microsof...reamreader.aspx


Quote

Then I have another button to do:
"ReadDataFormat: This does the mostly the same work as the ReadData button, but its output is formatted. First it should clear ListBox1. Then it should open a file named data.txt which you can assume has 3 lines in it. Each line is assume to have a item name, a space, and then a number on it. Place into ListBox1 3 lines each of the form
"itemname" costs "amount"
where itemname and amount are values from the text file. You are required to use a format string and the String.Format function. item names can be as big as 15 characters and amounts are fractional numbers that could be as large as 9999.99. The numbers should be formatted as currency."

So yea... help lol

"So yeah" doesn't quite explain what you have done, tried, or are lost on.

Oh.. I guess I should ask - is this VB6 or VB.NET? I sort of guessed it was VB.NET and answered as such.. though.. who knows.. it might not be.
Was This Post Helpful? 0
  • +
  • -

#3 BFox549  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 13

Re: Need Help! VB Noob. Placing .Txt lines in ListBox

Posted 12 February 2013 - 09:48 PM

sorry about the vague question, I simply just copied and pasted from a different site.

here is what I have so far:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListBox1.Items.Clear()
        Dim datareader As IO.StreamReader
        datareader = IO.File.OpenText("data.txt")
        Dim line As String
        line = datareader.ReadLine()
        ListBox1.Items.Add(line)
        line = datareader.ReadLine()
        ListBox1.Items.Add(line)
        line = datareader.ReadLine()
        ListBox1.Items.Add(line)

        textbox1.text = 


Right now the ReadData button just puts what's in the data.txt file into the listbox. i dont know how to put just the names in the listbox. and then I have to put the sum of the numbers in the textbox, which I also do not know how to do.

This post has been edited by modi123_1: 12 February 2013 - 09:49 PM
Reason for edit:: please use code tags

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8393
  • View blog
  • Posts: 31,213
  • Joined: 12-June 08

Re: Need Help! VB Noob. Placing .Txt lines in ListBox

Posted 12 February 2013 - 09:56 PM

Start by putting a break point where you are reading a line.. see how that is a string? You can use string functions like 'substring' or even 'split' to take a given string and break it into parts. Then you can consistently pick the part you need.

http://msdn.microsof...tem.string.aspx
        '-- my string.
        Dim foo As String = "abc 123 456d"
        '-- a string array.
        Dim bar() As String
        '-- my string array catching foo being split apart into multiple sub strings when a space is found.
        bar = foo.Split(" ")

        '-- me printing each sub string.
        For Each i As String In bar
            Console.WriteLine(i)
        Next

        '-- me printing the substring at index 1
        Console.WriteLine(bar(1))

Was This Post Helpful? 0
  • +
  • -

#5 BFox549  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 13

Re: Need Help! VB Noob. Placing .Txt lines in ListBox

Posted 12 February 2013 - 09:58 PM

thanks for the help modi
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,979
  • Joined: 19-May 09

Re: Need Help! VB Noob. Placing .Txt lines in ListBox

Posted 14 February 2013 - 04:26 PM

Look at split first, since your prof is obviously letting you know that there are no spaces in the names.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1