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 ;}
}