Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading and Writing a small CSV File 1

Status
Not open for further replies.

ulchm

Programmer
May 22, 2007
23
0
0
CA
Ok, last one, this is a one liner in php, don't understand why VB has to be so difficult. After googling for 1/2 hour I gave up (told you you were making me lazy) and decided to post here.

I've got a csv file, looks like this

1,40,48,1,10,500
2,40,48,2,10,500
3,40,48,3,10,500
4,40,48,1,10,440
5,40,48,2,10,440
6,40,48,3,10,440


I want to create a label for each line (ie saving and opening with the open / close of the program)

using lbl
.name = 1
.width = 40
.height = 48
.parent = 1
.location(10,500)


Hoepfully that's clear enough for you to understand what I'm looking for. The damn internet at this office is just WAY to slow to be trying to browse MSDN for this. Thanks in advance!
 
I managed to figure out the read, working on write now!
 
The following listings haven't been tested but should do the trick

In order to write:
Code:
dim sb as new system.text.stringbuilder

for each lbl as label in yourPanel.controls
   sb.append(lbl.name)
   sb.append(",")
   sb.append(lbl.width.tostring)
   sb.append(",")
   sb.append(lbl.height.tostring)
   sb.append(",")
   sb.append(lbl.parent.tostring)
   sb.append(",")
   sb.append(lbl.location.x)
   sb.append(",")
   sb.append(lbl.location.y)
   sb.append(vbcrlf)
end for

Using writer As System.IO.StreamWriter = New System.IO.StreamWriter(YourCsvFilePath)
    writer.Write(sb.tostring)
End Using

-----------------------------------
Well, you said you allready figured out how to read but check this out, it might help, it might not:

Code:
        Using reader As System.IO.StreamReader = New System.IO.StreamReader(YourCsvFilePath)
            Dim ar() As String
            Dim lbl As Label
            While Not reader.EndOfStream
                ar = reader.ReadLine.Split(",")
                lbl = New Label
                With lbl
                    .Text = "Some text"
                    .BorderStyle = BorderStyle.FixedSingle
                    .BackColor = Color.Yellow
                    .Name = ar(0)
                    .Width = CInt(ar(1))
                    .Height = CInt(ar(2))
                    .Parent = ar(3) 'Because as string cannot be converted to Control, process this value as it should
                    .Location = New Point(CInt(ar(4)), CInt(ar(5)))
                End With

                AddHandler lbl.MouseDown, AddressOf DraggableLabel_MouseDown
                AddHandler lbl.MouseMove, AddressOf DraggableLabel_MouseMove
                AddHandler lbl.MouseUp, AddressOf DraggableLabel_MouseUp

                YourPanel.Controls.Add(lbl)
            End While

        End Using

Best served chilled!

---
[tt][COLOR=white Black]MASTA[/color][COLOR=Black lightgrey]KILLA[/color][/tt] [pipe]
 
Thanks for the writer code that gave me the syntax I couldn't find yesterday!

As for the reader that's pretty much exactly what I did, only not in the onclick event, I put it in the on_load for the form etc..

Still though very valuable information and yet again, this form impresses!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top