using Microsoft.Win32;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace C_Windows_1
{
    public partial class Form2 : Form
    {
        string datasource;
        string userid;
        string pwd;
        string dataname;
        //  public static Form2 A = null;
        public Form2()
        {
            InitializeComponent();


        }

        private void Form2_Load(object sender, EventArgs e)
        {
            ReadRegistryKey();
            success.Text = "";
            if (d4.Text == "")
            {
                d4.Text = "LeakTest";

            }

        }
        private void SetRegistryKey()
        {
            RegistryKey regName;
            //  RegistryKey regName2;
            //   RegistryKey regName3;
            //  RegistryKey regName4;

            regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18", true);
            if (regName is null)
            {
                regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18");
            }



            //  regName2 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User", true);
            //  regName3 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User", true);
            //   regName4 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User", true);


            //   if (regName2 is null)
            //   {
            //       regName2 = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User");
            //   }

            //   if (regName3 is null)
            //   {
            //       regName3 = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User");
            //   }
            //    if (regName4 is null)
            //    {
            //       regName4 = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User");
            //   }


            regName.SetValue("datasource", datasource);
            // regName.Close();
            regName.SetValue("userid", userid);
            // regName2.Close();
            regName.SetValue("pwd", pwd);
            //  regName3.Close();
            regName.SetValue("dataname", dataname);
            regName.Close();
        }






        private void ReadRegistryKey()
        {

            RegistryKey regName;
            //      RegistryKey regName2;
            //       RegistryKey regName3;
            //     RegistryKey regName4;

            regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18", true);
            //     regName2 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User", true);
            //     regName3 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User", true);
            //     regName4 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User", true);
            if (regName is null)
            {
                regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18");
            }
            if (regName.GetValue("datasource") is null)
            {

            }
            else
            {
                regName.OpenSubKey("User");
                string str1 = regName.GetValue("datasource").ToString();
                d1.Text = str1;
                //        regName.Close();

            }


            if (regName.GetValue("userid") is null)
            {

            }
            else
            {
                regName.OpenSubKey("User");
                string str2 = regName.GetValue("userid").ToString();
                d2.Text = str2;
                //        regName.Close();
            }

            if (regName.GetValue("pwd") is null)
            {

            }
            else
            {
                regName.OpenSubKey("User");
                string str3 = regName.GetValue("pwd").ToString();
                d3.Text = str3;
                //       regName.Close();
            }
            if (regName.GetValue("dataname") is null)
            {

            }
            else
            {
                regName.OpenSubKey("User");
                string str4 = regName.GetValue("dataname").ToString();
                d4.Text = str4;
                regName.Close();
            }


        }

        private void BtnKeep_Click(object sender, EventArgs e)
        {
            datasource = d1.Text;
            userid = d2.Text;
            pwd = d3.Text;
            dataname = d4.Text;
            SetRegistryKey();
            success.ForeColor = Color.Red;
            success.Text = "保存成功！";
            timer1.Interval = 3000;
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            success.Text = "";
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //  Form2 Form2 = new Form2();
            //   this.Visible =false ;
            e.Cancel = true;
            this.Hide();
            //    success.Text = "12345";
            //   Form1.button13.Enabled = true;

        }

        private void BtnRead2_Click(object sender, EventArgs e)
        {
            ReadRegistryKey();
        }
    }
}
