Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB 2010 Express saving and opening text file

Posted on 2011-03-09
7
Medium Priority
?
1,225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
Suggested Courses

704 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