Results 1 to 11 of 11

Thread: Saving and Loading TextBoxes and Labels

  1. #1

    Thread Starter
    Junior Member Corvette's Avatar
    Join Date
    Jun 2011
    Posts
    22

    Saving and Loading TextBoxes and Labels

    I've been looking into the SaveFileDialog and OpenFileDialog features, but have been failing to implement them. The actualy program that I would be implementing this in is much larger, so alternative methods are welcomed.

    Code:
    Public Class Form1
        Public Results(2) As Double
    
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
            Dim A, B, C As Double
            Double.TryParse(TextBox1.Text, A)
            Double.TryParse(TextBox2.Text, B)
    
            C = A + B
    
            Label3.Text = C
        End Sub
    
        Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
            Dim FileToSaveAs As String = SaveFileDialog1.FileName
    
            Dim objwriter As New System.IO.StreamWriter(FileToSaveAs)
            For i = 0 To 2
                objwriter.WriteLine(Results(i))
            Next
            objwriter.Close()
        End Sub
    
        Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
            Results(0) = TextBox1.Text
            Results(1) = TextBox2.Text
            Results(2) = Label3.Text
            SaveFileDialog1.ShowDialog()
        End Sub
    
        Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click
            OpenFileDialog1.ShowDialog()
            TextBox1.Text = Results(0)
            TextBox2.Text = Results(1)
            Label3.Text = Results(2)
        End Sub
    
        Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
            Dim SavedFile As String = OpenFileDialog1.FileName
    
            Dim objreader As New System.IO.StreamReader(SavedFile)
            For i = 0 To 2
                objreader.ReadLine(Results(i))
            Next
            objreader.Close()
        End Sub
    End Class
    I also found this page: http://visualbasic.about.com/od/usin...ppsettings.htm which seems to suggest that VB.Net can just save thing automatically

    And this thread: http://www.vbforums.com/showthread.p...ht=save+inputs which at the end mentions a "bianary formatter".

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Saving and Loading TextBoxes and Labels

    you can automatically save the textboxes + label text like this:

    for each control to save:

    • select the control in the designer
    • expand the properties (ApplicationSettings) member
    • select the (PropertyBinding) member + click the ellipsis to the right
    • in the dialog, select the Text property + click the dropdown arrow to the right
    • click (New...) + type in a unique Name for the setting in the dialog
    • click OK on both dialogs + that's it
    • now repeat for your other controls


    after performing these steps, your text will be automatically saved + reloaded next time you run the app.

  3. #3

    Thread Starter
    Junior Member Corvette's Avatar
    Join Date
    Jun 2011
    Posts
    22

    Re: Saving and Loading TextBoxes and Labels

    Awesome quick response. Sounds good, can the user save multiple instances? If your instructions covered my next question, ignore me because I'll be going through them later tonight

    In my case:
    Acme boiler
    Big Co. boiler

    Then open and save these and a blank form as needed?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Saving and Loading TextBoxes and Labels

    perform those steps for each textbox or label whose text you want to save

  5. #5

    Thread Starter
    Junior Member Corvette's Avatar
    Join Date
    Jun 2011
    Posts
    22

    Re: Saving and Loading TextBoxes and Labels

    Sorry, I probably wasn't clear or I'm not getting it.

    My interpretation of your method makes it easy for a person to start and stop a set of calculations, but it doesn't make it possible for them to start over while still retaining a copy of thier previous work.

    If the person completes the program and wants to retain the data for their records or for later use can all of the data be retained as a group while also allowing a different set of data to be calculated?

    I guess they could make a copy and then rename the executable or something sloppy like that. I have also succeded in kicking out text files so a copy of their work could be retained, but not easily inserted into a new instance of the program.

    I guess that leads me to this question. Can I make a button that saves the .exe under a user assigned name while also retaining the original un-filled program? New thread or leave it here?

    Thanks for the help. So far I have a potential solution which I didn't have before.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Saving and Loading TextBoxes and Labels

    you can't use the (ApplicationSettings) method in that case
    what was the problem with the code in your original post?

  7. #7

    Thread Starter
    Junior Member Corvette's Avatar
    Join Date
    Jun 2011
    Posts
    22

    Re: Saving and Loading TextBoxes and Labels

    I set the Text properties of all of the textbox and labels and the program flips out now. I try to enter number into the fields and they erase, the cursor moves around, and the field resets. Setting the property back to none solves the problem, but I'm left with no way for the user to save progress.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Saving and Loading TextBoxes and Labels

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         'save
    5.         Dim sfd As New SaveFileDialog
    6.         If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
    7.             IO.File.WriteAllLines(sfd.FileName, New String() {TextBox1.Text, TextBox2.Text, Label3.Text})
    8.         End If
    9.     End Sub
    10.  
    11.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    12.         'load
    13.         Dim ofd As New OpenFileDialog
    14.         If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    15.             Dim lines() As String = IO.File.ReadAllLines(ofd.FileName)
    16.             If lines.Count = 3 Then
    17.                 TextBox1.Text = lines(0)
    18.                 TextBox2.Text = lines(1)
    19.                 Label3.Text = lines(2)
    20.             End If
    21.         End If
    22.     End Sub
    23.  
    24. End Class

  9. #9

    Thread Starter
    Junior Member Corvette's Avatar
    Join Date
    Jun 2011
    Posts
    22

    Re: Saving and Loading TextBoxes and Labels

    Maybe this will help somebody. If you have code like this:

    Code:
    Public Class Form1
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
            Dim A, B, C As Double
            Double.TryParse(TextBox1.Text, A)
            Double.TryParse(TextBox2.Text, B)
    
            C = A + B
    
            Label3.Text = C
        End Sub
    End Class
    You cannot set (ApplicationSettings) -> (PropertyBinding) -> Text to both the TextBoxes and the Label. In this simple example doing so results in textboxes that do not show what you type and a Sub that doesn't perform calculations. By simply setting Label3 -> Properties -> (ApplicationSettings) -> (PropertyBinding) -> Text back to None I was able to get the example working.

    .paul. , I hope to have time to try your code today. Also, it would be nice to have text saying "The steam temperature is ###." I think I can do that, but can I get the button2 action to skip the text. I could probably double.tryparse the text right out of it you think?

    Thanks.

  10. #10

    Thread Starter
    Junior Member Corvette's Avatar
    Join Date
    Jun 2011
    Posts
    22

    Re: Saving and Loading TextBoxes and Labels

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         'save
    5.         Dim sfd As New SaveFileDialog
    6.         If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
    7.             IO.File.WriteAllLines(sfd.FileName, New String() {TextBox1.Text, TextBox2.Text, Label3.Text})
    8.         End If
    9.     End Sub
    10.  
    11.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    12.         'load
    13.         Dim ofd As New OpenFileDialog
    14.         If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    15.             Dim lines() As String = IO.File.ReadAllLines(ofd.FileName)
    16.             If lines.Count = 3 Then
    17.                 TextBox1.Text = lines(0)
    18.                 TextBox2.Text = lines(1)
    19.                 Label3.Text = lines(2)
    20.             End If
    21.         End If
    22.     End Sub
    23.  
    24. End Class
    Any way to get that to look for a folder and only at .txt like with Dialogs -> OpenFileDialog and SaveFileDialog?

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Saving and Loading TextBoxes and Labels


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