How to create custom controls containing populated lists?
How to properly create cus开发者_开发百科tom controls containing populated lists?
For example: I need a combobox control which contains a list of products, then I would reuse this control in other project.
My code is this:
Public Class ProductCombo
Inherits ComboBox
Public Sub New()
Me.Items.Add("Product 1")
Me.Items.Add("Product 2")
End Sub
End Class
and it works but when I run the project each item is duplicated - this is because list is populated twice. First when user add a control to form, second by designer when project is started.
I already found a solution to prevent adding items in edit mode, but this is not what I need. I would like to have them existing in edit mode so they could be edited at this point.
Is there a solution to this problem?
protected override void OnCreateControl()
{
base.OnCreateControl();
if (Items.Count == 0)
{
Items.Add("Product 1");
Items.Add("Product 2");
}
}
精彩评论