|
-
Apr 5th, 2012, 06:35 PM
#1
Thread Starter
Junior Member
Saving and Loading TextBoxes and Labels
I've been looking into the SaveFileDialog and OpenFileDialog features, but have been failing to implement them. The actualy program that I would be implementing this in is much larger, so alternative methods are welcomed.
Code:
Public Class Form1
Public Results(2) As Double
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
Dim A, B, C As Double
Double.TryParse(TextBox1.Text, A)
Double.TryParse(TextBox2.Text, B)
C = A + B
Label3.Text = C
End Sub
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Dim FileToSaveAs As String = SaveFileDialog1.FileName
Dim objwriter As New System.IO.StreamWriter(FileToSaveAs)
For i = 0 To 2
objwriter.WriteLine(Results(i))
Next
objwriter.Close()
End Sub
Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
Results(0) = TextBox1.Text
Results(1) = TextBox2.Text
Results(2) = Label3.Text
SaveFileDialog1.ShowDialog()
End Sub
Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click
OpenFileDialog1.ShowDialog()
TextBox1.Text = Results(0)
TextBox2.Text = Results(1)
Label3.Text = Results(2)
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim SavedFile As String = OpenFileDialog1.FileName
Dim objreader As New System.IO.StreamReader(SavedFile)
For i = 0 To 2
objreader.ReadLine(Results(i))
Next
objreader.Close()
End Sub
End Class
I also found this page: http://visualbasic.about.com/od/usin...ppsettings.htm which seems to suggest that VB.Net can just save thing automatically
And this thread: http://www.vbforums.com/showthread.p...ht=save+inputs which at the end mentions a "bianary formatter".
-
Apr 5th, 2012, 06:54 PM
#2
Re: Saving and Loading TextBoxes and Labels
you can automatically save the textboxes + label text like this:
for each control to save:
- select the control in the designer
- expand the properties (ApplicationSettings) member
- select the (PropertyBinding) member + click the ellipsis to the right
- in the dialog, select the Text property + click the dropdown arrow to the right
- click (New...) + type in a unique Name for the setting in the dialog
- click OK on both dialogs + that's it
- now repeat for your other controls
after performing these steps, your text will be automatically saved + reloaded next time you run the app.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2012, 07:03 PM
#3
Thread Starter
Junior Member
Re: Saving and Loading TextBoxes and Labels
Awesome quick response. Sounds good, can the user save multiple instances? If your instructions covered my next question, ignore me because I'll be going through them later tonight 
In my case:
Acme boiler
Big Co. boiler
Then open and save these and a blank form as needed?
-
Apr 5th, 2012, 07:09 PM
#4
Re: Saving and Loading TextBoxes and Labels
perform those steps for each textbox or label whose text you want to save
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2012, 07:20 PM
#5
Thread Starter
Junior Member
Re: Saving and Loading TextBoxes and Labels
Sorry, I probably wasn't clear or I'm not getting it.
My interpretation of your method makes it easy for a person to start and stop a set of calculations, but it doesn't make it possible for them to start over while still retaining a copy of thier previous work.
If the person completes the program and wants to retain the data for their records or for later use can all of the data be retained as a group while also allowing a different set of data to be calculated?
I guess they could make a copy and then rename the executable or something sloppy like that. I have also succeded in kicking out text files so a copy of their work could be retained, but not easily inserted into a new instance of the program.
I guess that leads me to this question. Can I make a button that saves the .exe under a user assigned name while also retaining the original un-filled program? New thread or leave it here?
Thanks for the help. So far I have a potential solution which I didn't have before.
-
Apr 5th, 2012, 07:32 PM
#6
Re: Saving and Loading TextBoxes and Labels
you can't use the (ApplicationSettings) method in that case
what was the problem with the code in your original post?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 7th, 2012, 06:29 PM
#7
Thread Starter
Junior Member
Re: Saving and Loading TextBoxes and Labels
I set the Text properties of all of the textbox and labels and the program flips out now. I try to enter number into the fields and they erase, the cursor moves around, and the field resets. Setting the property back to none solves the problem, but I'm left with no way for the user to save progress.
-
Apr 7th, 2012, 06:40 PM
#8
Re: Saving and Loading TextBoxes and Labels
try this:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'save
Dim sfd As New SaveFileDialog
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
IO.File.WriteAllLines(sfd.FileName, New String() {TextBox1.Text, TextBox2.Text, Label3.Text})
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'load
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim lines() As String = IO.File.ReadAllLines(ofd.FileName)
If lines.Count = 3 Then
TextBox1.Text = lines(0)
TextBox2.Text = lines(1)
Label3.Text = lines(2)
End If
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 8th, 2012, 07:21 AM
#9
Thread Starter
Junior Member
Re: Saving and Loading TextBoxes and Labels
Maybe this will help somebody. If you have code like this:
Code:
Public Class Form1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
Dim A, B, C As Double
Double.TryParse(TextBox1.Text, A)
Double.TryParse(TextBox2.Text, B)
C = A + B
Label3.Text = C
End Sub
End Class
You cannot set (ApplicationSettings) -> (PropertyBinding) -> Text to both the TextBoxes and the Label. In this simple example doing so results in textboxes that do not show what you type and a Sub that doesn't perform calculations. By simply setting Label3 -> Properties -> (ApplicationSettings) -> (PropertyBinding) -> Text back to None I was able to get the example working.
.paul. , I hope to have time to try your code today. Also, it would be nice to have text saying "The steam temperature is ###." I think I can do that, but can I get the button2 action to skip the text. I could probably double.tryparse the text right out of it you think?
Thanks.
-
Apr 8th, 2012, 10:28 AM
#10
Thread Starter
Junior Member
Re: Saving and Loading TextBoxes and Labels
 Originally Posted by .paul.
try this:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'save
Dim sfd As New SaveFileDialog
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
IO.File.WriteAllLines(sfd.FileName, New String() {TextBox1.Text, TextBox2.Text, Label3.Text})
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'load
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim lines() As String = IO.File.ReadAllLines(ofd.FileName)
If lines.Count = 3 Then
TextBox1.Text = lines(0)
TextBox2.Text = lines(1)
Label3.Text = lines(2)
End If
End If
End Sub
End Class
Any way to get that to look for a folder and only at .txt like with Dialogs -> OpenFileDialog and SaveFileDialog?
-
Apr 8th, 2012, 10:49 AM
#11
Re: Saving and Loading TextBoxes and Labels
- 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
|