Question: VISUAL BASIC- create a program for managing a To Do list. The program will need to read and update a text file named ToDoList.txt. The
VISUAL BASIC-
create a program for managing a "To Do" list. The program will need to read and update a text file named ToDoList.txt. The record structure of the file must be: 1) sort order, 2) task name and 3) desired completion date.
When the program starts, it should read the contents file into a structure. The information should then be displayed in a data grid view. The data should be initially sorted by the sort order value. Afterwards, the user should be able to click on a column name to sort the data by that specific column.
The user should be able to add, update and delete tasks. The methods to implement these features are up to you. Please note that each value (sort order, task name and desired completion date) should be saved as a string value.
The program must use StreamReader and StreamWriter objects. The program must also contain exception handling for a missing input file. If the input file is not found, the user should be informed and still be allowed to use the program (i.e. do not automatically close the program).
THIS IS THE CODE I CURRENTLY HAVE, BUT I AM GETTING THE ERROR: "NO ROW CAN BE ADDED TO A DATAGRIDVIEW CONTROL THAT DOES NOT HAVE COLUMNS. COLUMNS MUST BE ADDED FIRST. HOW DO I GO ABOUT ADDING COLUMNS?
Imports System.IO Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try Using fileReader As StreamReader = New StreamReader("C:\Users\*****\Desktop\ToDoList.txt") 'provide correct file path here Dim fileLine As String = "" Dim index As Integer = 0 Dim temp(3) As String 'A temp array to store each row at a time (3 columns) fileLine = fileReader.ReadLine() While (fileLine <> Nothing) 'read file until the end of the file temp = fileLine.Split(CChar(",")) 'split the file line with ',' delimeter 'temp(0) contains Sorting Order, temp(1) contains Task Name, temp(2) contains Desired Date dgvOutput.Rows.Add(temp(0), temp(1), temp(2)) fileLine = fileReader.ReadLine() End While End Using
Catch err As Exception 'let the user know if any error/exception occurs MessageBox.Show("Error: " + err.Message) End Try End Sub
Private Sub saveList() Try Using fileWriter As StreamWriter = New StreamWriter("D:\ToDoList.txt") 'provide correct file path here For i As Integer = 0 To dgvOutput.Rows.Count - 1 Step +1 For j As Integer = 0 To dgvOutput.Columns.Count - 1 Step +1 fileWriter.Write(dgvOutput.Rows(i).Cells(j).Value.ToString + ",") Next fileWriter.WriteLine() Next fileWriter.Close() End Using
Catch err As Exception 'let the user know if any error/exception occurs MessageBox.Show("Error: " + err.Message) End Try End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click dgvOutput.Rows.Add("", "", "") End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click Dim RowIndex As Integer For i As Integer = 0 To dgvOutput.SelectedCells.Count - 1 RowIndex = dgvOutput.SelectedCells.Item(i).RowIndex Next
If RowIndex < 0 Then 'if row selected then alert the user MsgBox("Please select any row to delete !") Else dgvOutput.Rows.RemoveAt(RowIndex) MsgBox("Selected row has been deleted !") End If End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click saveList() MsgBox("Data saved to file successfully.") End Sub End Class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
