13 Replies - 592 Views - Last Post: 15 November 2015 - 02:47 PM Rate Topic: -----

#1 doyouevenvisualbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 15

Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 11:39 AM

Good morning,
I'm having a bit of an issue with trying to get some specific data from an ini file and passing that data to a textbox. I can get the program to read the entire file, read one line or add multiple lines however I need it make it where it reads specific sections.
For example:

[cat1]
name=
pass=
loc=
dis=
[cat2]
stuff=
stuff1=
stuff2=
[cat3]
sdf=
csds=
dcvs=
sd=



I need the program to read each section individually where everything pertaining to [cat1] is stored in one textbox, [cat2] in another textbox and so on until each cat section is stored in its own textbox.
This is what I have so far. It's very sloppy and buggy and it works for a default ini file however if things get added or subtracted in the ini it'll throw everything off and not work.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim textfile As String = My.Settings.Directory & "\" & ListBox1.SelectedItem
    If IO.File.Exists(textfile) Then
        readLines = IO.File.ReadAllLines(textfile)
    End If
    count = 0
    While readLines IsNot Nothing AndAlso count < 20
        RichTextBox1.Text = RichTextBox1.Text & readLines(count) & vbCrLf
        count += 1
    End While
End Sub



I've also tried this

Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
    Dim textfile As String = My.Settings.Directory & "\" & ListBox1.SelectedItem
    If IO.File.Exists(textfile) Then
        readLines = IO.File.ReadAllLines(textfile)
    End If
    count = 0
    While readLines IsNot Nothing AndAlso count < 200
        If readLines(count).Equals("[Cat1]") Then
            Do Until readLines(count).Equals("[Cat2]")
                RichTextBox1.Text = RichTextBox1.Text & readLines(count) & vbCrLf
                count += 1
            Loop
        End If
    End While
End Sub



and this is the closest I've come to actually getting it. It'll read the first line that I need [cat1] but it won't read any other line between [cat1] and [cat2]. I know why it doesn't read it but I can't figure out how to advance the line.

Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
    Dim textfile As String = My.Settings.Directory & "\" & ListBox1.SelectedItem
    If IO.File.Exists(textfile) Then
        readLines = IO.File.ReadAllLines(textfile)
    End If

    For Each line In readLines
        If line.Contains("[cat1]") Then
            Do Until line.Contains("[cat2]")
                RichTextBox1.Text = RichTextBox1.Text & line & vbCrLf
            Loop
        End If
    Next
End Sub



I'm still pretty new to all of this but I've been trying to a few hours now to figure this out and I think it's time I just ask for help and see what the community says.

Thanks

Sorry for the formatting errors. Is post editing now allowed here or am I just missing the button to do it?

This post has been edited by andrewsw: 14 November 2015 - 11:54 AM
Reason for edit:: fixed botched code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Reading a file and storing blocks of info from it.

#2 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6737
  • View blog
  • Posts: 27,745
  • Joined: 12-December 12

Re: Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 11:54 AM

I have fixed the tags for you. You cannot edit yet, you need to have made a number of posts first.
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 12:58 PM

Perhaps the example below is what you want to do. It will read each line of the ini file into an Array of String. Then it iterates through the Array of Strings (the lines of text) and adds each section to a List(Of String).

When the next section or the end of the lines is reached, it adds a new RichTextBox to the form with the lines of the last section added to it.

I commented the code to help understand it. Hope it helps. 8)
Public Class Form1
    Private RtbLocY As Integer = 40 'the starting Y location of the first RTB that will be added

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AutoScroll = True 'set the AutoScroll to true so you can scroll to see all RTB`s that may be added
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'read the full ini file into an array of String
        Dim lines() As String = IO.File.ReadAllLines("C:\TestFolder\MyIniFile.ini")

        Dim Section As New List(Of String) 'create a List of String to hold each value of the current section

        'iterate from 0 to the length of the lines array minus 1
        For x As Integer = 0 To lines.Length - 1

            Dim line As String = lines(x) 'get the current line

            'if the line starts with "[" and it is not the first line or if the last line has been read
            'then create the new RichTextBox with the lines of text from the current section.
            If (line.StartsWith("[") And Not x = 0) Or x = lines.Length - 1 Then
                AddNewRichTextBox(Section.ToArray) 'pass the section list as an array to the sub that creates the new RTB
                Section.Clear() 'clear the list so the next line added to it is the beginning of the next section
            End If

            'If it is not a blank line then add it to the list
            If line.Trim <> "" Then Section.Add(line)

        Next
    End Sub

    Private Sub AddNewRichTextBox(ByVal LinesOfText As String())
        Dim rtb As New RichTextBox 'create a new RichTextBox
        rtb.Location = New Point(12, RtbLocY) 'set its location to below the last RichTextBox
        rtb.Size = New Size(250, 100) 'Set its size
        rtb.Lines = LinesOfText 'set its Lines property to the Array of strings passed to this sub
        Me.Controls.Add(rtb) 'add the RichTextBox to the form`s controls
        RtbLocY += 106 'add the height + a few extra pixels so, the next RTB will be below this one
    End Sub
End Class


This post has been edited by IronRazer: 14 November 2015 - 12:59 PM

Was This Post Helpful? 1
  • +
  • -

#4 doyouevenvisualbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 15

Re: Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 05:39 PM

That worked awesome.

How do I go about interacting with each RTB? I can't figure out what their name is.

I changed the x y locs so they all get added on top of each other. When I click a button I want the richtextbox that contains that info to be shown.

For example:

click button Cat1 - RTB that contains Cat[1] shows.
click button Cat2 - RTB that contains Cat[2] shows.

This post has been edited by andrewsw: 14 November 2015 - 05:42 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6737
  • View blog
  • Posts: 27,745
  • Joined: 12-December 12

Re: Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 05:41 PM

Note that you don't have to quote the previous post in full, there is a Reply button further down the page, or use the Fast Reply box.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 06:45 PM

Ok, that explains a bit more about what you are trying to do. From what i can see, you would be better off just using 1 RichTextBox and just add the lines to the RTB from the "cat" section that is indicated by the button you click.

Add 3 Buttons and 1 RichTextBox to a form and try it like this.
Public Class Form1
    Private FileLines As List(Of String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Read all the lines of the ini file into a List of Strings
        FileLines = IO.File.ReadAllLines("C:\TestFolder\MyIniFile.ini").ToList
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ShowCat("cat1") 'the cat name must be the same caseing or the cat name will not be found
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ShowCat("cat2")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ShowCat("cat3")
    End Sub

    Private Sub ShowCat(ByVal catName As String)

        'Find the index of the item in the List that matches the "cat" name passed to this sub.
        'If a match is not found then StartIndex will be -1.
        Dim StartIndex As Integer = FileLines.IndexOf("[" & catName & "]")

        'Check if the "cat" name was found
        If StartIndex > -1 Then

            RichTextBox1.Clear() 'clear the existing text from the RTB

            'Iterate through the list starting at the index the cat name was found at
            For i As Integer = StartIndex To FileLines.Count - 1

                'if the current line of text starts with a "[" character and it is not the starting index then
                'the next cat name has been reached. Meaning you have added all the data in the cat you wanted.
                'So, you exit the For Next loop by calling "Exit For" so no more lines are added to the RTB.
                If FileLines(i).StartsWith("[") And Not i = StartIndex Then Exit For

                'Add the current line of text to the RTB.
                RichTextBox1.AppendText(FileLines(i) & vbNewLine)
            Next

        End If
    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#7 doyouevenvisualbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 15

Re: Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 07:24 PM

That is exactly what I needed and I appreciate all of your help. You made it look extremely easy!
Was This Post Helpful? 0
  • +
  • -

#8 doyouevenvisualbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 15

Re: Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 10:38 PM

Ok, this is the last issue I have while working on this project(hopefully)

I'm using a SaveDialog to save all the changes to an .ini file. Everything saves fine but there aren't any line breaks in the file, it's all mushed together. How do I add line breaks while using WriteLine or is there a better method out there to save files?

Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Dim ksave As New StreamWriter(SaveFileDialog1.FileName)
        ksave.WriteLine(cat1 & cat2 & cat3)
        ksave.Close()
    End Sub



That outputs [cat1]mess=1mess=2mess=3[cat2]mess=1mess=2mess=3[cat3]mess=1mess=2mess=3

I'm using the code you helped me with above then adding each [catX] to it's own variable after it's edited in the textbox and storing/calling that variable as the user switches between them. The output in the textbox is perfect so I can't figure out why it's getting all jumbled together during the saving process.
Was This Post Helpful? 0
  • +
  • -

#9 doyouevenvisualbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 15

Re: Reading a file and storing blocks of info from it.

Posted 14 November 2015 - 10:53 PM

Edit: Well, it formats properly using Notepad++ but the format is still ugly while using Windows Notepad. Is there any way to fix it for Windows Notepad?
Was This Post Helpful? 0
  • +
  • -

#10 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Reading a file and storing blocks of info from it.

Posted 15 November 2015 - 03:36 AM

It looks like the CarriageReturn and LineFeed characters are being removed from the end of each line somehow before they are written to the file. I would need to see the code you have now to be able to help you fix it.

This post has been edited by IronRazer: 15 November 2015 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#11 doyouevenvisualbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 15

Re: Reading a file and storing blocks of info from it.

Posted 15 November 2015 - 08:21 AM

Here you go.

Private Sub ShowCat(ByVal catname As String)
        Dim StartIndex As Integer = fileLines.IndexOf("[" & catname & "]")
        If StartIndex > -1 Then
            RichTextBox1.Clear()
            For i As Integer = StartIndex To fileLines.Count - 1
                If fileLines(i).StartsWith("[") And Not i = StartIndex Then Exit For
                RichTextBox1.AppendText(fileLines(i) & vbNewLine)
        End If
    End Sub


    Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Dim ksave As New StreamWriter(SaveFileDialog1.FileName)
        ksave.WriteLine(cat1 & cat2 & cat3)
        ksave.Close()
    End Sub


That's pretty much all there is to it. Everything else is handled by variables.

This is used for storing the information as the user switches between the different cats.

Public Sub SwitchCat()
        If kCat = 1 Then
            kcat1 = RichTextBox1.Text
        End If

        If kCat = 2 Then
            kcat2 = RichTextBox1.Text
        End If

        If kCat = 3 Then
            kcat2 = RichTextBox1.Text
        End If
End Sub


This is used to populate all of the variables upon the loading of the ini.

 Public Sub populateVars()
        ShowCat("cat1")
        kcat1 = RichTextBox1.Text
        RichTextBox1.Clear()
        ShowCat("cat2")
        kcat1 = RichTextBox1.Text
        RichTextBox1.Clear()
        ShowCat("cat3")
        kcat3 = RichTextBox1.Text
        RichTextBox1.Clear()
End Sub

Was This Post Helpful? 0
  • +
  • -

#12 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Reading a file and storing blocks of info from it.

Posted 15 November 2015 - 01:40 PM

Well, i was half right, it seems that the CarriageReturn is being removed from the text when it is put in the RichTextBox. The text that is assigned back to the "cat" strings contains the LineFeed character but, the CarriageReturn is removed. To tell you the truth, i have never run into this problem with a RTB before and never knew the RTB control did that.

Anyways, it is basically a simple fix to the string that is saved to the file. You can join the "cat" strings into one string and then use the String.Replace method to replace all the single LineFeed characters with a CarriageReturn and LineFeed.

Something like this.
    Private Sub SaveFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Dim txt As String = cat1 & cat2 & cat3 'join the 3 strings into one
        txt = txt.Replace(vbLf, vbCrLf) 'replace all single LineFeed characters with a CaridgeReturn and a LineFeed character
        IO.File.WriteAllText(SaveFileDialog1.FileName, txt)
    End Sub



However, i saw a few things that could be changed around and done differently so, i threw this example together that you can check out. I made the ShowCat sub into a Function called GetCat. I use that to assign the cat`s to the 3 cat strings when the form loads.

Then i just switch the RTB`s text to the cat strings themselves when 1 of the 3 cat buttons are clicked. I also use the RTB`s TextChanged event to modify the cat string as you are typing changes into the RTB.

That way you can switch back an forth between the cat`s and they will keep any changes when you do.

Imports System.IO

Public Class Form1
    Private FileLines As List(Of String)
    Private cat1, cat2, cat3 As String
    Private kCat As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Read all the lines of the ini file into a List of Strings
        FileLines = IO.File.ReadAllLines("C:\TestFolder\MyIniFile.ini").ToList

        'fill all the cat strings from the file
        cat1 = GetCat("cat1")
        cat2 = GetCat("cat2")
        cat3 = GetCat("cat3")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        kCat = -1 'set to -1 before changing the RTB`s text so it will not save the wrong text to the wrong "cat"
        RichTextBox1.Text = cat1 'now change the RTB`s text to the cat string
        kCat = 1 'then set to the cat number so that any further changes to the text will be saved to the cat
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        kCat = -1
        RichTextBox1.Text = cat2
        kCat = 2
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        kCat = -1
        RichTextBox1.Text = cat3
        kCat = 3
    End Sub

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

    Private Sub SaveFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Dim txt As String = cat1 & cat2 & cat3 'join the 3 strings into one
        txt = txt.Replace(vbLf, vbCrLf) 'replace all single LineFeed characters with a CaridgeReturn and a LineFeed character
        File.WriteAllText(SaveFileDialog1.FileName, txt)
    End Sub

    Private Function GetCat(ByVal catname As String) As String
        Dim StartIndex As Integer = FileLines.IndexOf("[" & catname & "]")
        Dim lines As String = ""
        If StartIndex > -1 Then
            For i As Integer = StartIndex To FileLines.Count - 1
                If FileLines(i).StartsWith("[") And Not i = StartIndex Then Exit For
                lines &= FileLines(i) & vbNewLine
            Next
        End If
        Return lines
    End Function

    Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        If kCat <> -1 Then 'if kCat is -1 then we are switching the cat and don`t want to save anything
            If kCat = 1 Then cat1 = RichTextBox1.Text
            If kCat = 2 Then cat2 = RichTextBox1.Text
            If kCat = 3 Then cat3 = RichTextBox1.Text
        End If
    End Sub

End Class


Was This Post Helpful? 1
  • +
  • -

#13 doyouevenvisualbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 15

Re: Reading a file and storing blocks of info from it.

Posted 15 November 2015 - 02:31 PM

That looks great, thanks for being such a big help. You've definitely given me a lot of new insight and I greatly appreciate it.
Was This Post Helpful? 0
  • +
  • -

#14 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Reading a file and storing blocks of info from it.

Posted 15 November 2015 - 02:47 PM

You`re welcome, i am glad it helped. 8)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1