Results 1 to 25 of 25

Thread: VB.NET read text file data between two dates

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    VB.NET read text file data between two dates

    Hi All.. how can I read data from a text file between two dates? Right now I am just going back X-days from today with this from a comma delimited text file:

    Code:
    Dim lines = IO.File.ReadAllLines(filename)
    For Each line In lines
        Dim Data = line.Split(","c)
        If (CDate(Data(4)) > DateTime.Now.AddDays(-CInt(HowMnyDays))) Then
    Thanks in advance.

    Example Data:
    Code:
    8-879003-02_BTM,B1022,741,55,4/18/2022 6:33:49 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,857,59,4/18/2022 6:33:59 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,1024,55,4/18/2022 6:34:02 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,284,669,4/18/2022 6:34:13 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,544,673,4/18/2022 6:34:17 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,724,677,4/18/2022 6:34:21 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,1000,667,4/18/2022 6:34:26 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,1039,672,4/18/2022 6:34:29 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_TOP,B1023,930,175,4/18/2022 6:35:12 AM,LiTran,No Solder,Color [LightSkyBlue],A2523217,
    8-879003-02_TOP,B1023,845,232,4/18/2022 6:37:24 AM,LiTran,Missing Part,Color [Yellow],A2523217,
    8-879003-02_TOP,B1023,489,375,4/18/2022 6:51:39 AM,LiTran,Missing Part,Color [Yellow],A2523217,
    4-946020-12_BTM,-5345,66,265,4/18/2022 9:27:38 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5345,510,231,4/18/2022 9:28:24 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5345,485,324,4/18/2022 9:29:00 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5345,567,136,4/18/2022 9:29:36 AM,thvan,Solder Bridge,Color [Chartreuse],A2524939,
    4-946020-12_BTM,-5345,593,469,4/18/2022 9:30:05 AM,thvan,Solder Bridge,Color [Chartreuse],A2524939,
    4-946020-12_BTM,-5346,65,268,4/18/2022 9:32:03 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5346,509,234,4/18/2022 9:32:56 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5346,493,311,4/18/2022 9:33:48 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5346,567,460,4/18/2022 9:35:50 AM,thvan,Solder Bridge,Color [Chartreuse],A2524939,
    4-946020-12_BTM,-5347,70,275,4/18/2022 9:37:06 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5347,513,235,4/18/2022 9:37:55 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    Last edited by mikeg71; Apr 19th, 2022 at 09:06 AM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    Maybe you could show us some example data. Will the data be sequential by date? If so, in ascending or descending order?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    Thanks for the reply jmcilhinney, I have edited my post. All data is in date order descending.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    This code works for me:
    vb.net Code:
    1. Private Sub ReadLinesByDateRange(startDate As Date, endDate As Date)
    2.     Dim filePath = Path.Combine(Environment.CurrentDirectory, "TextFile1.txt")
    3.     Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/dd/yyyy h:mm:ss tt", Nothing)
    4.     Dim lines = File.ReadLines(filePath).
    5.                      SkipWhile(Function(line) getDateTime(line) < startDate).
    6.                      TakeWhile(Function(line) getDateTime(line) < endDate)
    7.  
    8.     For Each line In lines
    9.         Console.WriteLine(line)
    10.     Next
    11. End Sub
    Note that, in that code, startDate is inclusive and endDate is exclusive. In my testing, I used this data:
    Code:
    8-879003-02_BTM,B1022,741,55,4/18/2022 6:33:49 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,857,59,4/18/2022 6:33:59 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,1024,55,4/19/2022 6:34:02 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,284,669,4/19/2022 6:34:13 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,544,673,4/20/2022 6:34:17 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,724,677,4/20/2022 6:34:21 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,1000,667,4/21/2022 6:34:26 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,1039,672,4/21/2022 6:34:29 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_TOP,B1023,930,175,4/22/2022 6:35:12 AM,LiTran,No Solder,Color [LightSkyBlue],A2523217,
    8-879003-02_TOP,B1023,845,232,4/22/2022 6:37:24 AM,LiTran,Missing Part,Color [Yellow],A2523217,
    8-879003-02_TOP,B1023,489,375,4/23/2022 6:51:39 AM,LiTran,Missing Part,Color [Yellow],A2523217,
    4-946020-12_BTM,-5345,66,265,4/23/2022 9:27:38 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5345,510,231,4/24/2022 9:28:24 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    4-946020-12_BTM,-5345,485,324,4/24/2022 9:29:00 AM,thvan,Insuff Solder,Color [Orange],A2524939,
    and called the method like this:
    vb.net Code:
    1. ReadLinesByDateRange(#4/20/2022#, #4/23/2022#)
    and got this output:
    Code:
    8-879003-02_BTM,B1022,544,673,4/20/2022 6:34:17 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,724,677,4/20/2022 6:34:21 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,1000,667,4/21/2022 6:34:26 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_BTM,B1022,1039,672,4/21/2022 6:34:29 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
    8-879003-02_TOP,B1023,930,175,4/22/2022 6:35:12 AM,LiTran,No Solder,Color [LightSkyBlue],A2523217,
    8-879003-02_TOP,B1023,845,232,4/22/2022 6:37:24 AM,LiTran,Missing Part,Color [Yellow],A2523217,
    so records on or after the startDate and before the endDate.

    Note that my code calls ReadLines rather than ReadAllLines. The latter will read every line first and return them in an array, which you could then process. The former will basically return each line for processing before reading the next. One advantage of that is that you can stop reading any time that you know that you don't need any more data. That's a boon for big files in particular, especially when the data you need is near the beginning.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    Thanks for your help on this.. I am going to try it out now. Would this be something that I could easily use with a date/time picker?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    Quote Originally Posted by mikeg71 View Post
    Would this be something that I could easily use with a date/time picker?
    Of course. The method takes two Date values as arguments. Where you get those Dates from is completely up to you, e.g. the Value properties of two DateTimePicker controls.

  7. #7
    Member
    Join Date
    Apr 2022
    Posts
    62

    Re: VB.NET read text file data between two dates

    I think your looking for concatenate ex:
    Code:
    Dim words = "hello I'm text"
    Dim endwords = "ending text"
    Dim datenow = Date.Now()
    Dim spacetoconcat
    
    Spacetoconcat = words + datenow.tostring() + endwords
    'theres spacer methods in the vb
    May be & to

  8. #8
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,970

    Re: VB.NET read text file data between two dates

    Quote Originally Posted by mmx88_ValidUser View Post
    I think your looking for concatenate ex:
    Code:
    Dim words = "hello I'm text"
    Dim endwords = "ending text"
    Dim datenow = Date.Now()
    Dim spacetoconcat
    
    Spacetoconcat = words + datenow.tostring() + endwords
    'theres spacer methods in the vb
    May be & to
    Maybe sit the next couple plays out...

  9. #9
    Member
    Join Date
    Apr 2022
    Posts
    62

    Re: VB.NET read text file data between two dates

    Quote Originally Posted by OptionBase1 View Post
    Maybe sit the next couple plays out...
    I'm gonna play with out like two dogs caged yo

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    Hi jmcilhinney... I am struggling a bit trying to remove what I need from my old code. How can I fit yours in? I am going to use a new form for a 'datetimepicker' to get two dates. I can then grab those variables, but I think I am just getting myself confused with this as I have not yet used a datetimepicker. I was able to get the datepicker values from the new form, now I just need to get it all sorted. Here is what I have now for how I have been doing it:

    Code:
    Dim filename = IO.Path.Combine(Environment.CurrentDirectory, "Defect_Files", "Defects.txt")
    Dim HowMnyDays = InputBox(vbCrLf & "How Many Days Back Do You Want To Review?", "Days to Review")
    
            If HowMnyDays Is Nothing Then Exit Sub
    
            Try
    
                If (IO.File.Exists(filename)) Then
                    Dim lines = IO.File.ReadAllLines(filename)
    
                    For Each line In lines
                        Dim Data = line.Split(","c)
                        If (Data.Length > 0 AndAlso Data(0) = ToggleFile) Then
                            If (CDate(Data(4)) > DateTime.Now.AddDays(-CInt(HowMnyDays))) Then
                                TextBox1.Text = "ALL-DEFECTS"
                                Dim fromTextFile As String = Data(7)
                                Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1)
    
                                Dim dynamicButton As New Button With {
                .Location = New Point(CInt(Data(2)), CInt(Data(3))),
                .Height = 7,
                .Width = 7,
                .FlatStyle = FlatStyle.Flat,
                .BackColor = Drawing.Color.FromName(color),
                .ForeColor = Drawing.Color.FromName(color)
            }

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    You have this:
    vb.net Code:
    1. Dim lines = IO.File.ReadAllLines(filename)
    2.  
    3. For Each line In lines
    I have this:
    vb.net Code:
    1. Dim lines = File.ReadLines(filePath).
    2.                  SkipWhile(Function(line) getDateTime(line) < startDate).
    3.                  TakeWhile(Function(line) getDateTime(line) < endDate)
    4.  
    5. For Each line In lines
    I'm not sure what isn't clear about what you need to change.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    Ok, this is what I have right now in my button code:

    Code:
    Private Sub BtnCreateMap_Click(sender As Object, e As EventArgs) Handles BtnCreateMap.Click
    
            If PictureBox1.Image Is Nothing Then
                MsgBox("No CCA image to create Heat-Map", vbExclamation, "")
                Exit Sub
            End If
    
            Dim filename = IO.Path.Combine(Environment.CurrentDirectory, "Defect_Files", "HeatMap_Defects.txt")
            Dim HowMnyDays = InputBox(vbCrLf & "How Many Days Back Do You Want To Review?", "Days to Review")
    
            PickDate.Show() <<< DatePicker Form <<<
    
            If HowMnyDays Is Nothing Then Exit Sub  <<< This should be replaced by DatePicker values <<<
    
            Try
    
                If (IO.File.Exists(filename)) Then
                    Dim lines = IO.File.ReadAllLines(filename)
    
                    For Each line In lines
                        Dim Data = line.Split(","c)
                        If (Data.Length > 0 AndAlso Data(0) = ToggleFile) Then
                            If (CDate(Data(4)) > DateTime.Now.AddDays(-CInt(HowMnyDays))) Then
                                TextBox1.Text = "DEFECT MAP"
                                Dim fromTextFile As String = Data(7)
                                Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1)
    
                                Dim dynamicButton As New Button With {
                    .Location = New Point(CInt(Data(2)), CInt(Data(3))),
                    .Height = 7,
                    .Width = 7,
                    .FlatStyle = FlatStyle.Flat,
                    .BackColor = Drawing.Color.FromName(color),
                    .ForeColor = Drawing.Color.FromName(color)
                }
    
    
                                If (Data.Length > 4) Then
                                    ToolTip1.SetToolTip(dynamicButton, $"SN: {Data(1)} '{Data(6)}'")
                                End If
    
                                If PictureBox1.Visible = True Then
                                    PictureBox1.Select()
                                    If Not color.ToString = "Empty" Then
                                        PictureBox1.Controls.Add(dynamicButton)
                                    End If
                                End If
    
                                If PictureBox2.Visible = True Then
                                    PictureBox2.Select()
                                    If Not color.ToString = "Empty" Then
                                        PictureBox2.Controls.Add(dynamicButton)
                                    End If
                                End If
                            End If
                        End If
                Next
                End If
    End Sub
    So from my DatePicker form values, how do I call the function you created? When I tried to merge all of my button code in with your function, I get an error about converting a string to an integer when trying to look at another value within the split file. I hope I am making sense trying to explain this.

    Here is my DatePicker Form code:

    Code:
    Public Class PickDate
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Form1.dpFrom = DatePickerFROM.Value
            Form1.dpTo = DatePickerTO.Value
            Dim result As TimeSpan = Form1.dpFrom.Subtract(Form1.dpTo)
            Dim ds As Integer = result.TotalDays
            TextBox1.Text = ds
            MsgBox(DatePickerFROM.Value & " - " & DatePickerTO.Value)
        End Sub
    End Class

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    If you are using a separate form with two DateTimePickers on it then you should display that as a modal dialogue and get the date values via properties, e.g. in the second form:
    vb.net Code:
    1. Public ReadOnly Property StartDate As Date
    2.     Get
    3.         Return startDatePicker.Value.Date
    4.     End Get
    5. End Property
    6.  
    7. Public ReadOnly Property EndDate As Date
    8.     Get
    9.         Return endDatePicker.Value.Date
    10.     End Get
    11. End Property
    and in the first form:
    vb.net Code:
    1. Using dialogue As New Form2
    2.     If dialogue.ShowDialog() = DialogResult.OK Then
    3.         ReadLinesByDateRange(dialogue.StartDate, dialogue.EndDate)
    4.     End If
    5. End Using

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    Sorry, I am still getting lost between the two forms. I put your code in my 'DatePicker' form and I do get the form to popup, then I select two dates. What do I need to do in order to get the values automatically after selecting them on the datepicker form? I am guessing that needs to somehow replace my original line here:

    Code:
    If (CDate(Data(4)) > DateTime.Now.AddDays(-CInt(HowMnyDays))) Then
    Never dealing with a datepicker, I feel totally lost on this.

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    You're trying to do every6thing backwards. Stop! Think about the logic first and then write code to implement that logic. If that means redoing something you've already done then so be it.

    Start by thinking about the date input only. What do you need? You need a form with two date controls that will pass that input back to the caller. Do that and that only. I've already told you what to do. Do that now. Once that's done, the date controls become completely irrelevant to the question of how to read lines from a file. You simply display the dialogue and get the date values, as I have shown you how to do, and then pass those values to the code that reads the file, as I've shown you how to do. getting the date input and using the date input are two co0mpletely separate issues so start treating them that way. One of the main reasons that beginners have problems is that they treat everything as one big problem instead of several little ones. The smaller the problem, the easier it is to solve, so break the big problem down into several smaller ones and solve each one independently. using date input is the same regardless of how you get it and getting date input is the same regardless of how you use it, so neither subproblem is at all dependent on the other. That also means that no question posted on this or similar sites should be asking about both.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    thanks jmcilhinney, I see your point on this and appreciate you explaining it. I am going to look closely at this over the weekend and break it down piece by piece.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    Ok, I am just now finally getting back to this. I can't seem to get beyond this error and do not understand what can be causing it. The data is the same with the same date format etc.

    Attachment 184875

  18. #18
    Member
    Join Date
    Apr 2022
    Posts
    62

    Re: VB.NET read text file data between two dates

    Wow some of this code examples look like trash from vb6

  19. #19
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    Quote Originally Posted by mikeg71 View Post
    Ok, I am just now finally getting back to this. I can't seem to get beyond this error and do not understand what can be causing it. The data is the same with the same date format etc.

    Attachment 184875
    Your attachment is invalid. That's the site's fault rather than yours - there's a bug with inline attachments - but that's irrelevant anyway. There's no need to attach anything. Post the relevant code as text, formatted as code. Post error message as the text they are. Attachments just make things harder, even when they do work. They have their uses but basic code snippets and error messages is not one of them.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    Thanks for the info on that, I will make sure I don't do attachments knowing that.

    Here is the portion from your original code and my modified version (which seems to work). I don't understand why this makes a difference, but when I run the original, I get "string not recognized as a valid date/time"

    Code:
    'modified line
    Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
    
    'your original line
    Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/dd/yyyy h:mm:ss tt", Nothing)

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: VB.NET read text file data between two dates

    I seem to have made it beyond the datepicker, but now I have another glitch. When trying to get my color from the text file, I get the error in the marked line below. Is this because I am re-splitting the already split file?

    Code:
         Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
         Dim lines = File.ReadAllLines(filePath).
                         SkipWhile(Function(line) getDateTime(line) < startDate).
                         TakeWhile(Function(line) getDateTime(line) < endDate)
    
            For Each line In lines
    
                MsgBox(line)
                TextBox1.Text = "ALL DEFECTS"
                Dim fromTextFile As String = line(7)
                Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1)    <<<< NEW ERROR: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
                Dim dynamicButton As New Button With {
                        .Location = New Point(CInt(line(2).ToString), CInt(line(3).ToString)),
                        .Height = 7,
                        .Width = 7,
                        .FlatStyle = FlatStyle.Flat,
                        .BackColor = Drawing.Color.FromName(color),
                        .ForeColor = Drawing.Color.FromName(color)
                    }

  22. #22
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    Quote Originally Posted by mikeg71 View Post
    Thanks for the info on that, I will make sure I don't do attachments knowing that.

    Here is the portion from your original code and my modified version (which seems to work). I don't understand why this makes a difference, but when I run the original, I get "string not recognized as a valid date/time"

    Code:
    'modified line
    Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
    
    'your original line
    Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/dd/yyyy h:mm:ss tt", Nothing)
    Look at the difference between them. One is specifying a 2-digit daya and the other is specifying a 1-digit day. Is you day 2 digits? Did you read the documentation about date/time formatting? If you have a day value less than 10 and it does not have a leading 0 on it then that is not valid if using "dd" for the day.

  23. #23
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,501

    Re: VB.NET read text file data between two dates

    Quote Originally Posted by mikeg71 View Post
    I seem to have made it beyond the datepicker, but now I have another glitch. When trying to get my color from the text file, I get the error in the marked line below. Is this because I am re-splitting the already split file?

    Code:
         Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
         Dim lines = File.ReadAllLines(filePath).
                         SkipWhile(Function(line) getDateTime(line) < startDate).
                         TakeWhile(Function(line) getDateTime(line) < endDate)
    
            For Each line In lines
    
                MsgBox(line)
                TextBox1.Text = "ALL DEFECTS"
                Dim fromTextFile As String = line(7)
                Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1)    <<<< NEW ERROR: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
                Dim dynamicButton As New Button With {
                        .Location = New Point(CInt(line(2).ToString), CInt(line(3).ToString)),
                        .Height = 7,
                        .Width = 7,
                        .FlatStyle = FlatStyle.Flat,
                        .BackColor = Drawing.Color.FromName(color),
                        .ForeColor = Drawing.Color.FromName(color)
                    }
    Use the debugger. The error message is telling you that the index you're using is out of range so look at the index you're using and the valid range to see why it's wrong. The index is hard-coded right there in your code: 1. Obviously there is not element at index 1 in that array. Did you look at the array you got back from Split to see what it contained, or even the line you were splitting beforehand? It would seem not, but why not? If you don't know how to use the debugger then stop what you're doing and learn that first because you need to have debugged your code before posting a question.

  24. #24
    Member
    Join Date
    Apr 2022
    Posts
    62

    Re: VB.NET read text file data between two dates

    Remember events are like structures there purpose is fast and accurate memory access.

  25. #25
    Hyperactive Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    438

    Re: VB.NET read text file data between two dates

    Quote Originally Posted by mmx88_ValidUser View Post
    Remember events are like structures there purpose is fast and accurate memory access.
    mmx88_ValidUser, would you please keep your gibberish in Chit Chat!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width