BF2Statistics.DatabaseConfigForm.NextBtn_Click C# (CSharp) Method

NextBtn_Click() private method

Event fired when the next button is clicked
private NextBtn_Click ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private async void NextBtn_Click(object sender, EventArgs e)
        {
            // Disable this form
            this.Enabled = false;
            string Message1 = "";

            // Initiate the Task Form
            if (TypeSelect.SelectedIndex == 1)
            {
                TaskForm.Show(this, "Create Database", "Connecting to MySQL Database...", false);
                Message1 = "Successfully Connected to MySQL Database! We will attempt to create the necessary tables into the specified database. Continue?";
            }
            else
            {
                TaskForm.Show(this, "Create Database", "Creating SQLite Database...", false);
                Message1 = "Successfully Created the SQLite Database! We will attempt to create the necessary tables into the specified database. Continue?";
            }

            // Temporarily set settings
            if (!SetConfigSettings())
            {
                this.Enabled = true;
                TaskForm.CloseForm();
                return;
            }

            // Try and install the SQL
            try
            {
                bool PreviousInstall = true;

                // Run in a seperate thread, dont wanna lock up the GUI thread
                await Task.Run(() =>
                {
                    // Switch mode
                    if (DbMode == DatabaseMode.Stats)
                    {
                        // Open up the database connetion
                        using (StatsDatabase Db = new StatsDatabase())
                        {
                            // We only have work to do if the tables are not installed
                            if (!Db.TablesExist)
                            {
                                PreviousInstall = false;

                                // Verify that the user wants to install DB tables
                                DialogResult Res = MessageBox.Show(Message1, "Verify Installation", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000 // Force window on top
                                );

                                // If we dont want to install tables, back out!
                                if (Res == DialogResult.No) return;

                                // Create our tables
                                TaskForm.Progress.Report(new TaskProgressUpdate("Creating Stats Tables"));
                                Db.CreateSqlTables(TaskForm.Progress);
                            }
                        }
                    }
                    else // Gamespy Mode
                    {
                        // Open up the database connetion
                        using (GamespyDatabase Db = new GamespyDatabase())
                        {
                            // We only have work to do if the tables are not installed
                            if (!Db.TablesExist)
                            {
                                PreviousInstall = false;

                                // Verify that the user wants to install DB tables
                                DialogResult Res = MessageBox.Show(Message1, "Verify Installation", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000 // Force window on top
                                );

                                // If we dont want to install tables, back out!
                                if (Res == DialogResult.No)  return;

                                // Create our tables
                                TaskForm.Progress.Report(new TaskProgressUpdate("Creating Gamespy Tables"));
                                Db.CreateSqlTables();
                            }
                        }
                    }
                });

                // No errors, so save the config file
                Program.Config.Save();

                // Close the task form
                TaskForm.CloseForm();

                // Show Success Form
                if (!PreviousInstall)
                    MessageBox.Show("Successfully installed the database tables!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000 // Force window on top
                    );
                else
                    MessageBox.Show(
                        "We've detected that the database was already installed here. Your database settings have been saved and no further setup is required.",
                        "Existing Installation Found", MessageBoxButtons.OK, MessageBoxIcon.Information,
                            MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000 // Force window on top
                        );

                // Close this form, as we are done now
                this.Close();
            }
            catch (Exception Ex)
            {
                // Close the task form and re-enable this form
                TaskForm.CloseForm();
                this.Enabled = true;

                // Revert the temporary config settings and show the error to the user
                Program.Config.Reload();
                MessageBox.Show(Ex.Message, "Database Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000 // Force window on top
                );
                return;
            }
            finally
            {
                if (TaskForm.IsOpen)
                    TaskForm.CloseForm();

                this.Enabled = true;
            }
        }
    }