using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace UniMarc
{
    public static class CExt
    {
        public static void InvokeEnable(this Control pControl, bool pEnable)
        {
            //lock (pControl)
            //{
            if (pControl.Enabled == pEnable) return;
            try
            {
                if (pControl.InvokeRequired)
                {
                    pControl.Invoke(new Action(() =>
                    {
                        pControl.Enabled = pEnable;
                    }));
                }
                else
                {
                    pControl.Enabled = pEnable;
                }
            }
            catch { }
            //}
        }
        public static void InvokeNumeric(this NumericUpDown pContorl,int pValue)
        {
            if (pContorl.Value == pValue) return;
            try
            {
                if (pContorl.InvokeRequired)
                {
                    pContorl.Invoke(new Action(() =>
                    {
                        pContorl.Value = pValue;
                    }));
                }
                else
                {
                    pContorl.Value = pValue;
                }
            }
            catch { }
        }
        public static void InvokeText(this Control pControl, string pText)
        {
            //lock (pControl)
            //{
            if (pControl.Text == pText) return;
            try
            {
                if (pControl.InvokeRequired)
                {
                    pControl.Invoke(new Action(() =>
                    {
                        pControl.Text = pText;
                    }));
                }
                else
                {
                    pControl.Text = pText;
                }
            }
            catch { }
            //}
        }
        public static void InvokeADDText(this Control pControl, string pText)
        {
            //lock (pControl)
            //{
            //if (pControl.Text == pText) return;
            try
            {
                if (pControl.InvokeRequired)
                {
                    pControl.Invoke(new Action(() =>
                    {
                        pControl.Text += (pText);
                        
                    }));
                }
                else
                {
                    pControl.Text += (pText);
                }
            }
            catch { }
            //}
        }
        public static void InvokeInsertText(this TextBox pTextBox, string pText)
        {
            try
            {
                int tIdx = pTextBox.SelectionStart;
                if (pTextBox.InvokeRequired)
                {
                    pTextBox.Invoke(new Action(() =>
                    {
                        pTextBox.Text = pTextBox.Text.Insert(tIdx, pText);
                        pTextBox.SelectionStart = tIdx + 1;

                    }));
                }
                else
                {
                    pTextBox.Text = pTextBox.Text.Insert(tIdx, pText);
                    pTextBox.SelectionStart = tIdx + 1;
                }
            }
            catch { }
        }
    }
}
