using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace WinForms
{
    public partial class Configure : Form
    {
        public Configure()
        {
            InitializeComponent();
            ShowDatabase();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                SaveDatabase();
                MessageBox.Show("保存成功！");
                this.DialogResult = DialogResult.OK;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void ShowDatabase()
        {
            this.dbTextBox1.Text = ConfigurationManager.AppSettings["ConnIP"];
            this.dbTextBox2.Text = ConfigurationManager.AppSettings["ConnPort"];
            this.dbTextBox3.Text = ConfigurationManager.AppSettings["ConnName"];
            this.dbTextBox4.Text = ConfigurationManager.AppSettings["ConnUser"];
            this.dbTextBox5.Text = ConfigurationManager.AppSettings["ConnPWD"];
        }

        private void SaveDatabase()
        {
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configuration.AppSettings.Settings["ConnIP"].Value = this.dbTextBox1.Text.Trim();
            configuration.AppSettings.Settings["ConnPort"].Value = this.dbTextBox2.Text.Trim();
            configuration.AppSettings.Settings["ConnName"].Value = this.dbTextBox3.Text.Trim();
            configuration.AppSettings.Settings["ConnUser"].Value = this.dbTextBox4.Text.Trim();
            configuration.AppSettings.Settings["ConnPWD"].Value = this.dbTextBox5.Text.Trim();
            configuration.Save();
        }
    }
}
