//private bool FindHandlesDone = false;
        //public void FindHandles()
        //{
        //     //******5-30 this might be problem, maybe this should just be done once...not sure if
        //    //finding the loadscript though. consider doing seperate for the 2 neb windows.  one
        //    //with (spawned)
        //    try
        //    {
        //        MainWindowName = "Nebulosity";
        //        IntPtr hWnd2 = SearchForWindow("wxWindow", MainWindowName);
        //        Log("Neb Handle Found -- " + hWnd2.ToInt32());
        //        NebhWnd = hWnd2.ToInt32();
        //        if (NebhWnd != 0)
        //        {
        //            string NebVersion;
        //            //finds the Neb version (the number after the "v")
        //            StringBuilder sb = new StringBuilder(1024);
        //            SendMessage(NebhWnd, WM_GETTEXT, 1024, sb);
        //            //  GetWindowText(camera, sb, sb.Capacity);
        //            Log(sb.ToString());
        //            NebVersion = sb.ToString();
        //            int NebVposNumber = NebVersion.IndexOf("v");
        //            string NebVNumberAfter = NebVersion.Substring(NebVposNumber + 1, 1);
        //            NebVNumber = Convert.ToInt32(NebVNumberAfter);
        //        }
        //        if (checkBox14.Checked == false)//don't need for slave mode
        //        {
        //            IntPtr PHDhwnd2 = SearchForWindow("wxWindow", "PHD");
        //            Log("PHD Handle Found --  " + PHDhwnd2.ToInt32());
        //            PHDhwnd = PHDhwnd2.ToInt32();
        //        }
        //        // PHDhwnd = FindWindow(null, "PHD Guiding 1.13.0b  -  www.stark-labs.com (Log active)");
        //        LoadScripthwnd = FindWindow(null, "Load script");
        //        //   Log("PHD " + PHDhwnd.ToString());
        //        //   Log("Load script " + LoadScripthwnd.ToString());
        //    }
        //    catch (Exception e)
        //    {
        //        Log("FindHandles Error" + e.ToString());
        //        Send("FindHandles Error" + e.ToString());
        //        FileLog("FindHandles Error" + e.ToString());
        //    }
        //}
        //  double CalibrationTol;//may not need this variable...in equation uses textbox value and convert to doulg
        //Loadhere
        private void MainWindow_Load_1(object sender, EventArgs e)
        {
            try
            {

                //5 lines below for path2 settings
                GlobalVariables.Path2 = WindowsFormsApplication1.Properties.Settings.Default.path.ToString();
                textBox11.Text = GlobalVariables.Path2.ToString();
                GlobalVariables.NebPath = WindowsFormsApplication1.Properties.Settings.Default.NebPath.ToString();
                textBox35.Text = GlobalVariables.NebPath.ToString();
                // send = NebPath + ScriptName;
                // textBox37.Text = send.ToString();
                toolStripStatusLabel5.Text = "Path: " + GlobalVariables.Path2.ToString();
                //     CalibrationTol = Convert.ToDouble(textBox59.Text);
                textBox24.Text = PublishVersion.ToString();//version number on about tab
                //************ try addition for new equipcomboxes
                if (WindowsFormsApplication1.Properties.Settings.Default.ComboItems7 == null)
                {
                    WindowsFormsApplication1.Properties.Settings.Default.ComboItems7 = new System.Collections.Specialized.StringCollection();
                }

                foreach (string item in WindowsFormsApplication1.Properties.Settings.Default.ComboItems7)
                {
                    comboBox7.Items.Add(item);
                    if (comboBox7.Items.Count == 1)
                        comboBox7.SelectedIndex = 0;
                }
                if (WindowsFormsApplication1.Properties.Settings.Default.ComboItems8 == null)
                {
                    WindowsFormsApplication1.Properties.Settings.Default.ComboItems8 = new System.Collections.Specialized.StringCollection();
                }
                /*
                foreach (string item in WindowsFormsApplication1.Properties.Settings.Default.ComboItems8)
                {
                    comboBox8.Items.Add(item);
                }
                 */
                backlash = WindowsFormsApplication1.Properties.Settings.Default.backlash;
                textBox8.Text = backlash.ToString();
                Handles H = new Handles();
                Callback myCallBack = new Callback(H.EnumChildGetValue);

                H.FindHandles();
                //   int hWnd;

                if (Handles.PHDhwnd == 0)//this can really just serve as reminded that PHD is needed for some functions...not necessary for focusing.
                {
                    DialogResult result1;
                    result1 = MessageBox.Show("PHD Not Found - Open and 'Retry', 'Ignore' or 'Abort' to Close", "scopefocus",
                         MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation);

                    if (result1 == DialogResult.Ignore)
                        this.Refresh();
                    if (result1 == DialogResult.Retry)
                    {
                        H.FindHandles();
                        this.Refresh();
                    }
                    if (result1 == DialogResult.Abort)
                        System.Environment.Exit(0);
                }
                else
                    Log("PHD version " + Handles.PHDVNumber.ToString());

                if (Handles.NebhWnd == 0)
                {
                    DialogResult result;
                    result = MessageBox.Show("Nebulosity Not Found - Open or Close & Restart and hit 'Retry' or 'Ignore' to continue",
                       "scopefocus", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation);//change so ok moves focus
                    if (result == DialogResult.Ignore)
                        this.Refresh();
                    if (result == DialogResult.Retry)
                    {
                        H.FindHandles();
                        this.Refresh();
                    }
                    if (result == DialogResult.Abort)
                        System.Environment.Exit(0);

                }
                else
                {

                    EnumChildWindows(Handles.NebhWnd, myCallBack, 0);
                    Log("Nebulosity Version " + Handles.NebVNumber.ToString());
                }
                if (Handles.NebhWnd != 0)
                    FindNebCamera();

                radioButton8.Checked = true;
                textBox27.Text = user.ToString();
                textBox13.Text = server.ToString();
                textBox28.Text = pswd.ToString();
                textBox36.Text = to.ToString();
                fileSystemWatcher1.Path = GlobalVariables.Path2.ToString();
                fileSystemWatcher2.Path = GlobalVariables.Path2.ToString();
                fileSystemWatcher5.Path = GlobalVariables.Path2.ToString();//added to test metricHFR
                fileSystemWatcher3.Path = GlobalVariables.Path2.ToString();
                fileSystemWatcher4.Path = GlobalVariables.Path2.ToString();
                //   travel = WindowsFormsApplication1.Properties.Settings.Default.travel;
                textBox2.Text = travel.ToString();
                textBox60.Text = WindowsFormsApplication1.Properties.Settings.Default.sigma.ToString();
                textBox49.Text = WindowsFormsApplication1.Properties.Settings.Default.DwnSz.ToString();
                textBox61.Text = WindowsFormsApplication1.Properties.Settings.Default.Low.ToString();
                textBox62.Text = WindowsFormsApplication1.Properties.Settings.Default.High.ToString();
                //   camera = WindowsFormsApplication1.Properties.Settings.Default.camera;
                //   textBox22.Text = camera;
                posMin = WindowsFormsApplication1.Properties.Settings.Default.focuspos;
                textBox4.Text = posMin.ToString();

                numericUpDown40.Value = WindowsFormsApplication1.Properties.Settings.Default.stepsize;
                numericUpDown2.Value = WindowsFormsApplication1.Properties.Settings.Default.stepsize * 4;//coarse V
                numericUpDown8.Value = WindowsFormsApplication1.Properties.Settings.Default.stepsize;//fine V
                numericUpDown39.Value = WindowsFormsApplication1.Properties.Settings.Default.stepsize * 3;//gotofocus sample pos
                //first time load both are false which if not set gives tab change error.
                if (WindowsFormsApplication1.Properties.Settings.Default.None == false && WindowsFormsApplication1.Properties.Settings.Default.AO == false)
                    radioButton8.Checked = true;
                else
                    radioButton8.Checked = WindowsFormsApplication1.Properties.Settings.Default.None;

                radioButton9.Checked = WindowsFormsApplication1.Properties.Settings.Default.Extender;
                radioButton10.Checked = WindowsFormsApplication1.Properties.Settings.Default.Reducer;
                radioButton4.Checked = WindowsFormsApplication1.Properties.Settings.Default.AO;

                setarraysize();

                //  Data d = new Data();
                FillData();

                toolStripStatusLabel1.Text = "Startup Complete";
                textBox44.Text = trackBar1.Value.ToString();
                toolStripStatusLabel4.Text = "No Filter";
                toolStripStatusLabel2.Text = "Idle";
                toolStripStatusLabel3.Text = "Equip";
                if (!Directory.Exists(@"C:\cygwin\home\astro"))
                    {
                    radioButton5_astrometry.Checked = true;
                    radioButton_local.Enabled = false;
                    }

                GlobalVariables.LocalPlateSolve = radioButton_local.Checked;

            }
            catch (Exception ex)
            {
                Log("MainWindow_Load Error" + ex.ToString());
            }
        }
            // public int Serverhwnd;
            //enable slave mode
            private void checkBox14_CheckedChanged_1(object sender, EventArgs e)
            {
            Handles H = new Handles();
            if (checkBox14.Checked == true)
            {
                if (textBox37.Text == "")
                {
                    MessageBox.Show("Capture time cannot be zero", "scopefocus");
                    checkBox14.Checked = false;
                    return;
                }
                // MainWindowName = "Nebulosity v3.0 - slave.fit";//change the mainwindow label in neb to signify sencond copy
                MainWindowName = "Nebulosity(spawned) v3.0";//change the mainwindow label in neb to signify sencond copy
                SocketPort = 4302; // was SocketPort2????
                ScriptName = "\\listenPort2.neb";
                this.Text = "scopefocus - slave mode";
                groupBox15.Enabled = false;
                groupBox13.Enabled = false;
                groupBox12.Enabled = false;
                groupBox6.Enabled = false;
                groupBox5.Enabled = false;
                checkBox8.Enabled = false;
                button40.Enabled = false;
                Callback myCallBack3 = new Callback(H.EnumChildGetValue);

                //   int hWnd;
                IntPtr hWnd2 = Handles.SearchForWindow("wxWindow", MainWindowName);//must open file named test.fit to specifiy second copu
                Log("Neb(spawned) handle -- " + hWnd2.ToInt32());
                Handles.NebhWnd = hWnd2.ToInt32();
                // FindHandles();
                if (Handles.NebhWnd == 0)
                {
                    DialogResult result;
                    result = MessageBox.Show("Nebulosity slave Not Found - Open or Close & Restart and hit 'Retry' or 'Ignore' to continue",
                       "scopefocus", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation);//change so ok moves focus
                    if (result == DialogResult.Ignore)
                        this.Refresh();
                    if (result == DialogResult.Retry)
                    {
                        H.FindHandles();
                        this.Refresh();
                    }
                    if (result == DialogResult.Abort)
                        System.Environment.Exit(0);

                }
                else
                {

                    EnumChildWindows(Handles.NebhWnd, myCallBack3, 0);
                }
                //  FindNebCamera();
                //IntPtr ServerhwndPtr = Handles.SearchForWindow("WindowsForms10", "scopefocus - Main");
                //Log("scopefocus-server handle found --  " + ServerhwndPtr.ToInt32());
                //Serverhwnd = ServerhwndPtr.ToInt32();
                //  this.Refresh();
            }
            else
            {
                SocketPort = 4301;
                ScriptName = "\\listenPort.neb";
            }

            }
 private void SendtoServer(string message)
 {
 Handles H = new Handles();
 StringBuilder sb = new StringBuilder(message);
 ShowWindowAsync(H.Serverhwnd(), SW_SHOW);
 Thread.Sleep(200);
 SetForegroundWindow(H.Serverhwnd());
 SendMessage(ServerStatusHandle, WM_SETTEXT, 0, sb);
 }
            //    int SocketPort2;
            //enable server
            private void checkBox20_CheckedChanged_1(object sender, EventArgs e)
            {
            Handles H = new Handles();
            //  groupBox18.Enabled = false;
            button45.Text = "S_Capture";
            button46.Text = "S_Pause";
            button47.Text = "S_Flat";
            button39.Text = "S_GotoFocus";
            numericUpDown37.Enabled = false;
            // numericUpDown23.Enabled = false;
            textBox37.Enabled = false;
            textBox40.Enabled = false;
            numericUpDown33.Enabled = false;
            numericUpDown35.Enabled = false;

            /*
            if (checkBox14.Checked == true)
            {
                checkBox14.Checked = false;//need to uncheck slave
                //all this needed only if changing FROM slave mode
                MainWindowName = "Nebulosity";//change the mainwindow label in neb to signify sencond copy
                SocketPort = 4301;
                ScriptName = "\\listenPort.neb";
                this.Text = "scopefocus";
                groupBox15.Enabled = true;
                groupBox13.Enabled = true;
                groupBox12.Enabled = true;
                groupBox6.Enabled = true;
                groupBox5.Enabled = true;
                checkBox8.Enabled = true;
                Callback myCallBack = new Callback(EnumChildGetValue);
                FindHandles();
                //   int hWnd;

                if (NebhWnd == 0)
                {
                    DialogResult result;
                    result = MessageBox.Show("Nebulosity Not Found - Open or Close & Restart and hit 'Retry' or 'Ignore' to continue",
                       "scopefocus", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation);//change so ok moves focus
                    if (result == DialogResult.Ignore)
                        this.Refresh();
                    if (result == DialogResult.Retry)
                    {
                        FindHandles();
                        this.Refresh();
                    }
                    if (result == DialogResult.Abort)
                        System.Environment.Exit(0);

                }
                else
                {

                    EnumChildWindows(NebhWnd, myCallBack, 0);
                }

                FindNebCamera();

                this.Refresh();
            }
             */

            //  SearchData sd = new SearchData { Wndclass = "WindowsForms10", Title = "scopefocus" };
            IntPtr SlavehwndPtr = Handles.SearchForWindow("WindowsForms10", "scopefocus - slave mode");
            Log("scopefocus-slave handle found --  " + SlavehwndPtr.ToInt32());
            Slavehwnd = SlavehwndPtr.ToInt32();
            //full class name   WindowsForms10.Window.8.app.0.201d787_r15_ad1
            if (Slavehwnd != 0)
            {
                Callback myCallBack2 = new Callback(H.EnumChildGetValue);
                EnumChildWindows(Slavehwnd, myCallBack2, 0);//this gets the slave capture, pause, flat and gotofocus buttons
                //this sends the server status box handle to the slave textbox
                string send = textBox41.Handle.ToString();
                StringBuilder sb = new StringBuilder(send);
                SendMessage(Handles.SlaveStatushwnd, WM_SETTEXT, 0, sb);
            }

            //make sure it's not the spawned copy

            MainWindowName = "Nebulosity v3.0";//change the mainwindow label in neb to signify sencond copy
            IntPtr hWnd2 = Handles.SearchForWindow("wxWindow", MainWindowName);//must open file named test.fit to specifiy second copu
            Log("Ensure not slave handle -- " + hWnd2.ToInt32());
            Handles.NebhWnd = hWnd2.ToInt32();
            /*
            Callback myCallBack3 = new Callback(EnumChildGetValue);
            EnumChildWindows(NebhWnd2, myCallBack3, 2);
            Log("Slave Handles-- Abort2: " + Aborthwnd2.ToString() + "Frame" + Framehwnd2.ToString());

            SocketPort2 = 4302;
                ScriptName = "\\listenPort2.neb";

                Callback myCallBack = new Callback(EnumChildGetValue);
              //  FindHandles();
                //   int hWnd;

                if (NebhWnd2 == 0)
                {
                    DialogResult result;
                    result = MessageBox.Show("Nebulosity-slave  Not Found - Open or Close & Restart and hit 'Retry' or 'Ignore' to continue",
                       "scopefocus", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation);//change so ok moves focus
                    if (result == DialogResult.Ignore)
                        this.Refresh();
                    if (result == DialogResult.Retry)
                    {
                        IntPtr hWnd3 = SearchForWindow("wxWindow", MainWindowName);//must open file named test.fit to specifiy second copu
                        Log("Neb-slave Handle Found -- " + hWnd3.ToInt32());
                        NebhWnd2 = hWnd2.ToInt32();
                        this.Refresh();
                    }
                    if (result == DialogResult.Abort)
                        System.Environment.Exit(0);

                }

                else
                {

                    EnumChildWindows(NebhWnd, myCallBack, 0);
                }

              //  FindNebCamera();

                this.Refresh();
            */

            }