Solved

VB 2010 Express saving and opening text file

Posted on 2011-03-09
7
1,210 Views
Last Modified: 2012-05-11
Hey i have a few fields setup as a form example:
UsernameTextbox.Text
EmailTextbox.Text
CountryComboBox.Text
------
I have a save button on the form screen called Button1.
When this button is pressed, i want the user to be able to save the above data to a file anywhere they want on their harddrive and name it whatever they want.

-------
I also have another button on the screen called Button2.
When this button is pressed, i want the user to be able to load data which was saved before.

If possible the file extension should change to something like .nme rather than .txt this will help error handling as users should only be able to load/save files with that extension.
0
Comment
Question by:alphaleads
  • 4
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35089020
0
 

Author Comment

by:alphaleads
ID: 35089038
I really don't understand any of that, how would i use it
0
 

Author Comment

by:alphaleads
ID: 35089207
        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "net manager e (*.nme)|*.nme"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here.
            End If
        End If

Open in new window


When i click the button it loads up with the save dialog, with a single extension as i set it, how do i write my values into this file?

On another note, i have another label called done1. this labels value is default set to NO this should also be stored on the text/nme file in my case.

But i have another command button called command2 on another form, when clicked it should update the value of done1 to YES, this should update the form aswell as the text/nme file which the user has saved before when first creating the file.

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35089241
>>  When this button is pressed, i want the user to be able to save the above data to a file anywhere they want on their harddrive and name it whatever they want.

SaveFileDialog is the component that would give you this functionality. It displays a dialog box to the user which allows him to navigate his filesystem and select a location and filename to save a file as. There is a member called Filename which you would pass to some Stream object in order to write the data to be saved to a file.

 Where to Find the SaveFileDialog Component SaveFileDialog Component in Action


>>  When this button is pressed, i want the user to be able to load data which was saved before.

OpenFileDialog is the component that would give you this functionality. It displays a dialog box to the user which allows him to navigate his filesystem and select a file to open. There is a member called Filename which is what you would pass to some Stream object in order to actually get the data from the file.

 Where to Find the OpenFileDialog Component OpenFileDialog Component in Action
0
 

Author Comment

by:alphaleads
ID: 35089351
I have already Done that?

When i click the button it loads up with the save dialog, with a single extension as i set it, how do i write my values into this file?

On another note, i have another label called done1. this labels value is default set to NO this should also be stored on the text/nme file in my case.

But i have another command button called command2 on another form, when clicked it should update the value of done1 to YES, this should update the form aswell as the text/nme file which the user has saved before when first creating the file.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35089638
Here's the very basics...I'd recommend using XML serialization instead though:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using sfd As New SaveFileDialog
            sfd.Filter = "Net manager e (*.nme)|*.nme|All files (*.*)|*.*"
            sfd.RestoreDirectory = True
            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Try
                    Using sw As New System.IO.StreamWriter(sfd.FileName, False)
                        sw.WriteLine(UsernameTextbox.Text)
                        sw.WriteLine(EmailTextbox.Text)
                        sw.WriteLine(CountryComboBox.SelectedItem)
                    End Using
                Catch ex As Exception
                    MessageBox.Show(ex.ToString, "Error Saving Data", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End Using
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Using ofd As New OpenFileDialog
            ofd.Filter = "Net manager e (*.nme)|*.nme|All files (*.*)|*.*"
            ofd.RestoreDirectory = True
            If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Try
                    Using sr As New System.IO.StreamReader(ofd.FileName)
                        UsernameTextbox.Text = sr.ReadLine
                        EmailTextbox.Text = sr.ReadLine()
                        CountryComboBox.SelectedItem = sr.ReadLine()
                    End Using
                Catch ex As Exception
                    MessageBox.Show(ex.ToString, "Error Loading Data", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End Using
    End Sub

End Class

Open in new window

0
 

Author Comment

by:alphaleads
ID: 35089995
Thank you so much i was looking for this all of this time  EmailTextbox.Text = sr.ReadLine() :),
it should help me with this .
"But i have another command button called command2 on another form, when clicked it should update the value of done1 to YES, this should update the form aswell as the text/nme file which the user has saved before when first creating the file."

all i will do is write to a text file,then load a text file, at the time of loading i will get the path to the file, so when it comes to writing the values for done1 that path can be used :) thank you
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question