none
Collection em Designer-Timer RRS feed

  • Discussão Geral

  • Estou criando um controle, onde eu tenho uma propriedade tipo coleção (CollectionItemBar ListItemBar), mas não armazena os dados no Designer-Timer.

    Como faço para guardar a coleção de itens?

    Controle:

    [Designer(typeof(StatusBarDesigner))]
    public class StatusBarWin : Control
    {
        private CollectionItemBar listItemBar;
    
        [Editor(typeof(StatusBarUITypeEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), MergableProperty(false)]
        public CollectionItemBar ListItemBar
        {
            get
            {
                if (this.listItemBar == null)
                {
                    this.listItemBar = new CollectionItemBar();
                }
                return this.listItemBar;
            }
        }
    
        public event ClickItemEventHandler ClickItem;
    
        public virtual void OnClickItem(ClickEventArgs e)
        {
            if (ClickItem != null)
            {
                ClickItem(this, e);
            }
        }
    
        public StatusBarWin()
        {
            base.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.Opaque | ControlStyles.Selectable, false);
            base.SetStyle(ControlStyles.UserMouse, true);
            base.SetStyle(ControlStyles.StandardClick, true);
    
    
            this.BackColor = ColorTranslator.FromHtml("#4183D7");
            this.Dock = DockStyle.Bottom;
            this.ForeColor = Color.White;
            this.Font = new Font("Segoe UI", 10f, FontStyle.Regular);
            this.Height = 28;
        }
    
        private void ListItemBar_Changed(object sender, EventArgs e)
        {
            this.Invalidate();
        }
    
        public Int32 WidthItem(ItemBar item)
        {
            Int32 width = 8;
            if (item.Image != null)
            {
                #region Image                
    
                int altura = item.Image.Size.Height;
                if (item.Image.Size.Height - 4 > item.Height)
                {
                    altura = item.Height - 4;
                }
                int largura = CalculateProportionalHeight(item.Image.Size.Width, item.Image.Size.Height, altura);
    
                width += largura + 2;
    
                #endregion
            }
            if (string.IsNullOrEmpty(item.Text) == false)
            {
                width += TextRenderer.MeasureText(item.Text, this.Font).Width;
            }
    
            return width;
        }
    
        private int CalculateProportionalHeight(int oldWidth, int oldHeight, int newHeight)
        {
            if (oldWidth <= 0 || oldHeight <= 0 || newHeight <= 0)
            {
                // For safety.
                return oldHeight;
            }
            double heightFactor = (double)newHeight / (double)oldHeight;
            int newWidth = (int)Math.Round(heightFactor * (double)oldWidth);
            if (newWidth < 1)
            {
                newWidth = 1; // just in case.
            }
            return newWidth;
        }
    
        public void PerformClick(ItemBar item)
        {
            ClickEventArgs clickArgs = new ClickEventArgs();
            clickArgs.ItemBar = item;
            OnClickItem(clickArgs);
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            foreach (ItemBar item in ListItemBar.Cast<ItemBar>())
            {
                #region Status Item
    
                if (item.StatusItem != StatusItem.Normal && item.TypeItem == TypeItem.Button)
                {
                    if (item.StatusItem == StatusItem.Hover)
                    {
                        Color color = ControlPaint.Light(this.BackColor);
                        if (color.A == this.BackColor.A && color.B == this.BackColor.B && color.G == this.BackColor.G)
                        {
                            color = ControlPaint.Dark(this.BackColor);
                        }
    
                        e.Graphics.FillRectangle(new SolidBrush(color), new Rectangle(item.Location.X, item.Location.Y, WidthItem(item), item.Height));
                    }
                    else if (item.StatusItem == StatusItem.Clicked)
                    {
                        Color color = ControlPaint.LightLight(this.BackColor);
                        if (color.A == this.BackColor.A && color.B == this.BackColor.B && color.G == this.BackColor.G)
                        {
                            color = ControlPaint.DarkDark(this.BackColor);
                        }
    
                        e.Graphics.FillRectangle(new SolidBrush(color), new Rectangle(item.Location.X, item.Location.Y, WidthItem(item), item.Height));
                    }
                }
    
                #endregion
    
                Int32 x = 4;
                if (item.Image != null)
                {
                    #region Image
    
                    int altura = item.Image.Size.Height;
                    if (item.Image.Size.Height - 4 > item.Height)
                    {
                        altura = item.Height - 4;
                    }
                    int largura = CalculateProportionalHeight(item.Image.Size.Width, item.Image.Size.Height, altura);
    
                    if (this.Enabled == true)
                    {
                        e.Graphics.DrawImage(item.Image, new Rectangle(item.Location.X + 2, item.Location.Y + (item.Height / 2) - (altura / 2), largura, altura));
                    }
                    else
                    {
                        Size size = item.Image.Size;
    
                        float[][] newColorMatrix = new float[5][];
                        newColorMatrix[0] = new float[] { 0.2125f, 0.2125f, 0.2125f, 0f, 0f };
                        newColorMatrix[1] = new float[] { 0.2577f, 0.2577f, 0.2577f, 0f, 0f };
                        newColorMatrix[2] = new float[] { 0.0361f, 0.0361f, 0.0361f, 0f, 0f };
                        float[] singleArray1 = new float[5];
                        singleArray1[3] = 1f;
                        newColorMatrix[3] = singleArray1;
                        newColorMatrix[4] = new float[] { 0.38f, 0.38f, 0.38f, 0f, 1f };
                        ColorMatrix matrix = new ColorMatrix(newColorMatrix);
                        ImageAttributes disabledImageAttr = new ImageAttributes();
                        disabledImageAttr.ClearColorKey();
                        disabledImageAttr.SetColorMatrix(matrix);
    
                        e.Graphics.DrawImage(item.Image, new Rectangle(item.Location.X + 4, item.Location.Y + (item.Height / 2) - (altura / 2), largura, altura), 0, 0, size.Width, size.Height, GraphicsUnit.Pixel, disabledImageAttr);
                    }
    
                    x += largura + 2;
    
                    #endregion
                }
                if (string.IsNullOrEmpty(item.Text) == false)
                {
                    #region Text
    
                    Int32 width = TextRenderer.MeasureText(item.Text, this.Font).Width;
    
                    TextRenderer.DrawText(e.Graphics, item.Text, this.Font, new Rectangle(item.Location.X + x, item.Location.Y + 2, width, item.Height - 4), this.ForeColor);
    
                    #endregion
                }
            }
    
            base.OnPaint(e);
        }
    
        protected override void OnMouseMove(MouseEventArgs e)
        {
            bool changes = false;
    
            var item = ListItemBar.Cast<ItemBar>().FirstOrDefault(f => e.X >= f.Location.X && e.X < f.Location.X + WidthItem(f) && 
                                                       e.Y >= f.Location.Y && e.Y <= f.Location.Y + f.Height);
            if (item != null)
            {
                if (item.StatusItem != StatusItem.Hover)
                {
                    item.StatusItem = StatusItem.Hover;
                    changes = true;
                }
            }
    
            foreach (var itemBar in ListItemBar.Cast<ItemBar>())
            {
                if (itemBar != item)
                {
                    if (itemBar.StatusItem != StatusItem.Normal)
                    {
                        itemBar.StatusItem = StatusItem.Normal;
                        changes = true;
                    }
                }
            }
    
            if (changes == true)
            {
                this.Invalidate();
            }
    
            base.OnMouseMove(e);
        }
    
        protected override void OnMouseLeave(EventArgs e)
        {
            foreach (var item in ListItemBar.Cast<ItemBar>())
            {
                item.StatusItem = StatusItem.Normal;
            }
            this.Invalidate();
    
            base.OnMouseLeave(e);
        }
    
        protected override void OnMouseDown(MouseEventArgs e)
        {
            var item = ListItemBar.Cast<ItemBar>().FirstOrDefault(f => e.X >= f.Location.X && e.X < f.Location.X + WidthItem(f) &&
                                                       e.Y >= f.Location.Y && e.Y <= f.Location.Y + f.Height);
            if (item != null)
            {
                item.StatusItem = StatusItem.Clicked;
                this.Invalidate();
    
                ClickEventArgs clickArgs = new ClickEventArgs();
                clickArgs.ItemBar = item;
                OnClickItem(clickArgs);
            }
    
            base.OnMouseDown(e);
        }
    
        protected override void OnMouseUp(MouseEventArgs e)
        {
            var item = ListItemBar.Cast<ItemBar>().FirstOrDefault(f => e.X >= f.Location.X && e.X < f.Location.X + WidthItem(f) &&
                                                       e.Y >= f.Location.Y && e.Y <= f.Location.Y + f.Height);
            if (item != null)
            {
                item.StatusItem = StatusItem.Hover;
                this.Invalidate();
            }
    
            base.OnMouseUp(e);
        }
    }

    Eventos:

    public delegate void ClickItemEventHandler(object sender, ClickEventArgs e);
    
    public class ClickEventArgs : EventArgs
    {
        public ItemBar ItemBar { get; set; }
    }

    Coleção de itens:

    public class CollectionItemBar : IList, ICollection
    {
        private ArrayList items;
    
        public object this[int index]
        {
            get
            {
                return items[index];
            }
    
            set
            {
                items[index] = value;
            }
        }
    
        public int Count
        {
            get
            {
                return items.Count;
            }
        }
    
        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }
    
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }
    
        public bool IsSynchronized
        {
            get
            {
                return false;
            }
        }
    
        public object SyncRoot
        {
            get
            {
                return this;
            }
        }
    
        public CollectionItemBar()
        {
            this.items = new ArrayList();
        }
    
        public int Add(object value)
        {
            return items.Add(value);
        }
    
        public void AddRange(ICollection value)
        {
            items.AddRange(value);
        }
    
        public void Clear()
        {
            items.Clear();
        }
    
        public bool Contains(object value)
        {
            return items.Contains(value);
        }
    
        public void CopyTo(Array array, int index)
        {
            items.CopyTo(array, index);
        }
    
        public IEnumerator GetEnumerator()
        {
            return items.GetEnumerator();
        }
    
        public int IndexOf(object value)
        {
            return items.IndexOf(value);
        }
    
        public void Insert(int index, object value)
        {
            items.Insert(index, value);
        }
    
        public void Remove(object value)
        {
            items.Remove(value);
        }
    
        public void RemoveAt(int index)
        {
            items.RemoveAt(index);
        }
    }

    Item utilizado na coleção:

    [TypeConverter(typeof(ItemBarTypeConverter))]
    [DesignTimeVisible(false)]
    public class ItemBar
    {
        public TypeItem TypeItem { get; set; }
    
        private string text = "ItemBar";
    
        public string Text
        {
            get { return text; }
            set
            {
                text = value;
            }
        }
    
        private string name = "itemBar";
    
        public string Name
        {
            get { return name; }
            set
            {
                if (string.IsNullOrWhiteSpace(value) == true)
                {
                    throw new Exception("RequiredProperty does not accept null values");
                }
    
                name = value;
            }
        }
    
        [DefaultValue(typeof(Image), null)]
        public Image Image { get; set; }
    
        private Point location = new Point(0, 0);
    
        public Point Location
        {
            get { return location; }
            set { location = value; }
        }
    
        private int height = 28;
    
        public int Height
        {
            get { return height; }
            set { height = value; }
        }
    
        [Browsable(false)]
        public StatusItem StatusItem { get; set; }
    
        public ItemBar()
        {
    
        }
    
        public ItemBar(TypeItem TypeItem, string Text, string Name, Image Image, Point Location, int Height, StatusItem StatusItem)
        {
            this.TypeItem = TypeItem;
            this.Text = Text;
            this.Name = Name;
            this.Image = Image;
            this.Location = Location;
            this.Height = Height;
            this.StatusItem = StatusItem;
        }
    }
    
    public enum TypeItem
    {
        Button,
        Label
    }
    
    public enum StatusItem
    {
        Normal,
        Hover,
        Clicked
    }

    Type Converter:

    internal class ItemBarTypeConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            MessageBox.Show("OI");
    
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
    
            if ((destinationType == typeof(InstanceDescriptor)) && (value != null))
            {
                ItemBar itemBar = value as ItemBar;
    
                ConstructorInfo constructor;
                if (itemBar != null)
                {
                    Type[] types = new Type[] { typeof(TypeItem), typeof(string), typeof(string), typeof(Image), typeof(Point), typeof(int), typeof(StatusItem) };
                    constructor = itemBar.GetType().GetConstructor(types);
                    if (constructor != null)
                    {
                        object[] objects = new object[7];
                        objects[0] = itemBar.TypeItem;
                        objects[1] = itemBar.Text;
                        objects[2] = itemBar.Name;
                        objects[3] = itemBar.Image;
                        objects[4] = itemBar.Location;
                        objects[5] = itemBar.Height;
                        objects[6] = itemBar.StatusItem;
    
                        return new InstanceDescriptor(constructor, new object[] { objects }, false);
                    }
                }
                constructor = itemBar.GetType().GetConstructor(new Type[0]);
                if (constructor != null)
                {
                    return new InstanceDescriptor(constructor, new object[0], false);
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    Control Designer:

    public class StatusBarDesigner : ControlDesigner
    {
        private DesignerVerb _addVerb;
        private DesignerVerbCollection _verbs;
    
        public override DesignerVerbCollection Verbs
        {
            get
            {
                if (_verbs == null)
                {
                    _verbs = new DesignerVerbCollection();
    
                    _addVerb = new DesignerVerb("Edit items", this.EditItems) { Description = "Edit items bar." };
    
                    _verbs.Add(_addVerb);
                }
    
                return _verbs;
            }
        }
    
        private void EditItems(object sender, EventArgs e)
        {
            StatusBarWin statusBarWin = this.Control as StatusBarWin;
    
            FrmEditorCollection frm = new FrmEditorCollection(statusBarWin.ListItemBar, typeof(ItemBar));
            DialogResult result = this.ShowDialog(frm);
            if (result == DialogResult.OK)
            {
                statusBarWin.ListItemBar.Clear();
                statusBarWin.ListItemBar.AddRange((CollectionItemBar)frm.List);
                statusBarWin.Invalidate();
            }
        }
    
        private DialogResult ShowDialog(Form dialog)
        {
            IUIService service = base.Component.Site.GetService(typeof(IUIService)) as IUIService;
            if (service != null)
            {
                return service.ShowDialog(dialog);
            }
            return dialog.ShowDialog(base.Component as IWin32Window);
        }
    
    
    }

    Type editor:

    public class StatusBarUITypeEditor : UITypeEditor
    {
        private StatusBarUITypeEditor()
        {
    
        }
    
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if ((service == null) || (context.Instance == null))
                {
                    return value;
                }
                IDesignerHost host = (IDesignerHost)provider.GetService(typeof(IDesignerHost));
                if (host == null)
                {
                    return value;
                }
    
                FrmEditorCollection frm = new FrmEditorCollection(value as CollectionItemBar, typeof(ItemBar));
    
                DialogResult result = service.ShowDialog(frm);
                if (result == DialogResult.OK)
                {
                    ((CollectionItemBar)value).Clear();
                    ((CollectionItemBar)value).AddRange((CollectionItemBar)frm.List);
                    ((StatusBarWin)context.Instance).Invalidate();
                }
    
    
                //CollectionEditor ce = new CollectionEditor(typeof(List<ItemBar>));
                //ce.EditValue(context, provider, value);
                //((StatusBarWin)context.Instance).Invalidate();
            }           
    
            return value;
        }
    
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }

    sexta-feira, 29 de setembro de 2017 17:23