RefEdit (Excel)


using Excel = Microsoft.Office.Interop.Excel; 

namespace MyRefEditForm
{
    public enum enEvents_RefEdit
    {
        TextBox_Click,
        Cancel_Click
    }

    public interface IRefEdit_View
    {
        void Method_Close();
        void Method_Initialize();

        int Property_WindowCenterX { get; set; }
        int Property_WindowCenterY { get; set; }
        string Property_Caption { get; set; }

        string Property_Workbook { get; set; }
        string Property_Worksheet { get; set; }
        Excel.Range Property_Range { get; set; }
    }

    public partial class RefEdit_View :
        System.Windows.Forms.Form,
        IRefEdit_View
    {
        private MVP.B_EventsCollection _eventsCollection = null;
        private bool _fireEvents = false;
        private bool _formClosing = false;

        private System.Windows.Forms.Timer _ctlTimer1;

        public delegate void DelegateReturn_OK(string textboxName, string newText, string additionalField = "");
        public delegate void DelegateReturn_Cancel();
        public DelegateReturn_OK Property_Delegate_OK { get; set; }
        public DelegateReturn_Cancel Property_Delegate_Cancel { get; set; }

        //-----------------------------------------------------------------------
        public RefEdit_View()
        {
            InitializeComponent();

            this._eventsCollection = new MVP.B_EventsCollection(this);
            this._fireEvents = true;
            this._formClosing = false;
        }
        //-----------------------------------------------------------------------
        public void Method_RegisterChangeRequestListener<T>(string PropertyName,
            System.EventHandler<MVP.B_EventArgs<T>> handler)
        {
            this._eventsCollection.Method_AddListener<T>(PropertyName, handler);
        }
        //-----------------------------------------------------------------------
        public void Method_UnRegisterChangeRequestListener<T>(string PropertyName,
            System.EventHandler<MVP.B_EventArgs<T>> handler)
        {
            this._eventsCollection.Method_RemoveListener<T>(PropertyName, handler);
        }
        //-----------------------------------------------------------------------
        public void Method_FireChangeRequest(string PropertyName, string requestedValue)
        {
            if (this._eventsCollection != null)
            {
                this._eventsCollection.Method_Fire<string>(PropertyName, requestedValue);
            }
        }
        //-----------------------------------------------------------------------
        public void Method_Close()
        {
            try
            {
                if (!this._formClosing)
                {
                    this._formClosing = true;
                }
                this.Close();

            }
            catch (System.Exception ex)
            {
                ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
            }
        }
        //-----------------------------------------------------------------------
        public void Method_Initialize()
        {
            try
            {
                int center_x = this.Property_WindowCenterX - (int)this.Width / 2;
                int center_y = this.Property_WindowCenterY - (int)this.Height / 2;

                if ((this.Property_WindowCenterX > 0) && (this.Property_WindowCenterY > 0))
                {
                    this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
                    this.Location = new System.Drawing.Point(center_x, center_y);
                }

                Code.CellRef_RangeSelect(this.Property_Range, this.Property_Worksheet);

                if (this.Property_Worksheet != null)
                {
                    if (this.Property_Worksheet != "")
                    {
                        this.Property_RefEditText = this.Property_Worksheet + "!" + this.Property_Range.Address;
                    }
                    else
                    {
                        this.Property_RefEditText = this.Property_Range.Address;
                    }
                }

                this.Text = this.Property_Caption;

                this._ctlTimer1 = new System.Windows.Forms.Timer();
                this._ctlTimer1.Interval = 100;
                this._ctlTimer1.Tick += _ctlTimer1_Tick;
                this._ctlTimer1.Start();
            }
            catch (System.Exception ex)
            {
                ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
            }
        }
        //-----------------------------------------------------------------------------


        public int Property_WindowCenterX { get; set; }
        public int Property_WindowCenterY { get; set; }
        public string Property_Caption { get; set; }


        public string Property_Workbook { get; set; }
        public string Property_Worksheet { get; set; }
        public Excel.Range Property_Range { get; set; }

        public string Property_AdditionalField { get; set; }
        public string Property_RefEditControl { get; set; }

        public string Property_RefEditText
        {
            get { return this.txtRefEdit.Text; }
            set { this.txtRefEdit.Text = value; }
        }

        //-----------------------------------------------------------------------------
        private void txtRefEdit_TextChanged(object sender, System.EventArgs e)
        {
            if ((this._eventsCollection != null) && (this._fireEvents == true))
            {
                this._eventsCollection.Method_Fire<string>(this.GetType().Name,
                    enEvents_RefEdit.TextBox_Click.ToString());
            }
        }
        //-----------------------------------------------------------------------------
        private void _ctlTimer1_Tick(object sender, System.EventArgs e)
        {
            try
            {
                this.txtRefEdit.Text = Code.Public_ReturnSelectionAddress();
                return;
            }
            catch (System.Exception ex)
            {
                this._ctlTimer1.Stop();
                ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
            }
        }
        //-----------------------------------------------------------------------------
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            try
            {
                string additionalfield = this.Property_AdditionalField;
                string refeditcontrol = this.Property_RefEditControl;
                string refeditreturn = this.Property_RefEditText;

                this.Method_Close();

                Property_Delegate_OK(refeditcontrol, refeditreturn, additionalfield);
            }
            catch (System.Exception ex)
            {
                this._ctlTimer1.Stop();
                ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
            }
        }
        //-----------------------------------------------------------------------------
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            try
            {
                this.Method_Close();

                Property_Delegate_Cancel();
            }
            catch (System.Exception ex)
            {
                this._ctlTimer1.Stop();
                ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
            }
        }
        //-----------------------------------------------------------------------------
    }
}



© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext