Jogesh Grover

Hi,

Propertygrid shows 0 bydefault for int and double datatype properties.

So is there any way to show blank textbox in propertygrid for the properties with int and double datatype as it comes for string datatype properties if default value is empty.




Re: Windows Forms Designer Showing blank textbox for int and double datatype properties in propertygrid

Zhi-Xin Ye - MSFT

You can derive from the TypeConveter class to make a Custom type converter, take the following steps:

1). Define a class that derives from TypeConverter.

2). Override the CanConvertFrom method that specifies which type the converter can convert from.

3). Override the ConvertFrom method, convert the value user input from string to int.

4). Override the CanConvertTo method that specifies which type the converter can convert to.

5). Override the ConvertTo method, convert the property value which is of int type to string type.

I write the following sample for your information, hope it helps.



Code Snippet

namespace Sample18

{

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

private void Form3_Load(object sender, EventArgs e)

{

this.propertyGrid1.SelectedObject = new a();

}

}

class myTypeConverter : TypeConverter

{

private bool isNull = true;

public override object ConvertFrom(ITypeDescriptorContext context,

System.Globalization.CultureInfo culture, object value)

{

if (value.ToString() == "")

{

isNull = true;

return 0;

}

else

{

isNull = false;

return Convert.ToInt32(value);

}

}

public override object ConvertTo(ITypeDescriptorContext context,

System.Globalization.CultureInfo culture, object value, Type destinationType)

{

if (Convert.ToInt32(value) == 0)

{

return isNull "" : "0";

}

return value.ToString();

}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)

{

if (sourceType == typeof(string))

{

return true;

}

return base.CanConvertFrom(context, sourceType);

}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)

{

if (destinationType == typeof(int))

{

return true;

}

return base.CanConvertTo(context, destinationType);

}

}

class a

{

private int m_int;

private int m_int2;

private string m_string;

[TypeConverter(typeof(myTypeConverter))]

public int M_INT

{

get { return m_int; }

set { m_int = value; }

}

[TypeConverter(typeof(myTypeConverter))]

public int M_INT2

{

get { return m_int2; }

set { m_int2 = value; }

}

public string M_String

{

get { return m_string; }

set { m_string = value; }

}

}

}






Re: Windows Forms Designer Showing blank textbox for int and double datatype properties in propertygrid

Jogesh Grover

Thanks Zhi,

Your post was really helpful...