Solved

Still exporting text, and a further question

Posted on 2013-01-01
3
291 Views
Last Modified: 2013-01-01
Hi:  I've been plugging away at exporting text from VB 2010.  Moving right along, but now Im at the stage where I want to deliver a prompt to users to allow them to select or enter a filename.  In short, I'm including a savefiledialog code with streamwriter code; I don't know where to insert the savefiledialog code.  Below is what I have so far:

Dim objFile As New System.IO.StreamWriter(SaveFileDialog1, True)

        SaveFileDialog1.Title = "Specify Desitination Filename"
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt"
        SaveFileDialog1.FilterIndex = 1

        objFile.WriteLine(RichTextBox1.Text)
        objFile.Close()

Thanks in advance.  Robert
0
Comment
Question by:RobertGeoff
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38734971
Use the ShowDialog() method and only save the file if "OK" is returned:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SaveFileDialog1.Title = "Specify Desitination Filename"
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt"
        SaveFileDialog1.FilterIndex = 1

        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Using objFile As New System.IO.StreamWriter(SaveFileDialog1.FileName, True)
                objFile.WriteLine(RichTextBox1.Text)
            End Using
        End If
    End Sub

Open in new window

0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 38735204
I will use WriteAllText instead of streaming the file. Replace lines 6 to 8  of previous post with:

System.IO.File.WriteAllText(SaveFileDialog1.FileName, RichTextBox1.Text)
0
 

Author Closing Comment

by:RobertGeoff
ID: 38735458
Thank you both for you help.  The second modification offered returned an exception, however.  Robert
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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