示例#1
0
        private void SaveTxt_Click(object sender, RoutedEventArgs e)
        {
            ReadWriteDialog rwd = new ReadWriteDialog();

            text.SelectAll();
            string luca = text.Selection.Text;

            rwd.WriteFileAs(luca);
        }
        /// <summary>
        /// Closes the window given a a property,
        /// asks if the text in textarea should be saved before closing,
        /// if the file already has a name WriteFile is called, if not WriteFileAs is called in
        /// ReadWriteDialog class.
        /// </summary>
        public bool Close() // in the end
        {
            window.text.SelectAll();
            string    whatToSave = window.text.Selection.Text;
            ReadWrite readWrite  = new ReadWrite();
            string    text       = readWrite.ReadFile(window.Title);
            bool      re         = false;

            //nothing to save
            if (window.text.IsEnabled == false || whatToSave.Length == 0 || whatToSave == text + "\r\n")
            {
                Environment.Exit(0);
            }
            else
            {
                DialogResult result = MessageBox.Show("Do you want to save?", "Exercise number 4", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Yes)
                {
                    if (window.Title == defaultfileName)
                    {
                        ReadWriteDialog.WriteFileAs(whatToSave);
                    }
                    else
                    {
                        ReadWriteDialog.WriteFile(fileName, whatToSave, false);
                    }
                }
                else if (result == DialogResult.No)
                {
                    Environment.Exit(0);
                }
                else
                {
                    return(re = true);
                }
            }
            return(re = false);
        }