i have made two programs and the both contain many textboxes and one contains a datetimepicker but if i make it a .exe file then every time i close the application it doesnt save the contents and i need it to what do i do?
Printable View
i have made two programs and the both contain many textboxes and one contains a datetimepicker but if i make it a .exe file then every time i close the application it doesnt save the contents and i need it to what do i do?
Welcome to VBForums :wave:
Thread moved from the 'Assembly language' forum to the 'VB.Net' (VB2002 and later) forum
(for the benefit of others, 1020lokie's profile says VB 2010 )
If you want to persist the values in the textbox, you will need to save it somewhere and load it back again when your application starts the next time.
I recommend writing the information to a settings file. You can go all difficult with:
To make a regular INI file, but for starters you could simply use the order of the values in your program.Quote:
option1="value"
option2="value2"
Use the streamreader/streamwriter object:
Writing the information (on form closing)
Reading the information (on form load)Code:Dim settingsfile As String = Application.StartupPath & "\Settings.txt"
If IO.File.Exists(settingsfile) Then IO.File.Delete(settingsfile)
Dim writer As New IO.StreamWriter(settingsfile, True)
writer.WriteLine(TextBox1.Text)
writer.WriteLine(TextBox2.Text)
writer.WriteLine(TextBox3.Text)
'etc.
writer.Close()
I recommend parsing the lines, since it is more resilient to user-made mistakes.Code:Dim settingsfile As String = Application.StartupPath & "\Settings.txt"
If IO.File.Exists(settingsfile) Then
Dim reader As New IO.StreamReader(settingsfile)
TextBox1.Text = reader.ReadLine()
TextBox2.Text = reader.ReadLine()
TextBox3.Text = reader.ReadLine()
reader.Close()
End If
Code:Dim reader As New IO.StreamReader(settingsfile)
Do While reader.Peek <> -1
Dim textline As String = reader.ReadLine().TrimStart()
If textline.StartsWith("username=") Then
TextBox1.Text = textline.Remove(0, 9)
ElseIf textline.StartsWith("password=") Then
TextBox2.Text = textline.Remove(0, 9)
End If
Loop
reader.Close()
Try this:
1. Open project properties, Settings tab.
2. Add a setting
Name: TextBoxValues
Type: System.Collections.Specialized.StringCollection
Scope:User
3. Add the following code to your form:
vb.net Code:
Dim textBoxes() As TextBox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load textBoxes = New TextBox() {TextBox1, TextBox2} '<--- add all textbox names here whose value you want to persist. With My.Settings If .TextBoxValues Is Nothing Then .TextBoxValues = New System.Collections.Specialized.StringCollection For i = 0 To textBoxes.Length - 1 If .TextBoxValues.Count <= i Then .TextBoxValues.Add("") textBoxes(i).Text = .TextBoxValues(i) AddHandler textBoxes(i).Validating, AddressOf TextBoxes_Validating Next End With End Sub Private Sub TextBoxes_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Dim tb As TextBox = CType(sender, TextBox) Dim i As Integer = Array.IndexOf(textBoxes, tb) My.Settings.TextBoxValues(i) = tb.Text My.Settings.Save() End Sub
References, i hope u may have some choices here
(1) Code Project
(2) http://="http://msdn.microsoft.com/e...s.80%29.aspx"]
(3) Ged mead
i'd recommend (ApplicationSettings).
for a textbox:
vb Code:
'1/ in design view select your textbox '2/ in your properties window expand (ApplicationSettings) '3/ select (PropertyBinding) + click the ellipsis to the right '4/ select the Text property from the list + click the dropdown arrow to the right '5/ click (New...) + type in a Name for the setting '6/ click ok on the setting window + ok on the window beneath