Battle_Script_Pro.Form1.CloseScript C# (CSharp) Method

CloseScript() private method

private CloseScript ( ) : void
return void
        private void CloseScript()
        {
            if (tabControl1.TabPages.Count > 1)
            {
                int toRemove = tabControl1.SelectedIndex;
                bool continueRoutine = true;
                if (unsaved[toRemove])
                {
                    DialogResult result = MessageBox.Show("Your script \"" + tabControl1.TabPages[toRemove].Text + "\" is unsaved. Would you like to save it before closing?",
            "Save?",
            MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    switch (result)
                    {
                        case DialogResult.Yes:
                            result = saveFileDialog1.ShowDialog();
                            if (result == DialogResult.OK)
                            {
                                try
                                {
                                    File.WriteAllLines(saveFileDialog1.FileName, scripts[tabControl1.SelectedIndex].Lines.ToArray());
                                }
                                catch
                                {
                                    MessageBox.Show("The file could not be saved. This programme will be kept open.");
                                    continueRoutine = false;
                                }
                            }
                            else
                            {
                                MessageBox.Show("The file could not be saved. This programme will be kept open.");
                                continueRoutine = false;
                            }
                            break;
                        case DialogResult.No:
                            break;
                        default:
                            continueRoutine = false;
                            break;
                    }
                }
                if (continueRoutine)
                {
                    int value = 0;
                    if (tabControl1.TabPages[tabControl1.SelectedIndex].Text.StartsWith("Script ") && Int32.TryParse(tabControl1.TabPages[tabControl1.SelectedIndex].Text.Split(' ')[1], out value))
                    {
                        newScriptNames.Remove(value);
                    }
                    tabControl1.SelectedIndex = 0;
                    tabControl1.TabPages.RemoveAt(toRemove);
                    scripts.RemoveAt(toRemove);
                    unsaved.RemoveAt(toRemove);
                    currentlyOpenBS.RemoveAt(toRemove);
                }
            }
            else
            {
                bool continueRoutine = true;
                if (unsaved[0])
                {
                    DialogResult result = MessageBox.Show("Your script \"" + tabControl1.TabPages[0].Text + "\" is unsaved. Would you like to save it before closing?",
            "Save?",
            MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    switch (result)
                    {
                        case DialogResult.Yes:
                            result = saveFileDialog1.ShowDialog();
                            if (result == DialogResult.OK)
                            {
                                try
                                {
                                    File.WriteAllLines(saveFileDialog1.FileName, scripts[tabControl1.SelectedIndex].Lines.ToArray());
                                }
                                catch
                                {
                                    MessageBox.Show("The file could not be saved. This programme will be kept open.");
                                    continueRoutine = false;
                                }
                            }
                            else
                            {
                                MessageBox.Show("The file could not be saved. This programme will be kept open.");
                                continueRoutine = false;
                            }
                            break;
                        case DialogResult.No:
                            break;
                        default:
                            continueRoutine = false;
                            break;
                    }
                }
                if (continueRoutine)
                {
                    scripts[0].Clear();
                    currentlyOpenBS[0] = null;
                    tabControl1.TabPages[0].Text = "Script 1";
                    unsaved[0] = true;
                }
            }
        }
Form1