I have a text file like this;

Bananas=60
Tomatoes=34
Rasberries=102
Apples=23

If I have a form with textboxes or numericalupdowns is there anyway I could change the number of fruit and veg in the text file?

Could it be possible to somehow open the textfile into the form then, edit & save?

Thanks

Recommended Answers

All 7 Replies

bump

What have you done so far? Please post your code here and someone will help you. Do wrap your programming code blocks within [code] .. [/code] tags.

I have no code at the moment.

All i have is abunch of textboxes for the numbers of fruits.

Member Avatar for Unhnd_Exception
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click

        'ToDo: Perform Error Checking
        Dim fStream As New System.IO.FileStream("C:\Fruit.txt", IO.FileMode.Open)
        Dim sReader As New System.IO.StreamReader(fStream)
        Dim line() As String

        Do While sReader.Peek > -1
            line = sReader.ReadLine.Split(New Char() {"="c}, StringSplitOptions.RemoveEmptyEntries)
            If line.Count <> 2 Then Continue Do 'wrong format
            If Not IsNumeric(line(1)) Then Continue Do 'wrong format

            Select Case line(0).ToLower
                Case "bananas"
                    txtBananas.Text = line(1)
                Case "apples"
                    txtApples.Text = line(1)
                Case "rasberries"
                    txtRasberries.Text = line(1)
                Case "tomatoes"
                    txtTomatoes.Text = line(1)
                Case Else
                    'Bad Fruit
            End Select
        Loop

        fStream.Close()
        sReader.Dispose()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        'ToDo: Perform Error Checking Andalso Validate TextBoxes
        Dim fStream As New System.IO.FileStream("C:\Fruit.txt", IO.FileMode.OpenOrCreate)
        Dim sWriter As New System.IO.StreamWriter(fStream)

        sWriter.WriteLine("Bananas=" & txtBananas.Text)
        sWriter.WriteLine("Apples=" & txtApples.Text)
        sWriter.WriteLine("Rasberries=" & txtRasberries.Text)
        sWriter.WriteLine("Tomatoes=" & txtTomatoes.Text)

        sWriter.Close()
        fStream.Close()

        sWriter.Dispose()

    End Sub

Awesome thanks dude :D

Ive got small problem.

It wont open the values from the text file when I have it like this

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'ToDo: Perform Error Checking
        Dim fStream As New System.IO.FileStream("c:\Users\Ollie\Desktop\test.txt", IO.FileMode.Open)
        Dim sReader As New System.IO.StreamReader(fStream)
        Dim line() As String

        Do While sReader.Peek > -1
            line = sReader.ReadLine.Split(New Char() {"="c}, StringSplitOptions.RemoveEmptyEntries)
            If line.Count <> 2 Then Continue Do 'wrong format
            If Not IsNumeric(line(1)) Then Continue Do 'wrong format

            Select Case line(0).ToLower
                Case "A"
                    A.Text = line(1)
                Case "B"
                    B.Text = line(1)
                Case "C"
                    C.Text = line(1)
                Case Else
                    'Bad Fruit
            End Select
        Loop

        fStream.Close()
        sReader.Dispose()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        'ToDo: Perform Error Checking Andalso Validate TextBoxes
        Dim fStream As New System.IO.FileStream("c:\Users\Ollie\Desktop\test.txt", IO.FileMode.OpenOrCreate)
        Dim sWriter As New System.IO.StreamWriter(fStream)

        sWriter.WriteLine("A" & " " & A.Text)
        sWriter.WriteLine("B" & " " & B.Text)
        sWriter.WriteLine("C" & " " & C.Text)


        sWriter.Close()
        fStream.Close()

        sWriter.Dispose()

    End Sub
   
End Class

Solved - PMed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.