简体   繁体   English

StreamReader第一行不会显示在文本框中-VB.NET

[英]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. 它编写正确,但是当我想从中读取内容并将其放在Visual Studio中的RichTextBox中时,它不会向我显示第一行,而我对此一无所知。 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. 因此,请评论一下,在循环之前阅读它会很好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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