I am creating a custom control derived from ComboBox. I add some items to custom control in constructor of control.
// this code is sample
public MyCustomControl
{
this.Items.Add("test1");
this.Items.Add("test2");
}
When I drag this custom control to my form, designer auto-generate the code:
// this code in Form1.Designer.cs
this.mycustomControls.Items.AddRange(new object[] {
"test1",
"test2"});
When this custom control display on Form1, will have 4 items:
test1
test2
test1
test2
This problem cause duplication.
Please help me how to solve this problem, I don't want designer auto-generate these codes. Thanks.
Loading
Kirtan PatelPosted Sep 1, 2009, 9:39 AM
you can restrict duplication by Checking for existing items before adding it to combobox
Code is Below For it
dont forget to mark "Do you like answer" if answer helped you please it will give me some credits:)
private void button2_Click(object sender, EventArgs e)
{
//Check for Existence before Adding Items to ComboBox
if ( comboBox1.Items.Contains(textBox1.Text) == false )
{
// Add Item to Combo Box
comboBox1.Items.Add(textBox1.Text.Trim());
}
}