GODDY

I have used this code to creat a sequential text file called MEMBERS.TXT. i want to get the information from the file and put it in an array but i dont know how to do that ....please help me.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim objStreamWriter As System.IO.StreamWriter

objStreamWriter = System.IO.File.CreateText("MEMBERS.TXT")

objStreamWriter.Close()

End Sub

Private Sub AddNewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewButton.Click

Dim strName As String, intNumber As Integer

Dim objStreamWriter As System.IO.StreamWriter

strName = Me.NameTextBox.Text

intNumber = Me.NumberTextBox.Text

objStreamWriter = System.IO.File.AppendText("MEMBERS.TXT")

objStreamWriter.WriteLine(strName.PadRight(30) & intNumber.ToString.PadRight(34))

objStreamWriter.Close()

Me.NameTextBox.Text = ""

Me.NumberTextBox.Text = ""

Me.NameTextBox.Focus()

End Sub




Re: Visual Basic General USING ARRAYS!!!

Spidermans_DarkSide - VSIP

Hi,

See this also on the StreamReader>>

http://msdn2.microsoft.com/en-us/library/system.io.streamreader.readline.aspx

If use use IMPORTS SYSTEM.IO instead at the top of your code widow then you can use.>>

StreamReader and StreamWriter like this instead.>>

Code Snippet

Dim path1 As String ="C:\" 'or some other path.

Dim path2 As String ="C:\" 'or some other path.

Dim fileName1 ="myInputFile.txt" 'or whatever.

Dim fileName2 = "myOutpuFile.txt" 'or whatever.

Dim sr As New StreamReader(path1 & fileName1)

Dim sw As New StreamReader (path2 & filename2)

Dim line As Integer = -1

Dim lines() As String

While sr.Readline <> Nothing

line = line+1

lines(line)=sr.ReadLine()

End While

sw.close()

sr.close()

Code Snippet

' To read it from the array use the Ubound function on the array.>>

Dim line As Integer

For line=0 to Ubound(lines)

RichTextBox1.AppendText(lines(line) & vbCrLf)

Next

That should work ( off the top of my head as i've just had to re-install WINDOWS on this PC for 'technical' reasons ).

Regards,

S_DS






Re: Visual Basic General USING ARRAYS!!!

LeonCS

It is also well worth knowing about the ReadAllBytes and the WriteAllBytes methods, which quickly dump file data into an array for you. (It's great for large files)

In the following example, you will have to copy your "MEMBERS.TXT" to your project"\bin\debug" folder

Dim myByteArray() As Byte 'a dynamic array

Dim myAppPath As String

myAppPath = My.Application.Info.DirectoryPath

'to load

myByteArray = My.Computer.FileSystem.ReadAllBytes(myAppPath + "\MEMBERS.TXT")

'to save

My.Computer.FileSystem.WriteAllBytes(myAppPath + "\MEMBERS.TXT", myByteArray, False)

To "append", you change the last field to True






Re: Visual Basic General USING ARRAYS!!!

Spidermans_DarkSide - VSIP

LeonCS wrote:

It is also well worth knowing about the ReadAllBytes and the WriteAllBytes methods, which quickly dump file data into an array for you. (It's great for large files)

In the following example, you will have to copy your "MEMBERS.TXT" to your project"\bin\debug" folder

Dim myByteArray() As Byte 'a dynamic array

Dim myAppPath As String

myAppPath = My.Application.Info.DirectoryPath

'to load

myByteArray = My.Computer.FileSystem.ReadAllBytes(myAppPath + "\MEMBERS.TXT")

'to save

My.Computer.FileSystem.WriteAllBytes(myAppPath + "\MEMBERS.TXT", myByteArray, False)

To "append", you change the last field to True

Hi,

Short note My. namespace isn't available in Framework 1.0 or 1.1 for anyone still using Visual Studio 2003.

Regards,

S_DS






Re: Visual Basic General USING ARRAYS!!!

GODDY

Hi LeonCS i used the code in the form load but when i use

myByteArray = My.Computer.FileSystem.ReadAllBytes(myAppPath + "\MEMBERS.TXT")

to load the array, they tell me "\MEMBERS.TXT") is not declared. I'm i doing something wrong






Re: Visual Basic General USING ARRAYS!!!

LeonCS

Hi GODDY

Yes you are doing something wrong and it's my fault. It appears as though you are getting your methodologies mixed up. My post on read/writeallbytes has probably led you astray, because I confused the issue by including your "MEMBERS.TXT" in my example illistration. I apologize for that, because really, your original posted question is in the setting of a streamreader/writer scenario - not a read/writeallbytes scenario, and you would have been best following Spiderman's trace on this subject.

If you really know what your doing you can mix up your methodology, but I am going to keep it simple and just send a simple example of the read/writeallbytes capability. I repeat though, you are probably best pursuing the streamreader/writer approach.

Put three buttons, a textbox, and a label on a form

When you first run this example, exit it and reposition the controls on the form so they all "sit pretty".

Public Class Form1

Dim myByteArray() As Byte 'a dynamic array

Dim myAppPath As String

Dim I%

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Button1.Text = "Load"

Button2.Text = "Save"

Button3.Text = "Clear"

TextBox1.Multiline = True

TextBox1.Size = New System.Drawing.Size(200, 300)

myAppPath = My.Application.Info.DirectoryPath

Label1.Text = myAppPath 'shows where your text file will be created

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

'to load

If My.Computer.FileSystem.FileExists(myAppPath + "\TEST1.TXT") Then

GoTo ues

Else

MsgBox("Cannot load as {TEST1.TXT} does not yet exist.") : Exit Sub

End If

ues:

myByteArray = My.Computer.FileSystem.ReadAllBytes(myAppPath + "\TEST1.TXT")

TextBox1.Clear()

For I% = 0 To UBound(myByteArray)

TextBox1.Text = TextBox1.Text + Chr(myByteArray(I%))

Next I%

ReDim myByteArray(0)

End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

'to save

ReDim myByteArray(Len(TextBox1.Text) - 1)

For I% = 1 To Len(TextBox1.Text)

myByteArray(I% - 1) = Asc(Microsoft.VisualBasic.Mid(TextBox1.Text, I%, 1))

Next I%

My.Computer.FileSystem.WriteAllBytes(myAppPath + "\TEST1.TXT", myByteArray, False)

ReDim myByteArray(0)

MsgBox("Your {TEST1.TXT} has now been saved.")

End Sub

Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

TextBox1.Clear()

End Sub

End Class






Re: Visual Basic General USING ARRAYS!!!

GODDY

Hi LeonSC

My.Computer.FileSystem.WriteAllBytes(myAppPath + "\TEST1.TXT", myByteArray, False)

when i use this in the button-_click event, they tell me that "My" is not declared.






Re: Visual Basic General USING ARRAYS!!!

LeonCS

Spidermans_DarkSide - VSIP wrote:

Short note My. namespace isn't available in Framework 1.0 or 1.1 for anyone still using Visual Studio 2003.

Shame about that. Can't you get an upgrade for free, or at a low cost






Re: Visual Basic General USING ARRAYS!!!

spotty

Yes you can download VB Express for free, which is VB.NET 2005 and will work for most windows application development.





Re: Visual Basic General USING ARRAYS!!!

GODDY

How do i creat an array then add the (MEMBERS.TXT) file

Public Class Form1

Structure Membersstruc

Public Name As String

Public number As Integer

End Structure

Dim objStreamWriter As System.IO.StreamWriter

Dim objStreamReader As System.IO.StreamReader

Dim strLine As String

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim intcount, intButton as integer, udtMembers as Membersstruc

intbutton = messagebox.show("Do you want to initialize the file")

If intbutton = system.windows.DialogResult.No Then

messagebox.show("File was not initialized")

Else

udtmembers.name = space (15)

udtmembers.number = space(18)

ojStreamWriter = System.IO.File.CreateText("MEMBERS.TXT")

objStreamWriter.Close()

End Sub

Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click

If System.IO.File.Exists("MEMBERS.TXT") Then

objStreamWriter = System.IO.File.AppendText("MEMBERS.TXT")

objStreamWriter.WriteLine(NameTextBox.Text)

objStreamWriter.Write(NumberTextBox.Text.PadLeft (30))

TextBox1.Text = ""

textbox2,text = ""

TextBox1.Focus()

objStreamWriter.Close()

Else

MessageBox.Show("File does not exist")

End If

End Sub

Private Sub LoadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoadButton.Click

If System.IO.File.Exists("MEMBERS.TXT") = True Then

objStreamReader = System.IO.File.OpenText("MEMBERS.TXT")

Do Until objStreamReader.Peek = -1

strLine = objStreamReader.ReadLine()

Loop

objStreamReader.Close()

Else

MessageBox.Show("The file MEMBERS.TXT does not exist")

End If

End Sub

Private Sub ClearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ClearButton.Click

TextBox1.text = ""

Textbox2.text = "'

End Sub

End Class






Re: Visual Basic General USING ARRAYS!!!

LeonCS

GODDY wrote:

(I want to put the file ("MEMBERS.TXT") in an array then load it into a listbox.)

Hi GODDY

Wish I could help, but the streamreader method is not part of my vocab

You may be best starting a new thread on this one, as this thread has the green tick, and people may not realise you still need help.

PS. Had a good look at your code, and noticed quite a few problems.

1) You have referenced buttons1-3 in your load event, but click events are of different buttons altogether (names are different).

2) Maybe it's just me, but I can't seem to find any reference to the actual location of your {MEMBERS.TXT} - like "C:\myproject\" or whatever.

3)In your code, I see you have reference to a {textbox}, but not sure how you have used it, and then you say you want to load a {listbox}.

Wish you Godspeed on this one..!