C 實現非列舉型別的在屬性控制項中可下拉選擇(二)

2021-10-04 02:13:18 字數 3805 閱讀 5553

前言:相較於之前文章,用這種方式優點是可以修改在屬性控制項上顯示的效果,然後重新繫結就可以了

property p1 = new property("自定義", "名稱", "name", "", "bridge", false, true);

p1.converter = new dropdownlistconverter("bridge", "hani", "jodan");

propertygridproperty.add(p1);

property p2 = new property("自定義", "年齡", "age", "", 100, false, true);

propertygridproperty.add(p2);

property p3 = new property("自定義", "籍貫", "location", "", "sz", false, true);

p3.converter = new dropdownlistconverter("sz", "gz", "bj");

propertygridproperty.add(p3);

this.propertygrid1.selectedobject = propertygridproperty;

using system;

using system.collections.generic;

using system.componentmodel;

using system.linq;

namespace attributetest}}

public void remove(property value)

}public property this[int index]

set}

public property this[object name]

set}

#region icustomtypedescriptor 成員

public attributecollection getattributes()

public string getclassname()

public string getcomponentname()

public typeconverter getconverter()

public eventdescriptor getdefaultevent()

public propertydescriptor getdefaultproperty()

public object geteditor(type editorbasetype)

public eventdescriptorcollection getevents(attribute attributes)

public eventdescriptorcollection getevents()

public propertydescriptorcollection getproperties(attribute attributes)

return new propertydescriptorcollection(newprops);

}public propertydescriptorcollection getproperties()

public object getpropertyowner(propertydescriptor pd)

#endregion

}/// /// 屬性類

///

public class property

/// /// 屬性所屬類別

///

public string category

/// /// 屬性顯示名稱

///

public string displayname

/// /// 屬性名

///

public string name

/// /// 描述

///

public string description

/// /// 屬性值

///

public object value

/// /// 是否為唯讀

///

public bool readonly

/// /// 是否可見

///

public bool visible

/// /// 型別轉換器,在製作下拉列表時需要用到

///

public typeconverter converter

/// /// 屬性編輯器

///

public virtual object editor

}public class custompropertydescriptor : propertydescriptor

#region propertydescriptor 重寫方法

public override bool canresetvalue(object component)

public override type componenttype

}public override object getvalue(object component)

public override string description

}public override string category

}public override string displayname

}public override bool isreadonly

}public override void resetvalue(object component)

public override bool shouldserializevalue(object component)

public override void setvalue(object component, object value)

public override typeconverter converter

}public override type propertytype

}public override object geteditor(type editorbasetype)

#endregion

}/// /// 下拉框型別轉換器

///

public class dropdownlistconverter : stringconverter

public override bool getstandardvaluessupported(itypedescriptorcontext context)

public override bool getstandardvalue***clusive(itypedescriptorcontext context)

public override standardvaluescollection getstandardvalues(itypedescriptorcontext context)}}

C 實現非列舉型別的在屬性控制項中可下拉選擇(二)

前言 相較於之前文章,用這種方式優點是可以修改在屬性控制項上顯示的效果,然後重新繫結就可以了 property p1 new property 自定義 名稱 name bridge false,true p1.converter new dropdownlistconverter bridge ha...

enum 在c中的使用 列舉型別)

如果乙個變數你需要幾種可能存在的值,那麼就可以被定義成為列舉型別。之所以叫列舉就是說將變數或者叫物件可能存在的情況也可以說是可能的值一一例舉出來。舉個例子來說明一吧,為了讓大家更明白一點,比如乙個鉛筆盒中有一支筆,但在沒有開啟之前你並不知道它是什麼筆,可能是鉛筆也可能是鋼筆,這裡有兩種可能,那麼你就...

enum 在c中的使用 列舉型別)

如果乙個變數你需要幾種可能存在的值,那麼就可以被定義成為列舉型別。之所以叫列舉就是說將變數或者叫物件可能存在的情況也可以說是可能的值一一例舉出來。舉個例子來說明一吧,為了讓大家更明白一點,比如乙個鉛筆盒中有一支筆,但在沒有開啟之前你並不知道它是什麼筆,可能是鉛筆也可能是鋼筆,這裡有兩種可能,那麼你就...