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

Source: Internet
Author: User
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 have tried it. I cannot add [browsable (false)] To the attribute.
My code is as 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.

Members marked by setting designonlyattribute to true 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 ).

Members without attributes or marked by setting designonlyattribute to false can be set at runtime. The default value is 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 ;}
}

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.