国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

如何讓.Net控件在設(shè)計(jì)時(shí)InitializeComponent()中不生成相關(guān)代碼

2019-11-18 17:17:27
字體:
供稿:網(wǎng)友

組件的一些公共屬性不希望被VS在設(shè)計(jì)時(shí)加到InitializeComponent()方法中怎么處理呢?我試過了,將屬性加上[Browsable(false)]也不行。
我的代碼如下:
    /// <summary>
    /// 控制器通訊類型下拉列表框。
    /// </summary>
    public class CommunicationTypeComboBox : ComboBox
    {
        /// <summary>
        /// 構(gòu)造列表框?qū)嵗?BR>        /// </summary>
        public CommunicationTypeComboBox()
        {
            Items.Add("串口");
            Items.Add("TCP");
        }

        /// <summary>
        /// 獲取列表框中的所有項(xiàng)。
        /// </summary>
        [Browsable(false)]
        public new ObjectCollection Items
        {
            get { return base.Items; }
        }
    }

 
將控件放到窗體上,VS回自動(dòng)在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ù)的增會(huì)變成這樣。除了不在構(gòu)造函數(shù)中增加項(xiàng)以外,有沒有辦法解決這個(gè)問題?
 
this.cmbCommunicationType.Items.AddRange(new object[] {
            "串口",
            "TCP",
            "串口",
            "TCP",
            "串口",
            "TCP",
            "串口",
            "TCP",
            "串口",
            "TCP"});
 
經(jīng)過幾天的努力終于找到了DesignOnlyAttribute 類 。
指定某個(gè)屬性 (PRoperty) 是否只能在設(shè)計(jì)時(shí)設(shè)置。

通過將 DesignOnlyAttribute 設(shè)置為 true 進(jìn)行標(biāo)記的成員只能在設(shè)計(jì)時(shí)進(jìn)行設(shè)置。通常,這些屬性 (Property) 只能在設(shè)計(jì)時(shí)存在,并且不對應(yīng)于運(yùn)行時(shí)對象上的某個(gè)實(shí)際屬性 (Property)。

沒有屬性 (Attribute) 或通過將 DesignOnlyAttribute 設(shè)置為 false 進(jìn)行標(biāo)記的成員可以在運(yùn)行時(shí)進(jìn)行設(shè)置。默認(rèn)為 false。

將CommunicationTypeComboBox的Items屬性加上DesignOnlyAttribute 就可以完美解決該問題。

        /// <summary>
        /// 獲取列表框中的所有項(xiàng)。
        /// </summary>
        [DesignOnly(false)]
        public new ObjectCollection Items
        {
            get { return base.Items; }
        }

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临沂市| 宜宾县| 石门县| 论坛| 沈阳市| 中宁县| 莱芜市| 轮台县| 玉林市| 英吉沙县| 榆中县| 城固县| 枣阳市| 烟台市| 济阳县| 茂名市| 田阳县| 广水市| 称多县| 花莲县| 达孜县| 巴东县| 正宁县| 巴彦淖尔市| 岳普湖县| 陆良县| 南投市| 高台县| 治多县| 海南省| 樟树市| 鄂托克旗| 镇赉县| 汕尾市| 金华市| 盐边县| 曲阳县| 阳信县| 炉霍县| 大关县| 临泽县|