private DialogResult SaveAs()
        {
            if (tabControl1.SelectedIndex == -1)
            {
                return(DialogResult.Cancel);
            }

            EditorControl tabControlsEditor = (EditorControl)tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Find("EDITORCONTROL", true)[0];

            SaveFileDialog sf = new SaveFileDialog();

            sf.Filter = "BasicScriptFile|*.bsl|Other (Everything)|*.*";
            DialogResult dr = sf.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                if (tabControlsEditor.SaveFile(sf.FileName) == 0)
                {
                    fileStatusLabel.Text = string.Format("Saved to {0} successfully!", Path.GetFileName(sf.FileName));
                    tabControl1.TabPages[tabControl1.SelectedIndex].Text = Path.GetFileName(sf.FileName);
                    this.Text = "BasicScriptingLanguage Editor - " + Path.GetFileName(sf.FileName);
                    return(DialogResult.OK);
                }
                else
                {
                    return(System.Windows.Forms.DialogResult.Cancel);
                }
            }
            else if (dr == System.Windows.Forms.DialogResult.Cancel)
            {
                return(DialogResult.Cancel);
            }

            return(DialogResult.Cancel);
        }
        //Test script
        private void menuItem22_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == -1)
            {
                MessageBox.Show("Please open a file.", "BasicScriptingLanguage Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EditorControl tabControlsEditor = (EditorControl)tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Find("EDITORCONTROL", true)[0];
            TestScript    ts;

            if (tabControlsEditor.CurrentFile != "New Document")
            {
                if (tabControlsEditor.HasChanges)
                {
                    tabControlsEditor.SaveFile(tabControlsEditor.CurrentFile);
                    fileStatusLabel.Text = string.Format("Saved to {0} successfully!", Path.GetFileName(tabControlsEditor.CurrentFile));
                    tabControl1.TabPages[tabControl1.SelectedIndex].Text = tabControl1.TabPages[tabControl1.SelectedIndex].Text.Trim('*');
                }

                ts = new TestScript(tabControlsEditor.CurrentFile);
                ts.ShowDialog();
            }
            else
            {
                DialogResult dr = MessageBox.Show("The current file must be saved to the disk before testing the script. Continue with saving?",
                                                  "BasicScriptingLanguage Editor",
                                                  MessageBoxButtons.YesNo,
                                                  MessageBoxIcon.Information);
                if (dr == System.Windows.Forms.DialogResult.Yes)
                {
                    SaveAs();
                    tabControlsEditor.SaveFile(tabControlsEditor.CurrentFile);
                    fileStatusLabel.Text = string.Format("Saved to {0} successfully!", Path.GetFileName(tabControlsEditor.CurrentFile));
                    tabControl1.TabPages[tabControl1.SelectedIndex].Text = tabControl1.TabPages[tabControl1.SelectedIndex].Text.Trim('*');
                    ts = new TestScript(tabControlsEditor.CurrentFile);
                    ts.ShowDialog();
                }
            }
        }
        private void Save()
        {
            if (tabControl1.SelectedIndex == -1)
            {
                return;
            }

            EditorControl tabControlsEditor = (EditorControl)tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Find("EDITORCONTROL", true)[0];

            if (File.Exists(tabControlsEditor.CurrentFile))
            {
                if (tabControlsEditor.SaveFile(tabControlsEditor.CurrentFile) == 0)
                {
                    fileStatusLabel.Text = string.Format("Saved to {0} successfully!", Path.GetFileName(tabControlsEditor.CurrentFile));
                    tabControl1.TabPages[tabControl1.SelectedIndex].Text = tabControl1.TabPages[tabControl1.SelectedIndex].Text.Trim('*');
                }
            }
            else
            {
                SaveAs();
            }
        }
        private void TabbedUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            cancelling = false;
            if (!cancelling)
            {
                int index = 0;
                foreach (TabPage page in tabControl1.TabPages)
                {
                    tabControl1.SelectedIndex = index;
                    foreach (Control control in page.Controls)
                    {
                        if (control.Name == "EDITORCONTROL")
                        {
                            EditorControl castAsEditor = (EditorControl)control;
                            if (castAsEditor.HasChanges == true)
                            {
                                DialogResult dr = MessageBox.Show(string.Format("{0} has unsaved changes. Would you like to save them?", Path.GetFileName(castAsEditor.CurrentFile)),
                                                                  "BasicScriptingLanguage Editor",
                                                                  MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                                if (dr == System.Windows.Forms.DialogResult.Yes)
                                {
                                    //save
                                    if (castAsEditor.CurrentFile == "New Document")
                                    {
                                        if (SaveAs() == DialogResult.Cancel)
                                        {
                                            e.Cancel = true;
                                        }
                                        else
                                        {
                                            e.Cancel = false;
                                        }
                                    }
                                    else
                                    {
                                        if (File.Exists(castAsEditor.CurrentFile))
                                        {
                                            castAsEditor.SaveFile(castAsEditor.CurrentFile);
                                            e.Cancel = false;
                                        }
                                        else
                                        if (SaveAs() == DialogResult.Cancel)
                                        {
                                            e.Cancel = true;
                                        }
                                        else
                                        {
                                            e.Cancel = false;
                                        }
                                    }
                                }
                                else if (dr == System.Windows.Forms.DialogResult.No)
                                {
                                    e.Cancel = false;
                                }
                                else if (dr == System.Windows.Forms.DialogResult.Cancel)
                                {
                                    e.Cancel   = true;
                                    cancelling = true;
                                }
                            }
                            //
                        }
                        //
                    }
                    index++;
                }
            }
            int           index2            = 0;
            List <string> FilesToBeReopened = new List <string>();

            foreach (var page in tabControl1.TabPages)
            {
                tabControl1.SelectedIndex = index2;
                EditorControl tabControlsEditor = (EditorControl)tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Find("EDITORCONTROL", true)[0];

                if (tabControlsEditor.CurrentFile != "New Document")
                {
                    FilesToBeReopened.Add(tabControlsEditor.CurrentFile);
                }

                index2++;
            }
            string built = "";

            for (int i = 0; i < FilesToBeReopened.Count(); i++)
            {
                if (i == (FilesToBeReopened.Count() - 1))
                {
                    built += "\"" + FilesToBeReopened[i] + "\"";
                }
                else
                {
                    built += "\"" + FilesToBeReopened[i] + "\",";
                }
            }
            if (built != "")
            {
                MainSettingsManager.MainIniFile.WriteValue("Settings", "AlreadyOpenFiles", built);
            }
            //
        }
        //close tab
        private void menuItem18_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != -1)
            {
                int newSelected = tabControl1.SelectedIndex - 1;

                if (newSelected == -1)
                {
                    newSelected = 0;
                }

                EditorControl editor       = (EditorControl)tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Find("EDITORCONTROL", false)[0];
                bool          isCancelling = false;

                if (editor.HasChanges)
                {
                    DialogResult dr = MessageBox.Show(Path.GetFileName(editor.CurrentFile) + " has unsaved changes, do you wish to save them before exiting?",
                                                      "BasicScriptingLanguage Editor",
                                                      MessageBoxButtons.YesNoCancel,
                                                      MessageBoxIcon.Information);
                    if (dr == System.Windows.Forms.DialogResult.Yes)
                    {
                        if (editor.CurrentFile == "New Document")
                        {
                            if (SaveAs() == System.Windows.Forms.DialogResult.Cancel)
                            {
                                isCancelling = true;
                            }
                            else
                            {
                                isCancelling = false;
                            }
                        }
                        else
                        {
                            if (File.Exists(editor.CurrentFile))
                            {
                                editor.SaveFile(editor.CurrentFile);
                            }
                        }
                    }
                    else if (dr == System.Windows.Forms.DialogResult.No)
                    {
                        isCancelling = false;
                    }
                    else if (dr == System.Windows.Forms.DialogResult.Cancel)
                    {
                        isCancelling = true;
                    }
                    ;
                }

                if (!isCancelling)
                {
                    tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
                    try
                    {
                        tabControl1.SelectedIndex = newSelected;
                    }
                    catch
                    { /*nothing*/ }
                }

                if (tabControl1.TabPages.Count == 0)
                {
                    tabControl1.Visible = false;
                }
            }
        }