Solved

VB 2010 Express saving and opening text file

Posted on 2011-03-09
7
1,212 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

827 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