using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace todolist { public partial class Form1 : Form { String currentFileName = "Untitled"; Boolean changed = false; public Form1() { InitializeComponent(); } private void mnuNew_Click(object sender, EventArgs e) { // It is possible that list has changed since last save: // If there are no changes we do not cancel. Boolean canceled = false; if (changed) { // AskToSave returns true if they select cancel. // Or they select Yes or No, canceled = AskToSave(); } if (!canceled) { // Clear all items from list box, set name to Untitled. lstData.Items.Clear(); currentFileName = "Untitled"; Text = currentFileName; } } private void mnuExit_Click(object sender, EventArgs e) { // Ask if they want to save. it returns true if the user chose to cancel. Boolean cancel=AskToSave(); if (!cancel) Close(); } private void mnuOpen_Click(object sender, EventArgs e) { Boolean canceled = false; if (changed) { // AskToSave returns true if they select cancel. canceled = AskToSave(); } if (!canceled) { // Delete all items in list box before reading new file. lstData.Items.Clear(); readfile(); } } private void mnuSave_Click(object sender, EventArgs e) { writefile(); } private void mnuDelete_Click(object sender, EventArgs e) { // Remove the selected item from the list box. lstData.Items.Remove(lstData.SelectedItem); changed = true; } private void mnuAdd_Click(object sender, EventArgs e) { // Display the input box form to let the user type in a new item. InputBox box = new InputBox(); box.ShowDialog(); TextBox txt = box.getTextBox(); // This gives us accfess to the text box on the form. if (txt.Text != "") { // If a new item was typed, add it to list box. lstData.Items.Add(txt.Text); changed = true; } } private void mnuEdit_Click(object sender, EventArgs e) { // Display the input box form to let the user edit the current item. InputBox box = new InputBox(); TextBox txt = box.getTextBox(); // Put the current item into the text box to edit. txt.Text = lstData.Text; box.ShowDialog(); // Save the current index of list box, so that old item // can be deleted and new item inserted in the same position. int pos = lstData.SelectedIndex; lstData.Items.Remove(lstData.SelectedItem); // If they changed the text to "", assume it is to be deleted. if (txt.Text != "") lstData.Items.Insert(pos, txt.Text); changed = true; } private void btnUp_Click(object sender, EventArgs e) { // Change the position by -1 example: Move from position 3 to position 2 MoveItem(-1); } private void btnDown_Click(object sender, EventArgs e) { // Change the position by 1 example: Move from position 2 to position 3 MoveItem(1); } public void MoveItem(int direction) { Boolean ok = true; // Checking selected item if (lstData.SelectedItem == null || lstData.SelectedIndex < 0) ok = false; // No selected item - nothing to do // Calculate new index using move direction int newIndex = lstData.SelectedIndex + direction; // Checking bounds of the range if (newIndex < 0 || newIndex >= lstData.Items.Count) ok = false; // Index out of range - nothing to do if (ok) { // None of the errors above occured, move the item. object selected = lstData.SelectedItem; // Removing removable element lstData.Items.Remove(selected); // Insert it in new position lstData.Items.Insert(newIndex, selected); // Restore selection lstData.SetSelected(newIndex, true); changed = true; } } private void readfile() { // Set the filter to show text files or all files. openFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"; openFileDialog1.ShowDialog(); if (openFileDialog1.FileName != "") { currentFileName = openFileDialog1.FileName; this.Text = currentFileName; // Read the entire file into an array. string[] lines = System.IO.File.ReadAllLines(openFileDialog1.FileName); // Add each line to the list box. foreach (string line in lines) { lstData.Items.Add(line); } // The file has just been read, so there are no changes yet. changed = false; } } private void writefile() { // Filename is "Untitled" initially, on mnuNew and mnuSaveAs. if (currentFileName == "Untitled") { saveFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"; saveFileDialog1.ShowDialog(); // Filename is "" if cancel selected. if (saveFileDialog1.FileName != "") { currentFileName = saveFileDialog1.FileName; Text = currentFileName; } } if (currentFileName!="Untitled") { using (System.IO.StreamWriter file = new System.IO.StreamWriter(currentFileName)) { // Write each item in list box to file. foreach (string item in lstData.Items) { file.WriteLine(item); } file.Close(); // We just saves, so no changes since last save. changed = false; } } } private void Form1_Resize(object sender, EventArgs e) { // Make list box fill form from top left corner. lstData.Width = this.Width - lstData.Location.X; lstData.Height = this.Height - lstData.Location.Y; } private void mnuSaveAs_Click(object sender, EventArgs e) { // Save the current file name in case the user selects cancel. String file = currentFileName; // Set to "Untitled" to signal writefile to ask for filename. currentFileName = "Untitled"; writefile(); // Restore filename if user cancelled. if (currentFileName == "Untitled") currentFileName = file; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Ask if they want to save. it returns true if the user chose to cancel. // The FormClosingEventArgs e has a cancel property. Setting it to true cancels the close event. e.Cancel = AskToSave(); } private Boolean AskToSave() { // If there is no change to list don't ask, don't save, and don't cancel DialogResult result= DialogResult.No; if (changed) { result = MessageBox.Show("Do you want to save changes to " + currentFileName + "?", "Closing", MessageBoxButtons.YesNoCancel); // If they answer yes, write the file if (result == DialogResult.Yes) writefile(); } // Returns true if they selected cancel. // Returns false if there has been no change. return (result== DialogResult.Cancel); } } }