I have coded this for you. It is written in EXTREMELY easy code so you can
understand it. I could remove the repetitious code, but I think you would get
lost & not understand it like that.
There is no error checking to keep to code to a minimum.
You will need to create the file first & add text to the textboxes, save it
or you will get an index out of bounds error. I suggest, you comment out the
line of code in FORM LOAD, which will enable the save buttons, type something
in all the textboxes & save it. Then uncomment the line of text in FORM LOAD
again to disable the buttons again.
As before: Create a new Windows application & replace ALL form code... with
this:
-----------------------------------------------------------------------------------------------
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 TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents TextBox6 As System.Windows.Forms.TextBox
Friend WithEvents TextBox7 As System.Windows.Forms.TextBox
Friend WithEvents TextBox8 As System.Windows.Forms.TextBox
Friend WithEvents TextBox9 As System.Windows.Forms.TextBox
Friend WithEvents TextBox10 As System.Windows.Forms.TextBox
Friend WithEvents TextBox11 As System.Windows.Forms.TextBox
Friend WithEvents TextBox12 As System.Windows.Forms.TextBox
Friend WithEvents TextBox13 As System.Windows.Forms.TextBox
Friend WithEvents TextBox14 As System.Windows.Forms.TextBox
Friend WithEvents TextBox15 As System.Windows.Forms.TextBox
Friend WithEvents TextBox16 As System.Windows.Forms.TextBox
Friend WithEvents TextBox17 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.TextBox5 = New System.Windows.Forms.TextBox
Me.TextBox6 = New System.Windows.Forms.TextBox
Me.TextBox7 = New System.Windows.Forms.TextBox
Me.TextBox8 = New System.Windows.Forms.TextBox
Me.TextBox9 = New System.Windows.Forms.TextBox
Me.TextBox10 = New System.Windows.Forms.TextBox
Me.TextBox11 = New System.Windows.Forms.TextBox
Me.TextBox12 = New System.Windows.Forms.TextBox
Me.TextBox13 = New System.Windows.Forms.TextBox
Me.TextBox14 = New System.Windows.Forms.TextBox
Me.TextBox15 = New System.Windows.Forms.TextBox
Me.TextBox16 = New System.Windows.Forms.TextBox
Me.TextBox17 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.BackColor = System.Drawing.SystemColors.HighlightText
Me.TextBox1.Location = New System.Drawing.Point(16, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ReadOnly = True
Me.TextBox1.Size = New System.Drawing.Size(488, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(16, 40)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(232, 20)
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = ""
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(16, 72)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(232, 20)
Me.TextBox3.TabIndex = 2
Me.TextBox3.Text = ""
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(16, 104)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(232, 20)
Me.TextBox4.TabIndex = 3
Me.TextBox4.Text = ""
'
'TextBox5
'
Me.TextBox5.Location = New System.Drawing.Point(16, 136)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(232, 20)
Me.TextBox5.TabIndex = 4
Me.TextBox5.Text = ""
'
'TextBox6
'
Me.TextBox6.Location = New System.Drawing.Point(16, 168)
Me.TextBox6.Name = "TextBox6"
Me.TextBox6.Size = New System.Drawing.Size(232, 20)
Me.TextBox6.TabIndex = 5
Me.TextBox6.Text = ""
'
'TextBox7
'
Me.TextBox7.Location = New System.Drawing.Point(16, 200)
Me.TextBox7.Name = "TextBox7"
Me.TextBox7.Size = New System.Drawing.Size(232, 20)
Me.TextBox7.TabIndex = 6
Me.TextBox7.Text = ""
'
'TextBox8
'
Me.TextBox8.Location = New System.Drawing.Point(16, 232)
Me.TextBox8.Name = "TextBox8"
Me.TextBox8.Size = New System.Drawing.Size(232, 20)
Me.TextBox8.TabIndex = 7
Me.TextBox8.Text = ""
'
'TextBox9
'
Me.TextBox9.Location = New System.Drawing.Point(16, 264)
Me.TextBox9.Name = "TextBox9"
Me.TextBox9.Size = New System.Drawing.Size(232, 20)
Me.TextBox9.TabIndex = 8
Me.TextBox9.Text = ""
'
'TextBox10
'
Me.TextBox10.Location = New System.Drawing.Point(272, 40)
Me.TextBox10.Name = "TextBox10"
Me.TextBox10.Size = New System.Drawing.Size(232, 20)
Me.TextBox10.TabIndex = 9
Me.TextBox10.Text = ""
'
'TextBox11
'
Me.TextBox11.Location = New System.Drawing.Point(272, 72)
Me.TextBox11.Name = "TextBox11"
Me.TextBox11.Size = New System.Drawing.Size(232, 20)
Me.TextBox11.TabIndex = 10
Me.TextBox11.Text = ""
'
'TextBox12
'
Me.TextBox12.Location = New System.Drawing.Point(272, 104)
Me.TextBox12.Name = "TextBox12"
Me.TextBox12.Size = New System.Drawing.Size(232, 20)
Me.TextBox12.TabIndex = 11
Me.TextBox12.Text = ""
'
'TextBox13
'
Me.TextBox13.Location = New System.Drawing.Point(272, 136)
Me.TextBox13.Name = "TextBox13"
Me.TextBox13.Size = New System.Drawing.Size(232, 20)
Me.TextBox13.TabIndex = 12
Me.TextBox13.Text = ""
'
'TextBox14
'
Me.TextBox14.Location = New System.Drawing.Point(272, 168)
Me.TextBox14.Name = "TextBox14"
Me.TextBox14.Size = New System.Drawing.Size(232, 20)
Me.TextBox14.TabIndex = 13
Me.TextBox14.Text = ""
'
'TextBox15
'
Me.TextBox15.Location = New System.Drawing.Point(272, 200)
Me.TextBox15.Name = "TextBox15"
Me.TextBox15.Size = New System.Drawing.Size(232, 20)
Me.TextBox15.TabIndex = 14
Me.TextBox15.Text = ""
'
'TextBox16
'
Me.TextBox16.Location = New System.Drawing.Point(272, 232)
Me.TextBox16.Name = "TextBox16"
Me.TextBox16.Size = New System.Drawing.Size(232, 20)
Me.TextBox16.TabIndex = 15
Me.TextBox16.Text = ""
'
'TextBox17
'
Me.TextBox17.Location = New System.Drawing.Point(272, 264)
Me.TextBox17.Name = "TextBox17"
Me.TextBox17.Size = New System.Drawing.Size(232, 20)
Me.TextBox17.TabIndex = 16
Me.TextBox17.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(528, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 17
Me.Button1.Text = "Open"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(528, 40)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 18
Me.Button2.Text = "Save"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(528, 72)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 19
Me.Button3.Text = "Clear"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(528, 104)
Me.Button4.Name = "Button4"
Me.Button4.TabIndex = 20
Me.Button4.Text = "Exit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(618, 303)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox17)
Me.Controls.Add(Me.TextBox16)
Me.Controls.Add(Me.TextBox15)
Me.Controls.Add(Me.TextBox14)
Me.Controls.Add(Me.TextBox13)
Me.Controls.Add(Me.TextBox12)
Me.Controls.Add(Me.TextBox11)
Me.Controls.Add(Me.TextBox10)
Me.Controls.Add(Me.TextBox9)
Me.Controls.Add(Me.TextBox8)
Me.Controls.Add(Me.TextBox7)
Me.Controls.Add(Me.TextBox6)
Me.Controls.Add(Me.TextBox5)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Open/Save Example By Crouchie1998"
Me.ResumeLayout(False)
End Sub
#End Region
#Region "Declarations"
' Variable used for filename
Dim strFilename As String
' Variable to hold the default save location
Dim strDefaultLocation As String =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
' Stream reader object
Dim sr As IO.StreamReader
' Stream writer object
Dim sw As IO.StreamWriter
#End Region
#Region "Open Button"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
With OpenFileDialog1
' Open in Desktop directory
.InitialDirectory = strDefaultLocation
' Set up filters
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
' Select text files
.FilterIndex = 0
' Show dialog
If .ShowDialog = DialogResult.OK Then
' User pressed OK
' Pass the filename to the
' 'strFilename' variable
strFilename = .FileName()
' Show the file path in the textbox
TextBox1.Text = strFilename
Else
' User pressed cancel
Exit Sub
End If
End With
' Open the file chosen
' by the user
OpenFile(strFilename)
End Sub
#End Region
#Region "Open File (SUB)"
Private Sub OpenFile(ByVal sFilename As String)
' Create a new stream reader object
sr = New IO.StreamReader(sFilename)
' Variable to hold lines in the text file
Dim strLines() As String
' Variable to hold the file contents
Dim strLine As String
' Read the entire file (16 lines)
strLine = sr.ReadToEnd
' Seperate those lines
strLines = strLine.Split(ControlChars.CrLf)
' Pass those lines to the textboxes
TextBox2.Text = strLines(0).Trim
TextBox3.Text = strLines(1).Trim
TextBox4.Text = strLines(2).Trim
TextBox5.Text = strLines(3).Trim
TextBox6.Text = strLines(4).Trim
TextBox7.Text = strLines(5).Trim
TextBox8.Text = strLines(6).Trim
TextBox9.Text = strLines(7).Trim
TextBox10.Text = strLines(8).Trim
TextBox11.Text = strLines(9).Trim
TextBox12.Text = strLines(10).Trim
TextBox13.Text = strLines(11).Trim
TextBox14.Text = strLines(12).Trim
TextBox15.Text = strLines(13).Trim
TextBox16.Text = strLines(14).Trim
TextBox17.Text = strLines(15).Trim
' Close the stream reader
sr.Close()
' Destroy the stream reader object
sr = Nothing
' Enable the save & clear buttons, plus
' disable the open button
EnableDisableButtons(True)
End Sub
#End Region
#Region "Save Button"
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
' See open file dialog for info on this
With SaveFileDialog1
' Default extension to use
.DefaultExt = "txt"
.InitialDirectory = strDefaultLocation
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
.FilterIndex = 0
If .ShowDialog = DialogResult.OK Then
strFilename = .FileName()
Else
Exit Sub
End If
End With
' Save the file (comment out the above if
' statement to use the default opened filename)
SaveFile(strFilename)
End Sub
#End Region
#Region "Save File (SUB)"
Private Sub SaveFile(ByVal sFilename As String)
' Create a new stream writer object
sw = New IO.StreamWriter(strFilename)
' Add the contents of the textboxes to the file
sw.WriteLine(TextBox2.Text)
sw.WriteLine(TextBox3.Text)
sw.WriteLine(TextBox4.Text)
sw.WriteLine(TextBox5.Text)
sw.WriteLine(TextBox6.Text)
sw.WriteLine(TextBox7.Text)
sw.WriteLine(TextBox8.Text)
sw.WriteLine(TextBox9.Text)
sw.WriteLine(TextBox10.Text)
sw.WriteLine(TextBox11.Text)
sw.WriteLine(TextBox12.Text)
sw.WriteLine(TextBox13.Text)
sw.WriteLine(TextBox14.Text)
sw.WriteLine(TextBox15.Text)
sw.WriteLine(TextBox16.Text)
sw.WriteLine(TextBox17.Text)
' Close the stream writer
sw.Close()
' Destroy the stream writer object
sw = Nothing
End Sub
#End Region
#Region "Form Load()"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Disable the save & clear buttons,
' but enable the open button
EnableDisableButtons(False)
End Sub
#End Region
#Region "Enable Or Disable Buttons"
Private Sub EnableDisableButtons(ByVal bEnabled As Boolean)
' Open button (Opposite to the other buttons)
Button1.Enabled = Not bEnabled
' Save button (enable/disable)
Button2.Enabled = bEnabled
' Clear button (enable/disable)
Button3.Enabled = bEnabled
End Sub
#End Region
#Region "Clear Button"
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim obj As Object
' Iterate through objects on the form
For Each obj In Me.Controls
' If its a textbox then clear its contents
If TypeOf obj Is TextBox Then
obj.text = "" ' Can say 'obj.text = String.Empty' too
End If
Next
' Clear the filename from
' the strfilename variable
strFilename = String.Empty
' 'Set the focus to the open button
Button1.Focus()
End Sub
#End Region
#Region "Close Application"
Private Sub CloseApplication()
' Close the stream reader if its open
If Not sr Is Nothing Then sr.Close()
' Close the stream writer if its open
If Not sw Is Nothing Then sw.Close()
' Exit the application
Application.Exit()
End Sub
#End Region
#Region "Exit Button"
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
' Exit the application
CloseApplication()
End Sub
#End Region
#Region "Form Closing Event()"
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
' Exit the application
CloseApplication()
End Sub
#End Region
#Region "Textbox1 Got Focus Event()"
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
' Give the open button the focus
Button1.Focus()
End Sub
#End Region
End Class
--------------------------------------------------------
Its 4am here & I am off to bed once I have heard from you. Please don't keep
me waiting too long.
I hope this helps.