改變 propertygrid 控件的編輯風格(4)——加入選擇列表
張昱[email protected] 效果:
適用場合:
限制選擇輸入
步驟一:定義從uitypeeditor 繼承的抽象類:comboboxitemtypeconvert。示例如下:
using system;
using system.collections;
using system.componentmodel;
namespace blog.csdn.net.zhangyuk
{
/// imstypeconvert 的摘要說明。
/// </summary>
public abstract class comboboxitemtypeconvert : typeconverter
{
public hashtable _hash = null;
public comboboxitemtypeconvert()
{
_hash = new hashtable();
getconverthash();
}
public abstract void getconverthash();
public override bool getstandardvaluessupported( itypedescriptorcontext context )
{
return true;
}
public override standardvaluescollection getstandardvalues(
itypedescriptorcontext context)
{
int [] ids = new int [ _hash.values.count ];
int i=0;
foreach (dictionaryentry myde in _hash)
{
ids[i++] = (int)(myde.key);
}
return new standardvaluescollection( ids );
}
public override bool canconvertfrom( itypedescriptorcontext context, type sourcetype)
{
if (sourcetype == typeof(string))
{
return true;
}
return base.canconvertfrom(context, sourcetype);
}
public override object convertfrom(
itypedescriptorcontext context,
system.globalization.cultureinfo culture,
object v )
{
if (v is string)
{
foreach (dictionaryentry myde in _hash)
{
if( myde.value.equals((v.tostring())) )
return myde.key;
}
}
return base.convertfrom(context, culture, v);
}
public override object convertto(
itypedescriptorcontext context,
system.globalization.cultureinfo culture, object v ,
type destinationtype)
{
if (destinationtype == typeof(string))
{
foreach (dictionaryentry myde in _hash)
{
if( myde.key.equals(v) )
return myde.value.tostring();
}
return "";
}
return base.convertto(context, culture, v, destinationtype);
}
public override bool getstandardvaluesexclusive(
itypedescriptorcontext context)
{
return false;
}
}
}
步驟二:定義 comboboxitemtypeconvert 的派生類,派生類中實現父類的抽象方法:
public abstract void getconverthash();
示例如下:
using system;
using system.collections;
using system.componentmodel;
namespace blog.csdn.net.zhangyuk
{
public class propertygridboolitem : comboboxitemtypeconvert
{
public override void getconverthash()
{
_hash.add(0,"是");
_hash.add(1,"否");
}
}
public class propertygridcomboboxitem : comboboxitemtypeconvert
{
public override void getconverthash()
{
_hash.add(0,"炒肝");
_hash.add(1,"豆汁");
_hash.add(2,"灌腸");
}
}
}
步驟三:編輯屬性類,指定編輯屬性。示例如下:
namespace blog.csdn.net.zhangyuk
{
public class someproperties
{
private string _finished_time = "";
……
// 布爾
bool _bool = true;
[
description("布爾"),
category("屬性"),
typeconverter(typeof( propertygridboolitem ))
]
public int 布爾
{
get { return _bool == true ? 0 : 1; }
set { _bool = (value == 0 ? true : false); }
}
// 選擇列表
int _comboboxitems = 0;
[
description("選擇列表"),
category("屬性"),
typeconverter(typeof( propertygridcomboboxitem ))
]
public int 選擇列表
{
get { return _comboboxitems; }
set { _comboboxitems = value; }
}
……
}
}
步驟四:設置propertygrid的屬性對象。示例如下:
private void form1_load(object sender, system.eventargs e)
{
this.propertygrid1.selectedobject = new someproperties(); }
收集最實用的網頁特效代碼!
新聞熱點
疑難解答