How to Prevent the. Net Control from generating relevant code in initializecomponent () during design

Source: Internet
Author: User
[Source] http://www.joyblog.cn/article.asp? Id = 47
Some public attributes of components do not want to be added to the initializecomponent () method by vs during design. How can this problem be solved? I tried to add the attribute [browsable ( False .
MyCodeAs follows:
/**/ /// <Summary>
///Controller communication type drop-down list box.
/// </Summary>
Public   Class Communicationtypecombobox: ComboBox
{
/**/ /// <Summary>
///Create a ListBox instance.
/// </Summary>
Public Communicationtypecombobox ()
{
Items. Add ("Serial Port");
Items. Add ("TCP");
}

/**/ /// <Summary>
///Obtain all items in the list box.
/// </Summary>
[Browsable ( False )]
Public   New Objectcollection items
{
Get {Return Base. Items ;}
}
}

 
Place the control on the form. vs automatically adds the code to the initializecomponent () method. Bold.
 
//  
// Cmbcommunicationtype
//  
This . Cmbcommunicationtype. dropdownstyle = System. Windows. Forms. comboboxstyle. dropdownlist;
This . Cmbcommunicationtype. formattingenabled =   True ;
This . Cmbcommunicationtype. Items. addrange ( New   Object [] {
"Serial Port",
"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 );

 
This will happen as the number of edits increases. Is there any way to solve this problem except adding items in the constructor?
 
This . Cmbcommunicationtype. Items. addrange ( New   Object [] {
" Serial Port " ,
" TCP " ,
" Serial Port " ,
" TCP " ,
" Serial Port " ,
" TCP " ,
" Serial Port " ,
" TCP " ,
" Serial Port " ,
" TCP " } );
 
After several days of efforts, I finally found the designonlyattribute class.
Specifies whether a property can only be set at design time.

Set designonlyattribute True The marked members can only be set at design time. Generally, these properties can only exist at design time, and do not correspond to a certain actual property on the runtime object ).

No attribute is available or you can set designonlyattribute False The marked members can be set at runtime. Default Value: False .

This problem can be solved perfectly by adding the items attribute of communicationtypecombobox with the designonlyattribute.

/**/ /// <Summary>
///Obtain all items in the list box.
/// </Summary>
[Designonly ( False )]
Public   New Objectcollection items
{
Get {Return Base. Items ;}
}


2006 - 9 - 15 Daily update

Ultimate and Solutions

  /**/ /// <Summary>
///Obtain all items in the list box.
/// </Summary>
[Designerserializationvisibility (designerserializationvisibility. Hidden)]
[Browsable ( False )]
Public   New Objectcollection items
  {
Get {Return Base. Items ;}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.