context is null in my TypeConverter.ConvertFrom() method

L

lucianoluke

Hi all,

I am trying to have a collection serialized to a property of the parent

control in HTML.


The collection items are controls found in the page. When the designer
is being loaded, My TypeConverter.ConvertFrom is called. The problem is

that ITypeDescriptorContext context is null.


I tryied creating a ControlDesigner and overriding
DesignTimeHtmlRequiresLoadComplete to always return true; No good. The
ConvertFrom is called BEFORE my ControlDesigner constructor's is even
called.


Is there any spell that I can cast to solve this problem?


my property attributes are:
Code:
-----------------------------------------
[Description("The tabs displayed on this control")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

[PersistenceMode(PersistenceMode.Attribute)]
[TypeConverter(typeof(TabCollectionTypeConverter))]
[Editor(typeof(TabCollectionUITypeEditor), typeof(UITypeEditor))]
public TabCollection TabPanels
{
get
{
object tabs = this.ViewState["TabPanels"] as
TabCollection;
if (tabs != null)
{
return (TabCollection) tabs;
}
return new TabCollection();
}
set
{
this.ViewState["TabPanels"] = value;
}



}


------------------------------------------------------
TabCollectionTypeConverter
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
TabCollection tc = new TabCollection();

if (context != null && value is string)
{
string[] v = ((string)value).Split(new char[] {','});
foreach (string s in v)
{
ComponentCollection components =
context.Container.Components;
foreach (IComponent component in components)
{
if (!(component is Control))
{
continue;
}


HPTabPanel ctrl =
(HPTabPanel)component;
if ((ctrl.ClientID != null) &&
(ctrl.ClientID.Length != 0) &&
ctrl.ClientID == s)
{
tc.Add(ctrl);
}
}
}
}


return tc;


}


-------------------------------------  UITypeEditor
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
if (provider != null)
{
IWindowsFormsEditorService editorService =
(IWindowsFormsEditorService)
provider.GetService(typeof(IWindowsFormsEditorService));
if (editorService == null)
{
return value;
}

// get forms editor service
IWindowsFormsEditorService wfes =
provider.GetService(typeof(IWindowsFormsEditorService)) as
IWindowsFormsEditorService;


// create our form
TabCollectionEditorForm form = new
TabCollectionEditorForm(context,
value as TabCollection);


// show our form
wfes.ShowDialog(form);


// Force designer update
context.OnComponentChanged();


// return updated value
return form.Tabs;
}


// just in case
return base.EditValue(context, provider, value);


}


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

Notice that I dont want to have InnerProperties and the like. I want my

control data string stored in a property.


Thanks for any input that may help.


Luciano Bargmann
 
C

Chris A.

See MSDN online help...

"Note Do not rely on the presence of this interface when you design a type
converter. If a particular interface, property, or service is necessary but
not available, the type converter should return a null reference (Nothing in
Visual Basic) rather than throw an exception. This interface's properties can
return a null reference (Nothing) at any time, and you should plan for this."

Meaning that context may (or not) always be null or undefined.
Unfortunately, this is a MS design SOOL that control authors must tolerate.

Hi all,

I am trying to have a collection serialized to a property of the parent

control in HTML.


The collection items are controls found in the page. When the designer
is being loaded, My TypeConverter.ConvertFrom is called. The problem is

that ITypeDescriptorContext context is null.


I tryied creating a ControlDesigner and overriding
DesignTimeHtmlRequiresLoadComplete to always return true; No good. The
ConvertFrom is called BEFORE my ControlDesigner constructor's is even
called.


Is there any spell that I can cast to solve this problem?


my property attributes are:
Code:
-----------------------------------------
[Description("The tabs displayed on this control")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

[PersistenceMode(PersistenceMode.Attribute)]
[TypeConverter(typeof(TabCollectionTypeConverter))]
[Editor(typeof(TabCollectionUITypeEditor), typeof(UITypeEditor))]
public TabCollection TabPanels
{
get
{
object tabs = this.ViewState["TabPanels"] as
TabCollection;
if (tabs != null)
{
return (TabCollection) tabs;
}
return new TabCollection();
}
set
{
this.ViewState["TabPanels"] = value;
}



}


------------------------------------------------------
TabCollectionTypeConverter
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
TabCollection tc = new TabCollection();

if (context != null && value is string)
{
string[] v = ((string)value).Split(new char[] {','});
foreach (string s in v)
{
ComponentCollection components =
context.Container.Components;
foreach (IComponent component in components)
{
if (!(component is Control))
{
continue;
}


HPTabPanel ctrl =
(HPTabPanel)component;
if ((ctrl.ClientID != null) &&
(ctrl.ClientID.Length != 0) &&
ctrl.ClientID == s)
{
tc.Add(ctrl);
}
}
}
}


return tc;


}


-------------------------------------  UITypeEditor
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
if (provider != null)
{
IWindowsFormsEditorService editorService =
(IWindowsFormsEditorService)
provider.GetService(typeof(IWindowsFormsEditorService));
if (editorService == null)
{
return value;
}

// get forms editor service
IWindowsFormsEditorService wfes =
provider.GetService(typeof(IWindowsFormsEditorService)) as
IWindowsFormsEditorService;


// create our form
TabCollectionEditorForm form = new
TabCollectionEditorForm(context,
value as TabCollection);


// show our form
wfes.ShowDialog(form);


// Force designer update
context.OnComponentChanged();


// return updated value
return form.Tabs;
}


// just in case
return base.EditValue(context, provider, value);


}


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

Notice that I dont want to have InnerProperties and the like. I want my

control data string stored in a property.


Thanks for any input that may help.


Luciano Bargmann
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,931
Messages
2,570,085
Members
46,536
Latest member
keelop

Latest Threads

Top