Question:

Having problem reading from file in vb.net 2003?

by  |  earlier

0 LIKES UnLike

I am a novice to vb.net, i have created a form with a command buttons and a text box. the form basically increments the value in a text box by one every time its pressed. what i wanted to do was then to write the value to a file, then when i reopen the file later read the number back to the text box and continue.

I have managed to write to a file and check to see if the correct value is there, which it is. but, i am having problems reading it back to file. Someone did say using the ReadAllLines, but it doesnt work in vb.net.

Here is my program code:

Imports System.IO

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

Friend WithEvents Button2 As System.Windows.Forms.Button

Friend WithEvents Button3 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough(... Private Sub InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button

Me.TextBox1 = New System.Windows.Forms.TextBox

Me.Button2 = New System.Windows.Forms.Button

Me.Button3 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(64, 72)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 0

Me.Button1.Text = "Add Person"

'

'TextBox1

'

Me.TextBox1.Location = New System.Drawing.Point(192, 72)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.TabIndex = 1

Me.TextBox1.Text = ""

'

'Button2

'

Me.Button2.Location = New System.Drawing.Point(176, 224)

Me.Button2.Name = "Button2"

Me.Button2.TabIndex = 2

Me.Button2.Text = "Save"

'

'Button3

'

Me.Button3.Location = New System.Drawing.Point(272, 224)

Me.Button3.Name = "Button3"

Me.Button3.TabIndex = 3

Me.Button3.Text = "Load"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(376, 273)

Me.Controls.Add(Me.Button3)

Me.Controls.Add(Me.Button2)

Me.Controls.Add(Me.TextBox1)

Me.Controls.Add(Me.Button1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Dim value(0) As String

Dim x As Integer

Dim results1 As Integer

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

x = x + 1

TextBox1.Text = x

results1 = TextBox1.Text

End Sub

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

End Sub

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

value(0) = Me.TextBox1.Text

Dim fs As New FileStream("c:\test.txt", FileMode.Create, FileAccess.Write)

Dim sw As New StreamWriter(fs)

sw.WriteLine(value(0))

sw.Close()

End Sub

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

Dim lines() As String = IO.File.readalllines("c:\test.txt")

Me.TextBox1.Text = line(0)

End Sub

End Class

The issue is with regards to button3 which shall retrieve data and place back in text box1.

Can anyone help please.

thanks

 Tags:

   Report

2 ANSWERS


  1. Me.TextBox1.Text = line(0)

    this line of code makes you read the first line ... consider reading the last line

    Me.TextBox1.Text = lines(lines.Length - 1)


  2. Open the text file first. Then read from it. Since you closed the file after you wrote to it, it can&#039;t be written to until you open it again.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.