Çarşamba, Mart 16, 2011

propertygrid üzerinde NumericUpDown kontrolü kullanma

        [DisplayName("Logo Uzaklığı"), DescriptionAttribute1("SelfDestruct", ExtraInfo = "LogoXMax")]
        [Editor(typeof(NumericUpDownEditor1),
            typeof(System.Drawing.Design.UITypeEditor))]
        public int LogoX { get; set; }


burada DescriptionAttribute1 ile NumericUpDown nesnesinin maximum değerini nereden alacağını gösteren ExtraInfo özelliği eklenmiştir.

[Editor(typeof(NumericUpDownEditor1),typeof(System.Drawing.Design.UITypeEditor))] ile de property editorun NumericUpDown nesnesi olarak görüntülenmesi sağlanmıştır.

        public int LogoXMax
        {
            get
            {
                return theParent.Width;
            }
            set { }
        }


NumericUpDown nesnesinin maximum değeri theParent ismindeki formun genişliğiyle eş zamanlı olarak değişiyor. bu şekilde aynı UITypeEditor farklı propertyler için de parametreler ayarlanarak kullanılabilir.


using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Reflection;

namespace WindowsFormsApplication1
{

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class DescriptionAttribute1: UITypeEditor
    {
        public NumericUpDownEditor1()
        {
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }


        public override object EditValue(
            ITypeDescriptorContext context,
            IServiceProvider provider,
            object value)
        {
            IWindowsFormsEditorService edSvc =
     (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (edSvc == null)
            {
                return null;
            }

            DescriptionAttribute1 attrParamName = new DescriptionAttribute1("null");

            foreach (Attribute t in context.PropertyDescriptor.Attributes)
            {
                if (t.GetType().Name == "DescriptionAttribute1")
                {
                    attrParamName = (DescriptionAttribute1)(t);

                }
            }

            if (attrParamName.ExtraInfo == "")
            {
                throw new System.ArgumentException("Parameter cannot be null", "ExtraInfo");

                return null;
            }

            int m2 = Convert.ToInt32(TypeDescriptor.GetProperties(context.Instance).Find(attrParamName.ExtraInfo, true).GetValue(context.Instance));
            if (m2 < (int)value)
            {
                value = m2;
            }

            NumericUpDown nmr = new NumericUpDown();

            nmr.Size = new Size(60, 120);

            nmr.Minimum = 0;

            nmr.Maximum = m2;

            nmr.Increment = 1;

            nmr.DecimalPlaces = 0;

            nmr.Value = (int)value;

            edSvc.DropDownControl(nmr);

            return Convert.ToInt32(nmr.Value);
        }

    }
    public class DescriptionAttribute1 : Attribute
    {
        private string description1;
        public string Description1 { get { return description1; } }


        private string extraInfo;
        public string ExtraInfo { get { return extraInfo; } set { extraInfo = value; } }

        public DescriptionAttribute1(string description1)
        {
            this.description1 = description1;
            this.extraInfo = "";
        }
    }

}




Hiç yorum yok:

Yorum Gönder