Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Visual Basics File Input/Output and Copy Operations In this assignment, you will

ID: 3571893 • Letter: V

Question

Visual Basics

File Input/Output and Copy Operations

In this assignment, you will practice file input/output operations.

Before starting this lab, copy and paste the following text data into Notepad, and save it to a file.

john simmonds 79

lisa burns 89

albert halls 90

tina wounds 81

saka lisbon 75

will toms 60

tom cat 74

Steps to Follow

1.       Create a VB.NET Windows project having one form.

2.       Add a button named “btnCopy” to the form displaying the text “&Copy.” Add another button named “btnSave” displaying the text “&Save.” Both buttons should be aligned horizontally at the bottom of the form.  

3.       Drag a textbox control named “txtText” onto the form. Set its Multiline property to true. Move the upper left corner of the textbox to the upper left corner of the form. Drag the lower-right corner of the textbox so it fills most of the form’s interior area (Leave enough room for the button controls).

4.       Drag an OpenFileDialog component onto the form naming it “ofdTextFile.” Note that this component is not visible on the form.

5.       Drag a SaveFileDialog component onto the form naming it “sfdTextFile.”

6.       In the “btnCopy” event handler, write code calling the “ShowDialog” method of the Open File Dialog allowing the user to select a filename to open. Write code to open the selected file and read its text into the textbox control if the user clicked the file dialog’s OK button. Open the file you saved earlier in Notepad.

7.       In the “btnSave” event handler, write code calling the “ShowDialog” method of the Save File Dialog allowing the user to provide a filename to save. Write code to save the text contained in the textbox control to the selected filename if the user clicked the file dialog’s OK button. You may make changes to the contents of the textbox before requesting the save operation.

8.       In Windows Explorer, verify that the save operation was successful by locating the file you saved and opening it in Notepad.

Explanation / Answer

Form1.vb

Public Class Form1
    Dim strText As String
    Dim strOpenFileName As String
    Dim strSaveFileName As String
    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click

        OpenFD.Title = "Please Select a File"
        OpenFD.InitialDirectory = "C:temp"
        OpenFD.Filter = "Text Files|*.txt"
        OpenFD.ShowDialog()
        strOpenFileName = OpenFD.FileName

        'My.Computer.FileSystem.ReadAllText(strFileName, strText.tos, True)
        strText = My.Computer.FileSystem.ReadAllText(strOpenFileName)
        'Dim value As String = My.Computer.FileSystem.ReadAllText(file)
        'My.Computer.FileSystem.ReadAllText(strFileName, strText, True)
        txtData.Text = strText
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        SaveFD.Title = "Save As"
        SaveFD.InitialDirectory = "C:temp"
        SaveFD.Filter = "Text Files|*.txt"
        SaveFD.ShowDialog()
        strSaveFileName = SaveFD.FileName
        My.Computer.FileSystem.WriteAllText(strSaveFileName, strText, True)
    End Sub
End Class

Form1.Designer.vb

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.btnCopy = New System.Windows.Forms.Button()
        Me.btnSave = New System.Windows.Forms.Button()
        Me.OpenFD = New System.Windows.Forms.OpenFileDialog()
        Me.txtData = New System.Windows.Forms.TextBox()
        Me.SaveFD = New System.Windows.Forms.SaveFileDialog()
        Me.SuspendLayout()
        '
        'btnCopy
        '
        Me.btnCopy.Location = New System.Drawing.Point(51, 227)
        Me.btnCopy.Name = "btnCopy"
        Me.btnCopy.Size = New System.Drawing.Size(75, 23)
        Me.btnCopy.TabIndex = 0
        Me.btnCopy.Text = "Copy"
        Me.btnCopy.UseVisualStyleBackColor = True
        '
        'btnSave
        '
        Me.btnSave.Location = New System.Drawing.Point(164, 227)
        Me.btnSave.Name = "btnSave"
        Me.btnSave.Size = New System.Drawing.Size(75, 23)
        Me.btnSave.TabIndex = 1
        Me.btnSave.Text = "Save"
        Me.btnSave.UseVisualStyleBackColor = True
        '
        'txtData
        '
        Me.txtData.Location = New System.Drawing.Point(12, 12)
        Me.txtData.Multiline = True
        Me.txtData.Name = "txtData"
        Me.txtData.ReadOnly = True
        Me.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtData.Size = New System.Drawing.Size(260, 200)
        Me.txtData.TabIndex = 2
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 262)
        Me.Controls.Add(Me.txtData)
        Me.Controls.Add(Me.btnSave)
        Me.Controls.Add(Me.btnCopy)
        Me.Name = "Form1"
        Me.Text = "Assignment 15"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents btnCopy As System.Windows.Forms.Button
    Friend WithEvents btnSave As System.Windows.Forms.Button
    Friend WithEvents OpenFD As System.Windows.Forms.OpenFileDialog
    Friend WithEvents txtData As System.Windows.Forms.TextBox
    Friend WithEvents SaveFD As System.Windows.Forms.SaveFileDialog

End Class


Data.txt

john simmonds 79
lisa burns 89
albert halls 90
tina wounds 81
saka lisbon 75
will toms 60
tom cat 74