Solved

VB 2010 Express saving and opening text file

Posted on 2011-03-09
7
1,221 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

624 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