Results 1 to 6 of 6

Thread: TypeConverters, UITypeEditor (Full Code Posted) Please someone help!!!

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Red face TypeConverters, UITypeEditor (Full Code Posted) Please someone help!!!

    Okay. I have an ASP.NET Server Control called ImageList. It provides javascript cached images to controls I am develpoing basicly just like the System.Windows.Forms.ImageList.

    Does any one know what the TypeConverter for the int Image attib for a control using this would look like? I mean I want the same support as MS's Image in the property grid....

    I got the concept covered because I examined System.Windows.Forms.ImageIndexConverter which inherits from Int32Converter and I have made TypeConverters before but should the SupportedValuesCollection be a collection of Bittmaps?

    Secondly this thing has no UI so does anyone know how I can get it off why WebForm page and into the ComponetTray? Like MS's ImageList..... and where it should be to keep the layout of a page intact at design time.
    Last edited by Magiaus; Feb 19th, 2004 at 01:55 AM.
    Magiaus

    If I helped give me some points.

  2. #2

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    I almost have this worked out. I created an Interface that does nothing but provided a Imagelist property to a class and in the editor and type converter I check the component through the context to see if it is an IImageIndexProvider and the cast it as one and I have the image list so I can get the information I need to create the standard values and to draw the images in the drop down for the property grid. I also created a ImageIndex class that acts like an int but has the editor and Converter applied to so I never have to do anything other then add a : IImageIndexProvider and the ImageIndex for any imageproperties it needs to support. If that makes since.... I am still fine tunning it.

    Does any one know of a good way to debug Converters and Editors because they don't break at breakpoints I set and this Message box pooping stuff is annoying beczuse I have to guees where I think the error might be and chack the data with a message box and rebuild and ... over and over... and over...
    Magiaus

    If I helped give me some points.

  3. #3

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    I am mear pixels away from getting this working as fully functional designer intergrated thing. but degugging it is making me crazy. The last problem I have to resolve is getting the image for the drop down in the properties window. it just has white boxes because the creation of my bitmap is failing because I can't find away to get the filepath and open the image because the context is so muddled between the Converter the Editor and the image to paint..... it should be working right now as a matter of fact but it just has empty white boxes....

    If I can't seem to fix it tonight I am going to post the code and beg for help...
    Magiaus

    If I helped give me some points.

  4. #4

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Okay here goes

    Okay the problem I have now is that I cannot find a way to get the physical path to the image to draw it in the drop down. this is going on in the editor. everything works just fine except for that and a message about widening the type if you try to use the drop down to change the image index, strangely enough if you double click the image index in the property window it moves to the next image and all is well

    the image class
    Code:
    namespace Ezekiel.Web.Controls
    {
    	[System.ComponentModel.ToolboxItem(false)]
    	public class Image : System.Web.UI.Control, System.Web.UI.INamingContainer
    	{
    		public Image(){}
    		public Image(string imageUrl){src = imageUrl;}
    		protected ImageList _imagelist = null;
    		#region Rendering
    		protected override void Render(System.Web.UI.HtmlTextWriter writer)
    		{
    //			if(src != "" && src != null)
    //			{
    //				if(_imagelist == null)
    //				{
    //					writer.Write("var " + ID + " = new Image();");
    //					writer.Write(ID + ".src=\"" + src + "\";");
    //				}
    //				else
    //				{
    //					writer.Write(_imagelist.ID + "." + ID + " = new Image();");
    //					writer.Write(_imagelist.ID + "." + ID + ".src=\"" + src + "\";");
    //				}
    //			}
    		}
    
    		#endregion
    		#region Override Methods
    		public override bool HasControls(){return false;}
    		protected override void OnInit(System.EventArgs e)
    		{
    			base.OnInit(e);
    		}
    		#endregion
    		#region Override Properties
    		[System.ComponentModel.Browsable(false)]
    		public override bool EnableViewState
    		{
    			get{return true;}
    			set{}
    		}
    		[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.Attribute)]
    		public override string ID
    		{
    			get{return base.ID;}
    			set{base.ID = value;}
    		}
    		[System.ComponentModel.Browsable(false)]
    		public override bool Visible
    		{	//has no effect
    			get{return true;}
    			set{}
    		}
    		#endregion
    		private string htmlid;
    		[System.ComponentModel.Description("ID used by ImageTage(), can not be the same as ID.")]
    		[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.Attribute)]
    		public string HTMLID
    		{
    			get
    			{
    				if(htmlid == null || htmlid == "")
    				{
    					htmlid = "image_" + base.ID;
    				}
    				return htmlid;
    			}
    			set
    			{
    				if(value != ID)
    				{
    					htmlid = value;
    				}
    			}
    		}
    		private string src;
    		[System.ComponentModel.Editor(typeof(System.Web.UI.Design.ImageUrlEditor),typeof(System.Drawing.Design.UITypeEditor))]
    		[System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)]
    		[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.Attribute)]
    		public string ImageUrl
    		{
    			get{return src;}
    			set{src = value;}
    		}
    		#region ImageTag
    		public string ImageTag()
    		{
    			
    			return "<img id=\"" + htmlid + "\" src=\"" + src + "\">";
    		}
    		public string ImageTag(int width, int height)
    		{
    			return "<img id=\"" + htmlid + "\" src=\"" + src + "\" width=\"" + width + "\" height=\"" + height + "\">";
    		}
    		public string ImageTag(int width, int height, string[] attributeNames, string[] attributeValues, int attributeCount)
    		{
    			string attrib = "";
    			for(int i = 0; i < attributeCount; i++)
    			{
    				attrib += attributeNames[i] + "=\"" + attributeValues[i] + "\"";
    				if(i != (attributeCount - 1)){attrib += " ";}
    			}
    			return "<img id=\"" + htmlid + "\" src=\"" + src + "\" width=\"" + width + "\" height=\"" + height + "\" " + attrib + ">";
    		}
    		public string ImageTag(string[] attributeNames, string[] attributeValues, int attributeCount)
    		{
    			string attrib = "";
    			for(int i = 0; i < attributeCount; i++)
    			{
    				attrib += attributeNames[i] + "=\"" + attributeValues[i] + "\"";
    				if(i != (attributeCount - 1)){attrib += " ";}
    			}
    			return "<img id=\"" + htmlid + "\" src=\"" + src + "\" " + attrib + ">";
    		}
    		#endregion
    		internal System.Drawing.Bitmap Bitmap
    		{
    			get
    			{
    				Helpers.Alert(this.ResolveUrl(src));
    				return new System.Drawing.Bitmap("");
    			}
    		}
    	}
    }
    the image collection
    Code:
    namespace Ezekiel.Web.Controls
    {
    	public class ImageCollection : System.Collections.CollectionBase
    	{
    		private System.Collections.ArrayList keys = new System.Collections.ArrayList();
    		#region Add and Remove Methods
    		public int Add(Image image)
    		{
    			keys.Add(image.ID);
    			return base.InnerList.Add(image);
    		}
    		public void Insert(int index, Image image)
    		{
    			keys.Insert(index, image.ID);
    			base.InnerList.Insert(index, image);
    		}
    		public void Remove(Image image)
    		{
    			keys.Remove(image.ID);
    			base.InnerList.Remove(image);
    		}
    		new public void RemoveAt(int index)
    		{
    			keys.RemoveAt(index);
    			base.InnerList.RemoveAt(index);
    		}
    		#endregion
    		#region Add and Remove Operators
    		public static ImageCollection operator + (ImageCollection images, Image image)
    		{
    			images.Add(image);
    			return images;
    		}
    		public static ImageCollection operator - (ImageCollection images, Image image)
    		{
    			/*if(images.InnerList.Contains(image))
    			 *{*/
    			images.Remove(image);
    			/*}*/
    			return images;
    		}		
    		#endregion
    		#region Item Property
    		public Image ImageFromID(string id)
    		{
    			return (Image)this.InnerList[keys.IndexOf(id)];
    		}
    		public Image this[int index]
    		{
    			get{return (Image)this.InnerList[index];}
    			set
    			{
    				keys[index] = ((Image)value).ID;
    				this.InnerList[index] = value;
    			}
    		}
    		#endregion
    	}
    }
    the image list
    Code:
    namespace Ezekiel.Web.Controls
    {
    	[System.ComponentModel.Designer(typeof(Ezekiel.Web.Controls.Design.ImageListDesigner))]
    	[System.Drawing.ToolboxBitmap("E:\\Visual Studio Projects\\Ezekiel.Web\\Controls\\ImageList.ico")]
    	[System.Web.UI.ParseChildren(true,"Images")]
    	[System.Web.UI.PersistChildren(true)]
    	public class ImageList : System.Web.UI.Control, System.Web.UI.INamingContainer
    	{
    		private ImageCollection images = new ImageCollection();
    		#region Control Parsing & Rendering
    		protected override void AddParsedSubObject(object obj)
    		{
    			if(obj is Image)
    			{
    				images.Add((Image)obj);
    			}
    		}
    		protected override void OnPreRender(System.EventArgs e)
    		{
    			base.Page.RegisterClientScriptBlock("zweb_ChangeImage", "<script language=\"javascript\">function zweb_ChangeImage(from, to){from.src = to.src;}</script>");
    			if(images.Count != 0)
    			{
    				string js = "<script language\"javascript\">";
    				foreach(Image img in images)
    				{
    					js += "var " + img.ID  + " = new Image();" + img.ID + ".src = \"" + img.ImageUrl + "\";";
    				}
    				js += "</script>";
    				base.Page.RegisterClientScriptBlock("zweb_ImageList_" + ID, js);
    			}
    
    		}
    		#endregion
    		#region Override Properties
    		[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.Attribute)]
    		public override string ID
    		{
    			get{return base.ID;}
    			set{base.ID = value;}
    		}
    		[System.ComponentModel.Browsable(false)]
    		public override bool EnableViewState
    		{
    			get{return false;}
    			set{}
    		}
    		[System.ComponentModel.Browsable(false)]
    		public override bool Visible
    		{
    			get{return true;}
    			set{}
    		}
    		#endregion
    		[System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)]
    		[System.ComponentModel.MergableProperty(false)]
    		[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerDefaultProperty)]
    		public ImageCollection Images
    		{
    			get{return images;}
    			set{images = value;}
    		}
    
    		#region ImageTag
    		//by id
    		public string ImageTag(string id)
    		{
    			return images.ImageFromID(id).ImageTag();
    		}
    		public string ImageTag(string id, int width, int height)
    		{
    			return images.ImageFromID(id).ImageTag(width, height);
    		}
    		public string ImageTag(string id, int width, int height, string[] attributeNames, string[] attributeValues, int attributeCount)
    		{
    			return images.ImageFromID(id).ImageTag(width, height, attributeNames, attributeValues, attributeCount);
    		}
    		public string ImageTag(string id, string[] attributeNames, string[] attributeValues, int attributeCount)
    		{
    			return images.ImageFromID(id).ImageTag(attributeNames, attributeValues, attributeCount);
    		}
    		//by index
    		public string ImageTag(int index)
    		{
    			return images[index].ImageTag();
    		}
    		public string ImageTag(int index, int width, int height)
    		{
    			return images[index].ImageTag(width, height);
    		}
    		public string ImageTag(int index, int width, int height, string[] attributeNames, string[] attributeValues, int attributeCount)
    		{
    			return images[index].ImageTag(width, height, attributeNames, attributeValues, attributeCount);
    		}
    		public string ImageTag(int index, string[] attributeNames, string[] attributeValues, int attributeCount)
    		{
    			return images[index].ImageTag(attributeNames, attributeValues, attributeCount);
    		}
    		#endregion
    		internal bool loaded = false;
    		protected override void OnInit(System.EventArgs e)
    		{
    			loaded = true;
    			base.OnLoad(e);
    		}
    
    	}
    }
    Magiaus

    If I helped give me some points.

  5. #5

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    wouldn't all fit

    the image index converter
    Code:
    namespace Ezekiel.Web.Controls.Design
    {
    	public class ImageIndexConverter : System.ComponentModel.TypeConverter
    	{
    		public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues
    		(System.ComponentModel.ITypeDescriptorContext context)
    		{
    			System.Collections.ArrayList  a = new System.Collections.ArrayList();
    			if(context != null && context.Instance != null)
    			{
    				System.ComponentModel.Design.IReferenceService iref = (System.ComponentModel.Design.IReferenceService)context.GetService(typeof(System.ComponentModel.Design.IReferenceService));
    				System.ComponentModel.IComponent icom = iref.GetComponent(context.Instance);
    				if(icom is IImageIndexer)
    				{
    					int c = ((IImageIndexer)icom).ImageList.Images.Count;
    					for(int i = 0; i < c; i++)
    					{
    						a.Add(i);
    					}
    					return new System.ComponentModel.TypeConverter.StandardValuesCollection(a);
    				}
    			}
    			System.Windows.Forms.MessageBox.Show("Didn't catch IImageIndexer","ImageIndexConverter Getting Standard Values");
    			a.Add(0);
    			return new System.ComponentModel.TypeConverter.StandardValuesCollection(a);
    		}
    		public override bool GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext context){return true;}
    		public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context){return true;}
    
    	}
    }
    the image index editor
    Code:
    namespace Ezekiel.Web.Controls.Design
    {
    	public class ImageIndexEditor : System.Drawing.Design.UITypeEditor
    	{
    		public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context){return true;}
    		public override void PaintValue(System.Drawing.Design.PaintValueEventArgs e)
    		{
    			if(e.Context != null && e.Context.Instance != null)
    			{
    				System.ComponentModel.Design.IReferenceService iref = (System.ComponentModel.Design.IReferenceService)e.Context.GetService(typeof(System.ComponentModel.Design.IReferenceService));
    				System.ComponentModel.IComponent icom = iref.GetComponent(e.Context.Instance);
    				if(icom is IImageIndexer)
    				{
    					try
    					{
    						Ezekiel.Web.Controls.IImageIndexer indexer = (Ezekiel.Web.Controls.IImageIndexer)icom;
    						if(indexer.ImageList.Images.Count != 0)
    						{
    							try
    							{
    								int i = int.Parse(e.Value.ToString());
    								try
    								{	
    									System.Web.UI.Control ctl = (System.Web.UI.Control)indexer;
    									if(ctl.Page != null)
    									{
    										Helpers.Alert(ctl.Page.MapPath(indexer.ImageList.Images[i].ImageUrl));
    									}
    									else
    									{
    										Helpers.Alert("null Page");
    									}
    									System.Drawing.Bitmap img = new System.Drawing.Bitmap(indexer.ImageList.Page.MapPath("C:\\Intetpub\\wwwroot\\Test_Area\\images\\002.jpg"));
    									try
    									{
    										e.Graphics.DrawImage(img, e.Bounds);
    										img.Dispose();
    									}catch{}
    								}catch{}
    							}catch{}
    						}
    					}catch{}
    				}	
    			}
    			base.PaintValue(e);
    		}
    	}
    }
    the test control
    Code:
    namespace Ezekiel.Web.Controls
    {
    	public class TestImageIndexer : System.Web.UI.Control, IImageIndexer
    	{
    
    		private int imageindex = 0;
    		[System.ComponentModel.TypeConverter("Ezekiel.Web.Controls.Design.ImageIndexConverter")]
    		[System.ComponentModel.Editor("Ezekiel.Web.Controls.Design.ImageIndexEditor", "System.Drawing.Design.UITypeEditor")]
    		public int ImageIndex
    		{
    			get{return imageindex;}
    			set{imageindex = value;}
    		}
    		#region Implementation of IImageIndexer
    		private Ezekiel.Web.Controls.ImageList imagelist = null;
    		public Ezekiel.Web.Controls.ImageList ImageList
    		{
    			get{return imagelist;}
    			set{imagelist = value;}
    		}
    		#endregion
    
    		protected override void Render(System.Web.UI.HtmlTextWriter writer)
    		{
    			if(imagelist != null)
    			{
    				writer.Write(imagelist.ImageTag(imageindex));
    			}
    		}
    	}
    }
    the image list designer
    Code:
    namespace Ezekiel.Web.Controls.Design
    {
    	class ImageListDesigner : System.Web.UI.Design.ControlDesigner
    	{
    		public override bool AllowResize{get{return false;}}
    		public override bool DesignTimeHtmlRequiresLoadComplete{get{return true;}}
    		public override string GetDesignTimeHtml(){return null;}
    		protected override string GetEmptyDesignTimeHtml(){return null;}
    		protected override string GetErrorDesignTimeHtml(System.Exception e)
    		{
    			return	"<div>"
    					+ "<b style=\"color: red;\">Error Source: </b>"
    					+ e.Source 
    					+ "<br>"
    					+ "<b style=\"color: red;\">Error Message: </b>" 
    					+ e.Message 
    					+ "</div>";
    		}
    	}
    }
    the ImageIndexer Interface
    Code:
    namespace Ezekiel.Web.Controls
    {
    	public interface IImageIndexer 
    	{
    		ImageList ImageList{get;set;}
    	}
    }
    If anyone has the time or this interest them please look this over and see what you think. It is kind of complex but not to, to much. It's going to kill me if I have it working except for the stupid image preview because I'm lame......

    I hope someone knows how to fix it.... MapPath will not work as far as I can tell I have tried it about 50 diffrent ways
    Magiaus

    If I helped give me some points.

  6. #6

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Wink

    after reading this thread again from the top I set the ImageIndexConverter to Inherit from Int32Converter and that fixed the pop up about widening from one type to another. So that really only leaves the painting of the image preview in the drop down. Which I am starting to think may be imposible for a web bassed unless I can open the image by it's url and have a N/A image if it fails or something. I don't know...
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width