Get data from a .txt file

Craig

Well-known member
Joined
Oct 15, 2005
Messages
110
Location
United Kingdom
Programming Experience
Beginner
Basically I have 12 textboxes, and I need the program to be able to save the text that is stored in each box so that, when the form is closed and opened again, it will still have what was typed in each textbox

Any help is appreciated
 
well you could simply make global variables to hold the information by adding a module to the project and declaring each as Friend or Public then in the form with the text box's you can use the load event to assign the values from the variables to the textbox's and in the form's closing event you can assign the contents of the textbox's to the variables

but if it needs to be saved in a text file simply use the StreamReader and StreamWriter classes to save and retreive the info
 
Global variables are a great solution. You could also use an array or datatable defined in a module called PublicVariables (search forum for examples of public variables). Trouble with that is if the project is closed the data is not saved anywhere. If you want the data to be loaded at startup and saved after shutdown you will need to use the text file.

Here is basic code for read/write to text files, you will need to update it for you application but it can get you started.

VB.NET:
Dim FS As IO.FileStream = SaveFileDialog1.OpenFile
Dim SW As New IO.StreamWriter(FS)
SW.Write(TextBox1.Text & ControlChars.NewLine & TextBox2.Text)
SW.Close()
FS.Close()

VB.NET:
Dim TextString As String
Dim FS As IO.FileStream = OpenFileDialog1.OpenFile
Dim SR As IO.StreamReader = New IO.StreamReader(FS)
TextString = SR.ReadToEnd
SR.Close()
RichTextBox1.Text = TextString
 
Open/Save Dialog Write Read to Text Issue

I was reading this thread in an attempt to get some form of configuration save and or loading for my program. I had to use properties to get my variables from my configuration form to my main form that went alright I modified the command as was suggested so that I would have all the variables I needed saved saved resulting in.
VB.NET:
Dim ConfigSave As IO.FileStream = SaveFileDialog1.OpenFile
        Dim ConfigWrite As New IO.StreamWriter(ConfigSave)
        ConfigWrite.Write(KeyConfig.Buff1Key & ControlChars.NewLine & KeyConfig.Buff2Key & ControlChars.NewLine & KeyConfig.Buff3Key & ControlChars.
NewLine & KeyConfig.AutoLootKey & ControlChars.NewLine & KeyConfig.AutoAttkKey & ControlChars.NewLine & KeyConfig.HPPotKey & ControlChars.
NewLine & KeyConfig.MPPotKey & ControlChars.NewLine & KeyConfig.BuffDelays & ControlChars.NewLine & KeyConfig.BuffDelays1 & ControlChars.
NewLine & KeyConfig.BuffDelays2)
        ConfigWrite.Close()
        ConfigSave.Close()

I added a savefiledialog and left it named SaveFileDialog1 as its the only save file dialog I have. Therefore no need to rename it anyway I ran my program outside of visual studio 2005 it compiled perfectly without so much as a warning.

I set my config keys to somthing in my keyconfig form just so it would have some value to write to text then I went to my main toolbar on main form hit the dropdown went to save click and boom crash Ill re-establish the crash so I can give you the error it presented.

Index was outside the bounds of the array? Would that have anything to do with VS Express not placing the save dialog with the command and if it is I would have to manually write the save dialog (No big deal there) Possibly Save Dialog Settings? I left everything default since I was just testing it

http://www.vbdotnetforums.com/showthread.php?t=8164&highlight=Save+Variables+To+File
 
Elsys, you do realize the StreamWriter have a WriteLine method that will add linefeed? For example:
VB.NET:
ConfigWrite.WriteLine(KeyConfig.Buff1Key)
ConfigWrite.WriteLine(KeyConfig.Buff2Key)
ConfigWrite.WriteLine(KeyConfig.Buff3Key)
 
I did end up figguring that out on my own but thanks for not leaving me in the dark.
 
Last edited by a moderator:
Back
Top