|
-
Jun 28th, 2011, 03:08 AM
#1
Thread Starter
New Member
HelpNeeded!!!
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?
-
Jun 28th, 2011, 03:42 AM
#2
Re: HelpNeeded!!!
Welcome to VBForums 
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 )
-
Jun 28th, 2011, 03:51 AM
#3
Re: HelpNeeded!!!
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.
-
Jun 28th, 2011, 04:08 AM
#4
Fanatic Member
Re: HelpNeeded!!!
I recommend writing the information to a settings file. You can go all difficult with:
option1="value"
option2="value2"
To make a regular INI file, but for starters you could simply use the order of the values in your program.
Use the streamreader/streamwriter object:
Writing the information (on form closing)
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()
Reading the information (on form load)
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
I recommend parsing the lines, since it is more resilient to user-made mistakes.
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()
-
Jun 28th, 2011, 05:28 AM
#5
Re: HelpNeeded!!!
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
-
Jun 28th, 2011, 09:08 AM
#6
-
Jun 28th, 2011, 03:57 PM
#7
Re: HelpNeeded!!!
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|