简体   繁体   中英

StreamReader first line won't show in textbox - VB.NET

I have those functions that write to and read from a file. It writes correctly, but when I want to read from it and put it in a RichTextBox in Visual Studio, it won't show me the first line and I'm totaly lost on this one. I'm a bit in a rush.

Functions Read/Save

Imports System.IO

Module ModuleFichier
Public Sub Save(ByVal Question As String, ByVal RepA As String, ByVal RepB   As String, ByVal RepC As String, ByVal Rep As String)
    If Not File.Exists("B:\Questions.txt") Then
        File.Create("B:\Questions.txt").Dispose()
    End If
    Using fichier As StreamWriter = File.AppendText("B:\Questions.txt")
        fichier.WriteLine(Question)
        fichier.WriteLine(RepA)
        fichier.WriteLine(RepB)
        fichier.WriteLine(RepC)
        fichier.WriteLine(Rep)
    End Using
End Sub
Public Function Read(txtbox As String) As String
    If Not File.Exists("B:\Questions.txt") Then
        txtbox = "Fichier vide"
        Return txtbox
        Exit Function
    End If
    Using fichier As New StreamReader("B:\Questions.txt")
        Dim line As String
        line = fichier.ReadLine()
        Do
            line = fichier.ReadLine()
            txtbox += vbNewLine & line
        Loop Until line Is Nothing
        Return txtbox
    End Using
End Function
End Module

Function called

Private Sub butQuestionsQ_Click(sender As Object, e As EventArgs) Handles butQuestionsQ.Click
    txtQuestionsQ.Text = Read(txtQuestionsQ.Text)
End Sub

This is part of a school homework so I'm pretty sure of what happens in there which is why I come here. Might be a newby mistake, but I can't seem to find it, also sorry for the french in there.

You overwrite the first read line.

    line = fichier.ReadLine() // line to remove
    Do
        line = fichier.ReadLine()

You just need to remove the commented line.

Public Function Read(txtbox As String) As String
If Not File.Exists("B:\Questions.txt") Then
    txtbox = "Fichier vide"
    Return txtbox
    Exit Function
End If
Using fichier As New StreamReader("B:\Questions.txt")
    Dim line As String
    //line = fichier.ReadLine()
    Do
        line = fichier.ReadLine()
        txtbox += vbNewLine & line
    Loop Until line Is Nothing
    Return txtbox
End Using

End Function`

See the line I commented. You are reading one line before going to loop. So it will read first line. Again you are reading another line as soon as you goo into loop. So it will get second line now and replace line variable with second line data. So comment that reading before loop and it will work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM