Solved

VB 2010 Express saving and opening text file

Posted on 2011-03-09
7
1,203 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 74

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 74

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now