none
For... Next RRS feed

  • Question

  • Hi All,

    I have simple question,just use For ...Next statement.

    When button_2 click ,textbox3 show less one of line text file.

    '7999,1910,1034,4059,5484,5245,6819,9779,8627,2770,3137,6276,2961,3121,4222,0070,0354,3838,4252,0074,2531,4250,6624
            '9978,3281,8154,2419,2939,8250,8799,0782,7819,9785,6805,2768,7975,6829,0321,1290,5361,4306,2191,5173,8405,6100,4054
            '7570,4120,0142,1082,8594,9136,9047,4110,3316,6670,7564,8331,0927,9420,8801,0791,2517,6501,1530,6370,1026,0902,5323

    When button_3 click ,textbox4 show less two of line text file.

    '7999,1910,1034,4059,5484,5245,6819,9779,8627,2770,3137,6276,2961,3121,4222,0070,0354,3838,4252,0074,2531,4250,6624
            '9978,3281,8154,2419,2939,8250,8799,0782,7819,9785,6805,2768,7975,6829,0321,1290,5361,4306,2191,5173,8405,6100,4054

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
        Private Data As New List(Of String)
        Private Data_ As New List(Of String)
    
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Text File is;
            '7999,1910,1034,4059,5484,5245,6819,9779,8627,2770,3137,6276,2961,3121,4222,0070,0354,3838,4252,0074,2531,4250,6624
            '9978,3281,8154,2419,2939,8250,8799,0782,7819,9785,6805,2768,7975,6829,0321,1290,5361,4306,2191,5173,8405,6100,4054
            '7570,4120,0142,1082,8594,9136,9047,4110,3316,6670,7564,8331,0927,9420,8801,0791,2517,6501,1530,6370,1026,0902,5323
            '4049,4193,2575,3760,1787,4119,6859,8327,8576,0351,6751,1146,8280,6521,6438,0167,7091,6205,3289,0363,3818,0431,3940
    
            Dim sr As New System.IO.StreamReader("C:\Users\family\Documents\B.txt")
    
            Do While sr.Peek <> -1
                Dim StringToCheck As String = sr.ReadToEnd
                TextBox1.AppendText(StringToCheck & vbCrLf)
                Data.Add(StringToCheck)
            Loop
    
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            For i As Integer = 0 To Data.Count - 1
                TextBox2.AppendText(Data(i) & vbCrLf)
            Next
    
        End Sub
    
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            For i As Integer = 0 To Data.Count - 2
                TextBox3.AppendText(Data(i) & vbCrLf)
            Next
    
        End Sub
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            For i As Integer = 0 To Data.Count - 3
                TextBox4.AppendText(Data(i) & vbCrLf)
            Next
        End Sub
    
    End Class
    

    Sunday, July 8, 2018 6:17 AM

Answers

  • It looks like you aren't loading the file line-by-line in the first place... you Data collection will only contain one entry.

    Try using:

    Data.AddRange(IO.File.ReadAllLines("C:\Users\family\Documents\B.txt"))

    Instead of the Do While sr.Peek <> -1 loop.  You don't need that loop to simply read all of the lines from a text file.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by mipakteh Sunday, July 8, 2018 12:51 PM
    Sunday, July 8, 2018 12:14 PM
    Moderator

All replies

  • It looks like you aren't loading the file line-by-line in the first place... you Data collection will only contain one entry.

    Try using:

    Data.AddRange(IO.File.ReadAllLines("C:\Users\family\Documents\B.txt"))

    Instead of the Do While sr.Peek <> -1 loop.  You don't need that loop to simply read all of the lines from a text file.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by mipakteh Sunday, July 8, 2018 12:51 PM
    Sunday, July 8, 2018 12:14 PM
    Moderator
  • Thank Reed Kimble.It working.
    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
        Private Data As New List(Of String)
        Private Data_ As New List(Of String)
    
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Text File is;
            '7999,1910,1034,4059,5484,5245,6819,9779,8627,2770,3137,6276,2961,3121,4222,0070,0354,3838,4252,0074,2531,4250,6624
            '9978,3281,8154,2419,2939,8250,8799,0782,7819,9785,6805,2768,7975,6829,0321,1290,5361,4306,2191,5173,8405,6100,4054
            '7570,4120,0142,1082,8594,9136,9047,4110,3316,6670,7564,8331,0927,9420,8801,0791,2517,6501,1530,6370,1026,0902,5323
            '4049,4193,2575,3760,1787,4119,6859,8327,8576,0351,6751,1146,8280,6521,6438,0167,7091,6205,3289,0363,3818,0431,3940
    
            Data.AddRange(IO.File.ReadAllLines("C:\Users\family\Documents\B.txt"))
    
            For I As Integer = 0 To Data.Count - 1
                TextBox1.AppendText(Data(I) & vbCrLf)
            Next
    
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            For i As Integer = 0 To Data.Count - 1
                TextBox2.Clear()
                Data.Remove(CStr(i))
                TextBox2.AppendText(Data(i) & vbCrLf)
            Next
    
        End Sub
    
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            TextBox3.Clear()
            For i As Integer = 0 To Data.Count - 2
                TextBox3.AppendText(Data(i) & vbCrLf)
            Next
        End Sub
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            For i As Integer = 0 To Data.Count - 3
                TextBox4.AppendText(Data(i) & vbCrLf)
            Next
        End Sub
    
    End Class
    

    Sunday, July 8, 2018 12:51 PM