Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error while returning value from EditValue method

9 views
Skip to first unread message

Kaushik

unread,
Oct 21, 2009, 5:11:40 AM10/21/09
to
Hi,
I am Kaushik from India.
I am creating a SSDBCombo user control providing design time feature to configure columns.
Main user control class name is SSDBCombo. This class contains a custom property DBColumnsCols of type SYColumns
SYColumns is a class which have properties and those will be displayed in property browser as sub properties of DBColumnsCols
FormEditorCombo is UITypeEditor type which lets user to add or remove columns during design time
UserTypeConverter is ExpandableObjectConverter type which serializes and deserializes the sub properties value of DbColumnsCols
UserControlForm is the custom Editor form.
Now I am facing problem in returning object from EditValue method. I am able to configure the columns in designtime and the columns are displayed correctly in design time. After exiting run time mode of the application and returning to design time to change the configuration of the control an error message is displayed while returning value from EditValue method. The error is as follows:
"Unable to cast object of type SYColumns to object of target type SYColumns".
Please assist me in resolving this problem.
For reference relevant code for each of the above class is mentioned below.
public partial class SSDBCombo : UserControl
{
[DefaultValue(""), DesignerSerializationVisibility (DesignerSerializationVisibility.Content),Browsable(true)]
[TypeConverter(typeof(ExpandableObjectConverter))
[EditorAttribute(typeof(FormEditorCombo), typeof(UITypeEditor))]
public SYColumns DBColumnsCols
{
get
{
return _syCols;
}
set
{
_syCols = value;
if (_syCols != null)
{
if (_syCols.ColsCount > 0)
{
if (_syCols.ColNames != null && _syCols.ColHeaderText != null)
{
MessageBox.Show("Setting DBCOLUMNSCOLS");
}
}
}
}
}
..
}

public class FormEditorCombo : UITypeEditor
{
Boolean m_Escaped;
IWindowsFormsEditorService IEditiorService;

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
string strColNames = "";
string strColHeader = "";
int iColCount = 0;
string sColNames = "";
string sColHeader = "";
if (value == null)
MessageBox.Show("Value is nothing");
if (context != null && provider != null)
{
WindowsApplication1.UserControlForm m_EditControl = new UserControlForm();
IEditiorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

if (IEditiorService != null)
{
if (value != null)
{
PropertyDescriptorCollection pdc = context.PropertyDescriptor.GetChildProperties();
sColNames = pdc[2].GetValue(value).ToString();
sColHeader = pdc[1].GetValue(value).ToString();
int i = (int)pdc[0].GetValue(value);
iColCount = i;
if (sColHeader != null & sColNames != null)
{
if (sColNames.Trim() != "" && sColHeader.Trim() != "")
{
for (int h = 0; h < sColNames.Split(new char[] { ',' }).Length; h++)
{
if (sColNames.Split(new char[] { ',' })[h].ToString() != "")
{
m_EditControl.lstCols.Items.Add(sColNames.Split(new char[] { ',' })[h].ToString() + "-" + sColHeader.Split(new char[] { ',' })[h].ToString());
}
}
}
}
}
SYColumns SYCOL = new SYColumns(sColNames, sColHeader, iColCount);
m_EditControl.SYCol = SYCOL;
IEditiorService.ShowDialog(m_EditControl);
SSDBCombo ssdb = context.Instance as SSDBCombo;
if (ssdb == null)
ssdb = new SSDBCombo();
ssdb.DBColumnsCols = m_EditControl.SYCol;
return m_EditControl.SYCol; //The error occurs here
}
}
return base.EditValue(context, provider, value);
}

public class UserTypeConverter : ExpandableObjectConverter
{

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string) || sourceType == typeof(WindowsApplication1.SYColumns))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string) | destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}

public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)

{
SYColumns cCol = new SYColumns();
if (value == null)
return cCol;
if (value is string)
{
string[] strValues = value.ToString().Split(new char[] { ';' });
cCol.ColsCount = int.Parse(strValues[0].ToString().Substring(strValues[0].IndexOf(":") + 1).Trim());
cCol.ColHeaderText = strValues[2].ToString().Substring(strValues[2].IndexOf(":") + 1).Trim();
cCol.ColNames = strValues[1].ToString().Substring(strValues[1].IndexOf(":") + 1).Trim();
return cCol;
return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
string strRet = "";
if (value.GetType().GetProperty("ColNames").GetValue(value, null) == null)
{
value.GetType().GetProperty("ColsCount").SetValue(value, 0, null);
value.GetType().GetProperty("ColNames").SetValue(value, "NA", null);
value.GetType().GetProperty("ColHeaderText").SetValue(value, "NA", null);
return "Items: " + "0" + ";ColNames: " + "NA" + ";ColHeaders: " + "NA";
}
string ColNames = value.GetType().GetProperty("ColNames").GetValue(value, null).ToString();
string HeaderText = value.GetType().GetProperty("ColHeaderText").GetValue(value, null).ToString();
int ColsCount = int.Parse(value.GetType().GetProperty("ColsCount").GetValue(value, null).ToString());
value.GetType().GetProperty("ColsCount").SetValue(value, ColsCount, null);
value.GetType().GetProperty("ColNames").SetValue(value, ColNames, null);
value.GetType().GetProperty("ColHeaderText").SetValue(value, HeaderText, null);
strRet = "Items: " + value.GetType().GetProperty("ColsCount").GetValue(value, null).ToString() + ";ColNames: " + value.GetType().GetProperty("ColNames").GetValue(value, null).ToString() + ";ColHeaders: " + value.GetType().GetProperty("ColHeaderText").GetValue(value, null).ToString();
return strRet;
}

public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}

public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
SYColumns sycol = new SYColumns();
sycol.ColHeaderText = (string)propertyValues["ColHeaderText"];
sycol.ColNames = (string)propertyValues["ColNames"];
sycol.ColsCount = (int)propertyValues["ColsCount"];
return sycol;
}
}

[TypeConverter(typeof(UserTypeConverter))]
public class SYColumns
{}

}

From http://www.developmentnow.com/g/32_0_0_0_0_0/dotnet-framework-windowsforms-designtime.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

0 new messages