Saving Text Boxes and Auto Loading it

Lanc1988

Well-known member
Joined
Nov 27, 2003
Messages
508
I have tabs on my main form and inside one of the tabs i have a Combat Calculator which has about 7 text boxes in which you enter numbers to them calculate your combat.

I was wondering the code required to save those values to a file in a specified directory and then when my program is opened it will auto load them into the correct text boxes in which they were entered.

Thanks in advance for any time you take to help me get this to work.
 
Hello Lanc1988,

Kind of depends on how you want the data to be saved. You can use the System.IO Class to save the values to a .dat file, or use the native .NET XML Class to save to XML. Both pretty easy to use.

As an example for System.IO ...

Prior to your main solution form class:
PHP:
Imports System.IO

Then in the form closing event:
PHP:
Dim sw as StreamWriter = new StreamWriter("yourpath.dat",True) The "True" at the end means you want to append.
sw.Writeline("TotalScore" & vbTab & me.txtTotalScore.Text)
sw.Writeline("SessionScore" & vbTab & me.txtScoreThisSession.Text)
Etc..
sw.Flush()

sw.Close()
sw = Nothing

You can then use the streamreader to get the values back out of the file and load to your text boxes.
 
How do I get the form closing? I only know how to get the Form_Load (by double clicking the form)
 
It is saying StreamWriter is not defined.

And also, for your example with:
sw.Writeline("TotalScore" & vbTab & me.txtTotalScore.Text)

I would replace TotalScore with what? I now that the me.txtTotalScore.Text I would replace with the textboxs name, but im not sure what to replace the TotalScore with.
 
Code:
    Private Sub Form3_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    End Sub
In some cases, you can go to your Form.Load event, then just look up and select your desired event from the right-hand combobox.
Or you can change the first combobox to (Form Events)
and then change the right-hand box to your event.

For the StreamWriter, you can try:
Dim sw As System.IO.StreamWriter
if your Imports statement was in the wrong place.

The "TotalScore" part is written directly to file (youll see TotalScore in the file if you open it in Notepad :))
 
Ok, they save now.

Now i just need the code to load them back into the boxes when the program is reopened. I guess I will need to use the "StreamReader"?

Well if someone could post an example it would help me alot. Thanks
 
Ill show you how to Read a line in StreamReader. I dont know the specifics of your program, but you should be able to read in and adjust it to do what you want.
Code:
Dim SR As System.IO.StreamReader = New System.IO.StreamReader(Filename)
Dim S As String, Sarm As Integer
S = SR.ReadLine()  Read a line... we will parse it soon.
Sarm = S.IndexOf(vbTab)  Finds the tab.
S = S.Substring(Sarm + 1)
 
Just discovered a problem with the saving, it doesnt seem to replace the file if it exists already, it will come up as an error.
 
And also, I pasted exactly what you put for loading it once its been saved, but it doesnt seem to load it, should I change any parts of the code you pasted?
 
I opened the .dat file in notepad and discovered it keeps adding to the file, not replacing it. So is there some code to have it replace the file if it exists?
 
I have this under the Form_Close event:

Code:
        On Error Resume Next
        Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(Application.StartupPath & "\Sidekick Files\CombatCalcLvls.dat", True) The "True" at the end means you want to append.
        sw.WriteLine("Attack" & vbTab & Me.AttackBox.Text)
        sw.WriteLine("Defense" & vbTab & Me.DefenseBox.Text)
        sw.WriteLine("Strength" & vbTab & Me.StrengthBox.Text)
        sw.WriteLine("Hits" & vbTab & Me.HitsBox.Text)
        sw.WriteLine("Ranged" & vbTab & Me.RangedBox.Text)
        sw.WriteLine("Magic" & vbTab & Me.MagicBox.Text)
        sw.WriteLine("Prayer" & vbTab & Me.PrayerBox.Text)

        sw.Flush()
        sw.Close()
        sw = Nothing
 
and this code I have under the Form_Load event:

Code:
        Loads values into the Combat Calculator
        Dim SR As System.IO.StreamReader = New System.IO.StreamReader(Application.StartupPath & "\Sidekick Files\CombatCalcLvls.dat")
        Dim S As String, Sarm As Integer
        S = SR.ReadLine()  Read a line... we will parse it soon.
        Sarm = S.IndexOf(vbTab)  Finds the tab.
        S = S.Substring(Sarm + 1)


When it saves on the Form_Close event, it saves, but continues to add information to the .dat file, it should replace it if it exists.
 
you have the streamwriter set to append , so it wont replace information , it will keep adding it to the file , changing the True , to False should sort that
Code:
/// change this --> Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(Application.StartupPath & "\Sidekick Files\CombatCalcLvls.dat", True) The "True" at the end means you want to append.
///to this 
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(Application.StartupPath & "\Sidekick Files\CombatCalcLvls.dat", False) /// replace the info in the file.
 
Ok, but I still have a problem loading the data back into the text boxes once i open the program.
 
anyone know how to get it to load them into the right text boxes? ( i cant get it to load at all )
 
When your reading back out of your file, thats why I suggested the descriptors in before the actual score.

You would just use the .split(vbTab) to convert the line to a string array.

PHP:
If vals(0) = "something" Then  Vals(0) is the first column in your .dat file, 1 is the actual score.
   me."MyTextBox".Text = vals(1)
Elseif vals(0) = "somthingelse" Then
etc...
End if
 
Code:
       On Error Resume Next
        Dim SR As System.IO.StreamReader = New System.IO.StreamReader(Application.StartupPath & "\Sidekick Files\CombatCalcLvls.dat")
        If vals(0) = "Attack" Then  Vals(0) is the first column in your .dat file, 1 is the actual score.
            Me.AttackBox.Text = vals(1)
        ElseIf vals(0) = "somthingelse" Then
            etc...
        End If

It is saying that vals is not declared. How do i declare it?
 
Back
Top