Hi @Burke, Brendan J ,
In order to read and write txt files, you need to learn to use StreamReader Class and StreamWriter Class.
Also learn about Form.FormClosing Event
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("Your file path", False, System.Text.Encoding.UTF8)
For Each worker In ComboBox1.Items
sw.WriteLine(worker.ToString)
Next
sw.Close()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim res = MessageBox.Show("Do you wish to update the worker.txt file?", "Warning", MessageBoxButtons.YesNoCancel)
If res = DialogResult.Yes Then
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("Your file path", False, System.Text.Encoding.UTF8)
For Each worker In ComboBox1.Items
sw.WriteLine(worker.ToString)
Next
sw.Close()
ElseIf res = DialogResult.Cancel Then
e.Cancel = True
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sr As System.IO.StreamReader = New System.IO.StreamReader("Your file path", System.Text.Encoding.UTF8)
Dim strs() As String = Split(sr.ReadToEnd, vbCrLf)
ComboBox1.Items.AddRange(strs)
sr.Close()
End Sub
Best Regards.
Jiachen Li
----------
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.