Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Help! manipulating text files in a form

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
mpaver
Asked:
mpaver
  • 4
  • 4
  • 2
2 Solutions
 
bramsquadCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Bob LearnedCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> 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
 
Bob LearnedCommented:
Ok, no harm, no foul :)

Bob
0
 
bramsquadCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> 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
 
bramsquadCommented:
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
 
bramsquadCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now