Sign in to follow this  
NovaCaine

[.net] Item List not storing in Custom User Control

Recommended Posts

Hey all, I'm trying to write my own custom control after finding an annoying bug in the .NET ImageList component. The bug shows itself when you add a transparent .PNG file to the list, the transparency gets corrupt and causes the image to have a blue tint to it. If I load the file directly into an Image it displays fine. So after a lot of research into this, I could use someone elses code - which isn't the preferred method as I'm wanting to learn from this project and try to keep it compatible with Mono on Linux, or writing my own custom control. I tried just overwriting the TreeView and ListView draw functions, but I then run into problems with the bounds not being big enough to fit the new data in. So getting back to the problem at hand, I've managed to create my own control and it's working exactly how I want it to - except when I add items at design they only stay attached to the list until I move the control/build the project. They are being added to the designer fine, but I've noticed in the area where my control is defined it doesn't have a line to add my items. I can manually enter one and it'll work great - something like this
this.myControl.Items.AddRange(new MyControlItems[] {
            myControlItem1,
            myControlItem2,
            myControlItem3,
            myControlItem4});
but I can't use the designer. Am I missing some specific designer code? The class(es) are below that I use on a simple form for testing: The Idea is to have something like a ListView (going to tackle the TreeView later) but allow for multiple lines after an image.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Collections;

namespace EVEPanelTest
{
	public partial class EVEPanel : UserControl
	{
		EVEPanelItemCollection _items = new EVEPanelItemCollection();
		int _itemHeight = 32;

		public EVEPanelItemCollection Items
		{
			get
			{
				return _items;
			}
		}

		public EVEPanel()
		{
			InitializeComponent();
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);

			// Paint each item
			int i = 0;
			foreach (EVEPanelItem item in _items)
			{
				Point newImgLocation = new Point(0, (i*(_itemHeight+2)));

				if (item.Icon != null)
				{
					e.Graphics.DrawImage(item.Icon, newImgLocation.X, newImgLocation.Y, _itemHeight, _itemHeight);

					e.Graphics.DrawString(item.Text, Font, new SolidBrush(ForeColor), new PointF(newImgLocation.X + _itemHeight, newImgLocation.Y));
				}

				i++;
			}
		}
	}

	public class EVEPanelItemCollection : CollectionBase
	{
		public int Add(EVEPanelItem item)
		{
			return List.Add(item);
		}
		public void Insert(int index, EVEPanelItem item)
		{
			List.Insert(index, item);
		}
		public void Remove(EVEPanelItem item)
		{
			List.Remove(item);
		}
		public bool Contains(EVEPanelItem item)
		{
			return List.Contains(item);
		}
		public int IndexOf(EVEPanelItem item)
		{
			return List.IndexOf(item);
		}
		public void CopyTo(EVEPanelItem[] array, int index)
		{
			List.CopyTo(array, index);
		}
		public EVEPanelItem this[int index]
		{
			get { return (EVEPanelItem)List[index]; }
			set { List[index] = value; }
		}
                public void AddRange(EVEPanelItem[] evePanelItems)
		{
			foreach (EVEPanelItem item in evePanelItems)
			{
				Add(item);
			}
		}
	}

	public class EVEPanelItem : Component
	{
		string _text;
		Image _icon;

		public EVEPanelItem()
		{
			_text = "";
		}

		public EVEPanelItem(string text, Image icon)
		{
			_text = text;
			_icon = icon;
		}

		public Image Icon
		{
			get
			{
				return _icon;
			}
			set
			{
				_icon = value;
			}
		}

		public string Text
		{
			get
			{
				return _text;
			}
			set
			{
				_text = value;
			}
		}
	}

	/*
	 * this one kinda works
	 * but has to be added manually
	public class EVEPanelItem
	{
		string _text;
		Image _icon;

		public EVEPanelItem()
		{
			_text = "";
		}

		public EVEPanelItem(string text, Image icon)
		{
			_text = text;
			_icon = icon;
		}

		public Image Icon
		{
			get
			{
				return _icon;
			}
			set
			{
				_icon = value;
			}
		}

		public string Text
		{
			get
			{
				return _text;
			}
			set
			{
				_text = value;
			}
		}
	}*/
}


Share this post


Link to post
Share on other sites
Hello,

I don't think I quite understood what your problem is. You created your own control that displays a EVEPanelItemCollection list, and you can't add items to it at the designer. Note that you use a list of a custom type, therefore VS can't figure out how to properly create and manage that kind of objects.

You may need to use a TypeConverter for that item class, that specifies to VS how to interact with it:
http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx

PS: You could simplify the EVEPanelItemCollection just by implementing it like:
public class EVEPanelItemCollection : List<EVEPanelItem> {}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this