組件的一些公共屬性不希望被vs在設計時加到initializecomponent()方法中怎么處理呢?我試過了,將屬性加上[browsable(false)]也不行。
我的代碼如下:
/// <summary>
/// 控制器通訊類型下拉列表框。
/// </summary>
public class communicationtypecombobox : combobox
{
/// <summary>
/// 構(gòu)造列表框?qū)嵗?br> /// </summary>
public communicationtypecombobox()
{
items.add("串口");
items.add("tcp");
}
/// <summary>
/// 獲取列表框中的所有項。
/// </summary>
[browsable(false)]
public new objectcollection items
{
get { return base.items; }
}
}
將控件放到窗體上,vs回自動在initializecomponent()方法中加入一下代碼。粗體部分。
//
// cmbcommunicationtype
//
this.cmbcommunicationtype.dropdownstyle = system.windows.forms.comboboxstyle.dropdownlist;
this.cmbcommunicationtype.formattingenabled = true;
this.cmbcommunicationtype.items.addrange(new object[] {
"串口",
"tcp"});
this.cmbcommunicationtype.location = new system.drawing.point(124, 66);
this.cmbcommunicationtype.name = "cmbcommunicationtype";
this.cmbcommunicationtype.selecteditem = xunmei.door.communicationtype.serialport;
this.cmbcommunicationtype.size = new system.drawing.size(121, 20);
this.cmbcommunicationtype.tabindex = 2;
this.cmbcommunicationtype.selectedindexchanged += new system.eventhandler(this.cmbcommunicationtype_selectedindexchanged);
隨著編輯次數(shù)的增會變成這樣。除了不在構(gòu)造函數(shù)中增加項以外,有沒有辦法解決這個問題?
this.cmbcommunicationtype.items.addrange(new object[] {
"串口",
"tcp",
"串口",
"tcp",
"串口",
"tcp",
"串口",
"tcp",
"串口",
"tcp"});
經(jīng)過幾天的努力終于找到了designonlyattribute 類 。
指定某個屬性 (property) 是否只能在設計時設置。
通過將 designonlyattribute 設置為 true 進行標記的成員只能在設計時進行設置。通常,這些屬性 (property) 只能在設計時存在,并且不對應于運行時對象上的某個實際屬性 (property)。
沒有屬性 (attribute) 或通過將 designonlyattribute 設置為 false 進行標記的成員可以在運行時進行設置。默認為 false。
將communicationtypecombobox的items屬性加上designonlyattribute 就可以完美解決該問題。
/// <summary>
/// 獲取列表框中的所有項。
/// </summary>
[designonly(false)]
public new objectcollection items
{
get { return base.items; }
}
新聞熱點
疑難解答
圖片精選