Results 1 to 12 of 12

Thread: [RESOLVED] Saving textbox.text to file as written

  1. #1

    Thread Starter
    Lively Member VisualBrian's Avatar
    Join Date
    Nov 2019
    Location
    North America
    Posts
    69

    Resolved [RESOLVED] Saving textbox.text to file as written

    I have a routine that saves multiple vars to a file...

    Code:
        Friend Sub SaveMap()
            Try
                For Each ctrl In Me.Controls.Cast(Of Control)().ToArray()
                    If ctrl.Tag = "room" Then
                        FileOpen(3, MapDir & ctrl.Name & ".room", OpenMode.Input)
                        RoomName = LineInput(3)
                        RoomTag = LineInput(3)
                        RoomSize = LineInput(3)
                        RoomLeft = LineInput(3)
                        RoomTop = LineInput(3)
                        RoomWidth = LineInput(3)
                        RoomHeight = LineInput(3)
                        RoomImage = LineInput(3)
                        RoomText = LineInput(3)
                        FileClose(3)
                        FileOpen(3, MapDir & ctrl.Name & ".room", OpenMode.Output)
                        ' name, tag, RoomSize, left, top, width, height, RoomImage, RoomText
                        ' get room size
                        Select Case ctrl.Width
                            Case Is = GridSize
                                If ctrl.Height = GridSize Then
                                    RoomSize = "small"
                                Else
                                    RoomSize = "tall"
                                End If
                            Case Is = GridSize * 2
                                If ctrl.Height = GridSize Then
                                    RoomSize = "wide"
                                Else
                                    RoomSize = "large"
                                End If
                        End Select
                        Print(3, RoomName & vbNewLine & RoomTag & vbNewLine & RoomSize & vbNewLine & ctrl.Left & vbNewLine & ctrl.Top & _
                         vbNewLine & ctrl.Width & vbNewLine & ctrl.Height & vbNewLine & RoomImage & vbNewLine & RoomText)
                        FileClose(3)
                    End If
                Next
                ' save MapFile
                FileOpen(3, MapFile, OpenMode.Output)
                Print(3, MapTemplate)
                FileClose(3)
            Catch ex As Exception
                MsgBox("While SaveMap()..." & vbNewLine & ex.Message)
            End Try
        End Sub
    My thought was to use Do Until EOF(3) to read all the "RoomText" at the end of the file, but RoomText does not get printed to the file as written. Only the first paragraph.

    The problem is that with "RoomText", only the first line of text is saved to the file. Anything after a 'return' in the TextBox is ignored, so I can only save one (the first) paragraph. The textbox is set to accept returns. I have tried this with a normal text box and a richtextbox. Other than using File.WriteAllText to a dedicated file for the text alone, I have not found a way to save the text, or read it back, as written to the Mapfile, with the other vars.

    What am I missing? How do I save and recover the text as written in the textbox?
    Last edited by VisualBrian; Dec 1st, 2023 at 10:46 PM.
    I may not know anything, but I know it well!

  2. #2
    New Member
    Join Date
    Dec 2023
    Posts
    2

    Re: Saving textbox.text to file as written

    One possible reason for this issue could be that the text box is not set up to accept multi-line input.

    To enable multi-line input, you can set the Multiline property of the text box to True. This will allow the user to enter multiple lines of text, and the text box will store the entire text as entered.

    To save the text as written in the textbox, you can use the TextBox.Text property to retrieve the entire text entered by the user. You can then write this text to a file using the System.IO.File.WriteAllText method.

    To read the text back from the file, you can use the System.IO.File.ReadAllText method to read the entire contents of the file into a string. You can then set the TextBox.Text property to this string to display the text in the textbox.
    Last edited by si_the_geek; Dec 2nd, 2023 at 01:32 PM. Reason: removed unnecessary link

  3. #3
    Addicted Member
    Join Date
    Jul 2022
    Posts
    167

    Re: Saving textbox.text to file as written

    It would be helpful to see an example of data in the file and the data in the textbox that was supposed to be placed in the file.

    This piece of code looks to just open a file, read it into some vars, sets RoomSize value depending on the control width and then writes everything back out to the file. Nothing is done with RoomText here (besides reading it from the room file and then writing it back). The code where RoomText is initially written to the file you're reading would need to be uploaded to assist in figuring out why it is not meeting your expectations.

  4. #4

    Thread Starter
    Lively Member VisualBrian's Avatar
    Join Date
    Nov 2019
    Location
    North America
    Posts
    69

    Re: Saving textbox.text to file as written

    Quote Originally Posted by jdelano View Post
    It would be helpful to see an example of data in the file and the data in the textbox that was supposed to be placed in the file.

    This piece of code looks to just open a file, read it into some vars, sets RoomSize value depending on the control width and then writes everything back out to the file. Nothing is done with RoomText here (besides reading it from the room file and then writing it back). The code where RoomText is initially written to the file you're reading would need to be uploaded to assist in figuring out why it is not meeting your expectations.
    This is what the file looks like after it is written.

    Code:
    Warren House
    room
    small
    300
    300
    100
    100
    house.jpg
    Catching a murder case two weeks prior to your retirement wasn't exactly the way you saw it playing out when you got to the office this morning, but as shorthanded as the department is, you count yourself lucky that they're willing to let you go. Mary—worrying about you more and more as your last workday approaches—is really going to lose it when she finds out. You hope this one will go quick and easy but standing outside the crime scene taped yard in front of Warren House looking up at the broken attic window, you have doubts. A uniformed officer is standing by the front door.
    What I left out was that before this code, TextBoxRoomText.Text is assigned to the var 'RoomText' before the save code is executed.
    Code:
    RoomText = TextBoxRoomText.Text
    RoomText is the last part of the file, the actual story part. So long as there's only one paragraph as above, it works fine. If I write a second paragraph by using the return key in the text box, the return is ignored, and the two paragraphs are written as one without a space. Is it the way RoomText is assigned? I've tried assigning it 'by line' but with no success.
    I may not know anything, but I know it well!

  5. #5
    Addicted Member
    Join Date
    Jul 2022
    Posts
    167

    Re: Saving textbox.text to file as written

    oh, okay. I see now. (side note: Warren house wouldn't have anything to do with Richard Warren???)

    Somehow, the CrLf is stripped out when the file is saved with the information. Thus, when it is read again the paragraphs are no longer separated.
    Let me do a quick test as well.

    EDIT:
    Maybe I'm over simplifying things. I have a multiline textbox, I copied in your text and then I just hit enter 2x in the middle and clicked my save button. I use a StreamWriter to write the info into a text file, I deleted everything in the textbox and then loaded the saved text from the file using a StreamReader.

    (pay no attention to the other buttons, I use this project to hold a bunch of things I attempt to help others with)

    Code:
        Private Sub btnSaveRoomText_Click(sender As Object, e As EventArgs) Handles btnSaveRoomText.Click
    
            Dim sw As New StreamWriter("F:\TEMP\RoomText.room")
            sw.Write(txtRoomText.Text)
            sw.Close()
    
        End Sub
    
        Private Sub btnLoadRoomText_Click(sender As Object, e As EventArgs) Handles btnLoadRoomText.Click
    
            txtRoomText.Text = ""
            Dim sr As New StreamReader("F:\TEMP\RoomText.room")
    
            txtRoomText.Text = sr.ReadToEnd()
    
            sr.Close()
    
        End Sub
    Attached Images Attached Images     
    Last edited by jdelano; Dec 2nd, 2023 at 09:55 AM.

  6. #6

    Thread Starter
    Lively Member VisualBrian's Avatar
    Join Date
    Nov 2019
    Location
    North America
    Posts
    69

    Re: Saving textbox.text to file as written

    @ jdelano
    Thanks for taking the time and doing that. It seems very difficult for me to get all of my thoughts and my entire need into a single post/request. I am aware of the way you did that and have done that before.

    The issue here is that I need to get the story text into the same file as the other information, and then get it back out as written. I'm not quite ready to double the number of files per story passage (100 plus) and code the rewrite it would take. I hope it doesn't come to that. Fingers crossed....

    On your side note, no I don't know Richard Warren. The name was pulled out of my hat as I grew up in Warren.
    Last edited by VisualBrian; Dec 2nd, 2023 at 10:38 AM.
    I may not know anything, but I know it well!

  7. #7
    Addicted Member
    Join Date
    Jul 2022
    Posts
    167

    Re: Saving textbox.text to file as written

    Yeah, my bad. I focused on the wrong portion.

    Code:
            txtRoomText.Text = ""
            Dim sr As New StreamReader("F:\TEMP\RoomText.room")
            Dim linesRead As Int16 = 0
            Dim fileContents As String
            Dim splitFileOnLF As String()
            Dim temp As String
    
            ' read the first 8 lines and put them in the listbox
            fileContents = sr.ReadToEnd
            splitFileOnLF = fileContents.Split(vbCrLf)
            sr.Close()
    
            For Each ln As String In splitFileOnLF
                linesRead += 1
                If linesRead < 9 Then
                    temp = ln.Replace(Chr(34), "")
                    temp = temp.Replace(vbLf, "")
                    temp = temp.Replace("&", "")
                    ListBox1.Items.Add(temp)
                Else
                    If ln = vbLf Then
                        txtRoomText.Text += vbCrLf
                    Else
                        temp = ln.Replace(vbLf, "")
                        temp = temp.Replace("&", "")
    
                        txtRoomText.Text += temp
                    End If
    
                End If
    
            Next

  8. #8

    Thread Starter
    Lively Member VisualBrian's Avatar
    Join Date
    Nov 2019
    Location
    North America
    Posts
    69

    Re: Saving textbox.text to file as written

    @jdelano Thanks again. Let me see if I understand this.

    We're using a StreamReader to grab the first 8 values from the file and put them aside (ListBox) for later use.
    Then the last lines (RoomText) are grabbed and put back in the TextBox correctly.

    Cool. If I can ever get the TextBoxRoomText to save with the needed vbCrLf's intact, this will be useful.
    I may not know anything, but I know it well!

  9. #9

    Thread Starter
    Lively Member VisualBrian's Avatar
    Join Date
    Nov 2019
    Location
    North America
    Posts
    69

    Re: Saving textbox.text to file as written

    OK, my bad...
    The text was saving 'as written' to the file, but when the file is read again only the first line is being read. I need to track that down. Thanks for the help.
    I may not know anything, but I know it well!

  10. #10
    Addicted Member
    Join Date
    Jul 2022
    Posts
    167

    Re: [RESOLVED] Saving textbox.text to file as written

    Sorry, the grand kids showed up and the eldest clicked my mouse which replied LMAO. Let me finish.

    Given this file (screenshot 1)
    I read it into a variable, when I did that this was the result
    Code:
    "Warren House" & vbCrLf & "room" & vbCrLf & "small" & vbCrLf & "300" & vbCrLf & "300" & vbCrLf & "100" & vbCrLf & "100" & vbCrLf & "house.jpg" & vbCrLf & "Catching a murder case two weeks prior to your retirement wasn't exactly the way you saw it playing out when you got to the office this morning, but as shorthanded as the department is, you count yourself lucky that they're willing to let you go. " & vbCrLf & vbCrLf & "Mary—worrying about you more and more as your last workday approaches—is really going to lose it when she finds out. You hope this one will go quick and easy but standing outside the crime scene taped yard in front of Warren House looking up at the broken attic window, you have doubts. A uniformed officer is standing by the front door."
    So, I added a little clean up code. I just added a listbox to write the first part to and then added the remaining text to the textbox. Not conventional nor elegant for sure.
    Attached Images Attached Images   

  11. #11

    Thread Starter
    Lively Member VisualBrian's Avatar
    Join Date
    Nov 2019
    Location
    North America
    Posts
    69

    Re: [RESOLVED] Saving textbox.text to file as written

    Thank you. I got all that and appreciate it. A little explanation on my part...

    Attachment 189439

    Each of these files is tied to a label (what I refer to as a room or story event/location) on the Map form and can be dragged around to create a relational story map. Each time a label MouseDown event happens, the file has to updated with the label location and then again on the MouseUp event to make sure there is no overlap, so all the vars are being read and wrote again. Again when changes are made to the room/event with the editor, all the vars must be updated again. I finally found where in the process of saving and loading that the lines were not being saved correctly. I thought the TextBox text was not being saved as written, but it was. It was that the next time it was saved, it was not being saved by lines correctly.

    All is well and thanks for your attention and time.
    I may not know anything, but I know it well!

  12. #12
    Addicted Member
    Join Date
    Jul 2022
    Posts
    167

    Re: [RESOLVED] Saving textbox.text to file as written

    Awesome, very glad you got it.

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