Menu Items has ONLY: New, Open, Save, Save As and Exit...nothing else
i dont know how to do:
-Create a member boolean variable to track when the contents of the textbox have changed. This boolean variable will be used to prevent the contents of the textbox from being lost, after changes have been made, when the user attempts to create a new file, or open a file.
-When the form is closing, check the boolean variable to determine if the contents of the textbox have been changed without being saved.
-Use a MessageBox to ask the user if they wish to discard the changes and exit, or to cancel the exit. If the user chooses to cancel the exit, prevent the form from closing.
-Menu Items Open : Check to determine if the contents of the textbox have been changed. If they have, use a MessageBox to ask the user if they wish to discard the changes. If the user disagrees, return. If the user agrees, use the OpenFileDialog to select a file. If the user cancels or closes the OpenFileDialog, return without reading in a file. Otherwise, read in the text file into the textbox, handling any exceptions by displaying a MessageBox. Update the form caption to include the file name of the new file, without the path.

private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtMain.Clear();
        }
 
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Shows the openFileDialog
            openFileDialog1.ShowDialog();
            //Reads the text file
            System.IO.StreamReader OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
            //Displays the text file in the textBox
            txtMain.Text = OpenFile.ReadToEnd();
            //Closes the proccess
            OpenFile.Close();
        }
 
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Shows the openFileDialog
            openFileDialog1.ShowDialog();
            //Reads the text file
            System.IO.StreamReader OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
            //Displays the text file in the textBox
            txtMain.Text = OpenFile.ReadToEnd();
            //Closes the proccess
            OpenFile.Close();
        }
 
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Open the saveFileDialog
            saveFileDialog1.ShowDialog();
            //Determines the text file to save to
            System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
            //Writes the text to the file
            SaveFile.WriteLine(txtMain.Text);
            //Closes the proccess
            SaveFile.Close();
        }
 
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();

Menu Items has ONLY: New, Open, Save, Save As and Exit...nothing else
i dont know how to do:
-Create a member boolean variable to track when the contents of the textbox have changed. This boolean variable will be used to prevent the contents of the textbox from being lost, after changes have been made, when the user attempts to create a new file, or open a file.
-When the form is closing, check the boolean variable to determine if the contents of the textbox have been changed without being saved.
-Use a MessageBox to ask the user if they wish to discard the changes and exit, or to cancel the exit. If the user chooses to cancel the exit, prevent the form from closing.
-Menu Items Open : Check to determine if the contents of the textbox have been changed. If they have, use a MessageBox to ask the user if they wish to discard the changes. If the user disagrees, return. If the user agrees, use the OpenFileDialog to select a file. If the user cancels or closes the OpenFileDialog, return without reading in a file. Otherwise, read in the text file into the textbox, handling any exceptions by displaying a MessageBox. Update the form caption to include the file name of the new file, without the path.

private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtMain.Clear();
        }
 
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Shows the openFileDialog
            openFileDialog1.ShowDialog();
            //Reads the text file
            System.IO.StreamReader OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
            //Displays the text file in the textBox
            txtMain.Text = OpenFile.ReadToEnd();
            //Closes the proccess
            OpenFile.Close();
        }
 
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Shows the openFileDialog
            openFileDialog1.ShowDialog();
            //Reads the text file
            System.IO.StreamReader OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
            //Displays the text file in the textBox
            txtMain.Text = OpenFile.ReadToEnd();
            //Closes the proccess
            OpenFile.Close();
        }
 
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Open the saveFileDialog
            saveFileDialog1.ShowDialog();
            //Determines the text file to save to
            System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
            //Writes the text to the file
            SaveFile.WriteLine(txtMain.Text);
            //Closes the proccess
            SaveFile.Close();
        }
 
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();

All you need to use form event i.e. "Form_Closing" to validate user input.

Whenever you use this.close method of form, above event occurs at last. This event you can use to check user options wheather he want to stay or leave form.

Refer below links:
http://www.codeproject.com/KB/cs/mishramynotepad.aspx
http://www.codeproject.com/KB/cs/notepad.aspx

This article has been dead for over six months. Start a new discussion instead.