[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Ken Kolda

10/18/2004 3:50:00 PM

I think you posted this to the wrong newsgroup. Try
microsoft.public.dotnet.framework.winforms.controls.

Ken


"Sheraz Khan via .NET 247" <anonymous@dotnet247.com> wrote in message
news:eu$n0GDtEHA.3200@TK2MSFTNGP14.phx.gbl...
(Type your message here)

I am trying to develop a very simple control that keeps the collection of an
object of type Person. I have a property Item that keeps the collection of
Person. I'm using Model UITypeEditor to populate the collection. Everything
looks fine but if at design time i close the form and reopen it, the control
looses the collection of person object means the control is not able to
serialize the collection. I'm using DesignerSerializationVisibility.Content
attribute on the property as follows

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor(typeof(PersonUIEditor), typeof(System.Drawing.Design.UITypeEditor))]
public PersonClassCollector Items
{
get
{
return this._Coll;
}
set
{
this._Coll = value;
}
}

and also using the type convertor for Person object but nothing seems to be
working. Following is the complete listing of my code. Thanks a million for
your anticipation.

Control Code and UITypeEditor
-----------

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Person_Collector_Control
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private PersonClassCollector _Coll;


public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
_Coll = new PersonClassCollector();

// TODO: Add any initialization after the InitComponent call

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor(typeof(PersonUIEditor), typeof(System.Drawing.Design.UITypeEditor))]
public PersonClassCollector Items
{
get
{
return this._Coll;
}
set
{
this._Coll = value;
}
}


#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(288, 150);

}
#endregion
}


public class PersonUIEditor: UITypeEditor
{

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext
context)
{
if(context!=null)
return UITypeEditorEditStyle.Modal;
return base.GetEditStyle(context);
}


public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
if(context!=null && provider !=null)
{
IWindowsFormsEditorService editorService =
(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorSe
rvice));
if(editorService !=null)
{
frmPerson frm = new frmPerson();
frm._PersonColl = (PersonClassCollector)value;
if(editorService.ShowDialog(frm) == DialogResult.OK)
{
return frm._PersonColl;
}
}
}
return base.EditValue(context, provider, value);
}
}

}


----------------------------------------------------------------------------
--------------

Person, PeronCollector and Person Type Convertor Object

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Person_Collector_Control
{
/// <summary>
/// Summary description for Person.
/// </summary>
///

#region "Person Class"
[TypeConverter(typeof(PersonConvertor))]
public class Person
{

private string _FirstName, _LastName;

public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}

public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}


public override string ToString()
{
return this._LastName + ", " + this._FirstName;
}




}

#endregion

#region "Peson Collector"
public class PersonClassCollector
{
public ArrayList _Coll ;
public PersonClassCollector()
{
_Coll = new ArrayList();

}

public void AddPerson(Person p)
{
this._Coll.Add(p);
}

public bool Contails(Person p)
{
return this._Coll.Contains(p);
}
}
#endregion

# region "Type Converter Code"

public class PersonConvertor:TypeConverter
{

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
return true;

return base.CanConvertTo(context, destinationType);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
System.Reflection.ConstructorInfo ci =
typeof(Person).GetConstructor(System.Type.EmptyTypes);

return new InstanceDescriptor(ci, null, false);
}

return base.ConvertTo(context, culture, value, destinationType);
}
}


# endregion
}


--------------------------------
From: Sheraz Khan

-----------------------
Posted by a user from .NET 247 (http://www.dotn...)

<Id>Cch22JZDe0WO1A2g2wMIUA==</Id>