I have just about finished my first program. It consists of 4 forms, each form with a multitude of text boxes and buttons. Basically this program is going to keep track of some numbers for me by deducting 1/0.5 (depending on which button is pushed) from a total entered at the beginning of each week. Ive got all my buttons etc doing everything I want them to do but I've come across a major problem, when I close my program all the numbers disappear, I don't know how to save my user input (the numbers in my textboxes) I don't need to keep any history or anything of these numbers. I just need the program to remember the numbers that it is up to when i close it so that I can carry on deducting from them the next time I open the program. At the beginning of each week this set of numbers will be deleted and new numbers entered for the current week. If anyone can help me I would really appreciate it.
12-09-2006 at 03:41 AM
|
shahidmojid Level: Professor Registered: 09-05-2002 Posts: 85
Re: Best/simplest way to save user input
Well, i have done in my previous program before 2 years.
1. save values in text file at the time of your form close
2. whenever open the form READ the text file and write in text boxes.
You may use OPEN command to open text file and print/ write command to write into text file. Dont forget to use close command to close the text file.
Thanks heaps for your reply, but could you tell me how to go about this please.
12-09-2006 at 08:51 PM
|
shahidmojid Level: Professor Registered: 09-05-2002 Posts: 85
Re: Best/simplest way to save user input
Hi...
I wish if i could make code smaller. But it will not give you complete idea. Here is the code and i am sure it will give you an idea how to store data.
1. add three textboxes in your form
2. add a button BtnClose to close your form
3. keep name of the form is form2
4. copy the code into your form and run
Imports System
Imports System.IO
Public Class Form2
Dim FILE_NAME As String = Application.StartupPath & "\saved_data.txt"
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
If File.Exists(FILE_NAME) Then
SaveText()
Else
Using sw As StreamWriter = File.CreateText(FILE_NAME)
sw.Close() 'just create and close
End Using
SaveText() 'here is saving data into created file
End If
Me.Close()
End Sub
Private Sub SaveText()
Try
Using sw As StreamWriter = New StreamWriter(FILE_NAME)
' Add some text to the file. you can use write for command for multiple text in one line.
' there are many file open mode like append mode, binary....
sw.WriteLine(Me.TextBox1.Text)
sw.WriteLine(Me.TextBox2.Text)
sw.WriteLine(Me.TextBox3.Text)
'sw.Write("The date is: " & Now()) you can use write command also
sw.Close()
End Using
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If File.Exists(FILE_NAME) Then
Using sw As StreamReader = New StreamReader(FILE_NAME)
' read text file
Me.TextBox1.Text = sw.ReadLine
Me.TextBox2.Text = sw.ReadLine
Me.TextBox3.Text = sw.ReadLine
sw.Close()
End Using
End If
End Sub
End Class