I've created a class for a telephone number containing the properties: country code, area code, prefix, and suffix
I've created a cusom control and included the phone number class as a property, but it's grey'd out in the property grid for the control when I place it on a Winform.
I was hoping someone could explain how to make the phone number class appear as other types such as a Point where you can expand the point property and you'll see the individual properties X and Y.
Thanx in advance
Gordon JirouxPosted Aug 24, 2007, 4:25 PM
thanx. It worked like a champ...
for other people that need to know...
the phone # class & converter code looks like this:
namespace AFIGclasses{
[Serializable]
public class PhoneNumber
{
private string _CountryCode = string.Empty;
private string _AreaCode = string.Empty;
private string _Prefix = string.Empty;
private string _Suffix = string.Empty;
public PhoneNumber() { }
public PhoneNumber(string AreaCode, string Prefix, string Suffix)
{
_AreaCode = AreaCode;
_Prefix = Prefix;
_Suffix = Suffix;
}
public PhoneNumber(int AreaCode, int Prefix, int Suffix)
{
_AreaCode = AreaCode.ToString();
_Prefix = Prefix.ToString();
_Suffix = Suffix.ToString();
}
public PhoneNumber(string TenDigitNumber)
{
_AreaCode = TenDigitNumber.Substring(0, 3);
_Prefix = TenDigitNumber.Substring(3, 3);
_Suffix = TenDigitNumber.Substring(5, 4);
}
public PhoneNumber(string CountryCode, string AreaCode, string Prefix, string Suffix)
{
_CountryCode = "+" + CountryCode;
_AreaCode = AreaCode;
_Prefix = Prefix;
_Suffix = Suffix;
}
public PhoneNumber(int CountryCode, int AreaCode, int Prefix, int Suffix)
{
_CountryCode = "+" + CountryCode.ToString();
_AreaCode = AreaCode.ToString();
_Prefix = Prefix.ToString();
_Suffix = Suffix.ToString();
}
public PhoneNumber(string CountryCode, string TenDigitNumber)
{
_CountryCode = "+" + CountryCode;
_AreaCode = TenDigitNumber.Substring(0, 3);
_Prefix = TenDigitNumber.Substring(3, 3);
_Suffix = TenDigitNumber.Substring(6, 4);
}
public override string ToString()
{
string output = string.Empty;
if (_CountryCode != string.Empty)
{
output = _CountryCode + " ";
}
output += "(" + _AreaCode + ")" + " " + _Prefix + "-" + _Suffix;
return output;
}
public string CountryCode
{
get { return _CountryCode; }
set { _CountryCode = value; }
}
public string AreaCode
{
get { return _AreaCode; }
set { _AreaCode = value; }
}
public string Prefix
{
get { return _Prefix; }
set { _Prefix = value; }
}
public string Suffix
{
get { return _Suffix; }
set { _Suffix = value; }
}
}
public class PhoneNumberConverter : ExpandableObjectConverter
{
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
PhoneNumber phonenumber = new PhoneNumber();
phonenumber.CountryCode = propertyValues[(object)"CountryCode"].ToString();
phonenumber.AreaCode = propertyValues[(object)"AreaCode"].ToString();
phonenumber.Prefix = propertyValues[(object)"Prefix"].ToString();
phonenumber.Suffix = propertyValues[(object)"Suffix"].ToString();
return (object)phonenumber;
}
}
}
the code on the control looks like this:
private AFIGclasses.PhoneNumber _Number = new AFIGclasses.PhoneNumber("1", "999", "555", "1212");
[Description("A Telephone Number"),TypeConverter(typeof(PhoneNumberConverter))]
public AFIGclasses.PhoneNumber Number
{
get { return _Number; }
set { _Number = value; }
}
private string _Text;
public string LabelText
{
get { return _Text; }
set
{
_Text = value;
lblTag.Text = value;
}
}
Louis KenneyPosted Aug 24, 2007, 3:59 PM
Hi,
this (http://www.codeproject.com/csharp/PropertyEditor.asp) shows an example for your problem.
Hope it helps,
Louis