Solved

Help! manipulating text files in a form

Posted on 2004-09-01
12
196 Views
Last Modified: 2010-04-23
Hi I have a textbox in a form that i use to click a button and display the text from it in. From this I then want to be able to edit the text in the textbox and save it, making it overwrite all previous data in that text file. So in short I need to;

- open the file in the textbox (done, but will accept changes)
- edit the text, then
- click a save button that overwrites the original text file with the text in the textbox

in the text file, the spacers between Text that I have used is a    ,    and each word is in " "

Thanks

mpaver
0
Comment
Question by:mpaver
  • 4
  • 4
  • 2
12 Comments
 
LVL 8

Accepted Solution

by:
bramsquad earned 63 total points
ID: 11954491
couple steps (for the save function)...

first go to your toolbar and drag over the

SaveFileDialog

tools.  then make a button (save button) and add this code

        Dim sv As New SaveFileDialog

        If Me.txtOutput.Text = "" Then
              MsgBox("Cannot Save, Text field is empty.")
              Exit Sub
        End If

        sv.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        sv.ShowDialog()
        If sv.FileName <> "" Then
            FileOpen(1, sv.FileName, OpenMode.Output)
            PrintLine(1, Me.txtOutput.Text)
            FileClose(1)
        End If

should be good

~b

       
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 62 total points
ID: 11955038
Here is a complete solution:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents cmdOpenFile As System.Windows.Forms.Button
    Friend WithEvents cmdSaveFile As System.Windows.Forms.Button
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.cmdOpenFile = New System.Windows.Forms.Button
        Me.cmdSaveFile = New System.Windows.Forms.Button
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.TextBox1.Location = New System.Drawing.Point(8, 40)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.TextBox1.Size = New System.Drawing.Size(536, 328)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'cmdOpenFile
        '
        Me.cmdOpenFile.Location = New System.Drawing.Point(8, 8)
        Me.cmdOpenFile.Name = "cmdOpenFile"
        Me.cmdOpenFile.Size = New System.Drawing.Size(72, 24)
        Me.cmdOpenFile.TabIndex = 2
        Me.cmdOpenFile.Text = "Open File"
        '
        'cmdSaveFile
        '
        Me.cmdSaveFile.Location = New System.Drawing.Point(88, 8)
        Me.cmdSaveFile.Name = "cmdSaveFile"
        Me.cmdSaveFile.Size = New System.Drawing.Size(72, 24)
        Me.cmdSaveFile.TabIndex = 3
        Me.cmdSaveFile.Text = "Save File"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(552, 374)
        Me.Controls.Add(Me.cmdSaveFile)
        Me.Controls.Add(Me.cmdOpenFile)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Text File Editor"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private fileName As String = ""
    Private originalContents As String = ""

    Private Sub cmdOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpenFile.Click
        OpenFileDialog1.Title = "Open Text File"
        OpenFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            fileName = OpenFileDialog1.FileName
            Try
                Dim sr As New System.IO.StreamReader(fileName)
                TextBox1.Text = sr.ReadToEnd
                sr.Close()
                Me.Text = "Text File Editor - " & fileName
                originalContents = TextBox1.Text
            Catch ex As Exception
                MsgBox(fileName & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Error Reading File")
            End Try
        End If
    End Sub

    Private Sub cmdSaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveFile.Click
        SaveFileDialog1.Title = "Save Text File"
        SaveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        If fileName.Length > 0 Then
            SaveFileDialog1.FileName = fileName
        End If
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            Try
                Dim sw As New System.IO.StreamWriter(SaveFileDialog1.FileName)
                sw.Write(TextBox1.Text)
                sw.Close()
            Catch ex As Exception
                MsgBox(SaveFileDialog1.FileName & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Error Saving File")
            End Try
        End If
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Not TextBox1.Text.Equals(originalContents) Then
            If MsgBox("Would you like to save your changes?", MsgBoxStyle.Information Or MsgBoxStyle.YesNo, "Changes will be lost") = MsgBoxResult.Yes Then
                cmdSaveFile_Click(Nothing, Nothing)
            End If
        End If
    End Sub

End Class
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11955634
It sounds to me like creating a CSV file, with commas and quote delimiters, which I don't see in any of the solutions provided.

Bob
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11955746
>> It sounds to me like creating a CSV file, with commas and quote delimiters

You are right.

>> which I don't see in any of the solutions provided.

What is there to see?  In my app the textfile is loaded into the textbox where you can edit it to your hearts content and then save the file again.  There is nothing preventing the user from putting quotes around their data and placing commas inbetween.  If the file was already CSV formatted, it will appear that way in the textbox.  Again the user can edit it to their hearts content and save it again.

There is nothing in the original question about how to generate a CSV file from any given input.  The question is presented in a way that to me suggests freeform editing of the text file contents:

>> - open the file in the textbox (done, but will accept changes)
>> - edit the text, then
>> - click a save button that overwrites the original text file with the text in the textbox

I don't see a problem...

Idle_Mind
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11955921
Ok, no harm, no foul :)

Bob
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Expert Comment

by:bramsquad
ID: 11956394
either way....i dont see what your code has that mine doesnt

command button declarations, open file code (which he already had)....why go through the process of undermining my correct answer?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11956717
>> either way....i dont see what your code has that mine doesnt

If you want to bring it up then:

1) More conventional use of the Open/Save File Dialogs
2) I/O operations contained in a Try...Catch statement
3) Use of the System.IO namespace for file I/O instead of Microsoft.VisualBasic (which basically allows old style VB6 code to be used - I am guilty of using the old MsgBox function instead of the newer MessageBox however)

>> command button declarations, open file code (which he already had)....why go through the process of undermining my correct answer?
 
"undermining" your "correct answer" would be saying that your answer is wrong...which I didn't.  If mpaver likes your answer better and sees no need for mine, then he will accept yours and give me no points.  I haven't prevented this from happening in any way.

A "correct answer" is in the eye of the beholder...

What I have done is given a DIFFERENT solution to the problem.

Regards,

Idle_Mind
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 11957104
i dont mean to come off wrong here, but i totally disagree

what he asked for was a form that would do a certain amount of things.  what i provided him with was enough for him to solve his solution.

in doing that, am i providing him with a "correct" answer?  yes.

did i post a "correct" answer before you did?  yes.

however, you did provide some more options and shed more light on different subjects......but nearly everything you provided (that i didnt that is) was extraneous to the question.

the "undermining" i speak of has absolutely everything to do with these extra features you put in.  the points on this site are arbitrary, but correct answers are certain.  why should someone be neglected points for providing a correct answer with less bells and whistles?  especially when they werent requested?

like i said, i dont mean to come off abraisve.....and i have quite a lot of respect for every member.  in this rebuttle im mearly stating the way i think points should be divided, and the reason i got defensive i think you can decipher from what ive said here.

thanks,

~b
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 11957809
Idle_Mind:

kinda funny, this is somewhat of a similar situation as were in now, just different roles.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21105828.html

thanks for your help btw.

~b
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11957842
My pleasure!

One thing I love about programming is the multitude of ways you can usually solve a problem.

Beauty is in the eye of the beholder...

Idle_Mind
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

15 Experts available now in Live!

Get 1:1 Help Now