using System.Drawing;
using System.Windows.Forms;

namespace ChuPiao.Common.Controls
{
    public class LabelableErrorProvidercs:ErrorProvider
    {
        //停留在哪个控件后面
        private Control _control;

        public LabelableErrorProvidercs(System.ComponentModel.IContainer componet)
            : base(componet)
        { 
            
        }

        /// <summary>
        /// 给errorprovider和label赋值，下
        /// </summary>
        /// <param name="control"></param>
        /// <param name="message"></param>
        public void SetErrorAndLabel(Control control, string message)
        {
            _control = control;
            //调用基类的方法
            SetError(control, message);

            //根据message为空来判断动态添加或删除label
            string labelname = "label_" + control.Name;
            if (!string.IsNullOrEmpty(message))
            {
                Label errorlabel;
                
                if (-1 == control.Parent.Controls.IndexOfKey(labelname))
                {
                    errorlabel = new Label();
                    errorlabel.Name = labelname;
                    errorlabel.Left = control.Left;
                    errorlabel.Top = control.Top + control.Height;
                    errorlabel.Height = control.Height;
                    //errorlabel.AutoSize = true;
                    errorlabel.ForeColor = Color.Red;
                    control.Parent.Controls.Add(errorlabel);
                }
                else
                {
                    errorlabel = control.Parent.Controls[labelname] as Label;
                }
                errorlabel.Text = message;
                
            }
            else
            {
                control.Parent.Controls.RemoveByKey(labelname);
            }
        }

        /// <summary>
        /// 右
        /// </summary>
        /// <param name="control"></param>
        /// <param name="message"></param>
        public void SetErrorAndLabel2(Control control,string message)
        {
            _control = control;
            //调用基类的方法
            SetError(control, message);

            //根据message为空来判断动态添加或删除label
            string labelname = "label_" + control.Name;
            if (!string.IsNullOrEmpty(message))
            {
                Label errorlabel;

                if (-1 == control.Parent.Controls.IndexOfKey(labelname))
                {
                    errorlabel = new Label();
                    errorlabel.Name = labelname;
                    errorlabel.Left = control.Left+control.Width+15;
                    errorlabel.Top = control.Top;
                    errorlabel.Height = control.Height;
                    //errorlabel.AutoSize = true;
                    errorlabel.ForeColor = Color.Red;
                    control.Parent.Controls.Add(errorlabel);
                }
                else
                {
                    errorlabel = control.Parent.Controls[labelname] as Label;
                }
                errorlabel.Text = message;

            }
            else
            {
                control.Parent.Controls.RemoveByKey(labelname);
            }
        }

        public void ClearErrorLabel()
        {
            Clear();
            if (_control != null)
            {
                //根据message为空来判断动态添加或删除label
                string labelname = "label_" + _control.Name;

                System.Windows.Forms.Control.ControlCollection controls = _control.Parent.Controls;
                if (controls.ContainsKey(labelname))
                {
                    controls.RemoveByKey(labelname);
                }
            }
        }
    }
}

