Windows Develop Bookmark and Share   
 index > Windows Forms General > Internally Initialize Derived ComboBox Items List?
 

Internally Initialize Derived ComboBox Items List?

I have a class that derives from ComboBox. I want to fill the Items list inside its constructor. Problem is, when I add it to another control (using the designer) the initialization gets put in the parent’s InitializeComponent() method. This doubles the items in the list.


How to stop the designer from doing this?


If this is not posible then:


What is the best practice for Initializing an Items list in a child class that derives from ComboBox?

ElectricBliss  Monday, July 14, 2008 12:26 AM
Ensure the items are only ever added once:

using System;
using System.Windows.Forms;

public class MyCombo : ComboBox {
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
if (this.DesignMode && this.Items.Count == 0) {
this.Items.Add("one");
this.Items.Add("two");
this.Items.Add("three");
}
}
}
nobugz  Tuesday, July 15, 2008 6:33 PM
If you dont need the items there in Design-Time, then you can make your Combo implement ISupportInitialize and add the items in the EndInit method.

hth,
Lior.
Lior Salem  Tuesday, July 15, 2008 7:25 PM
Ensure the items are only ever added once:

using System;
using System.Windows.Forms;

public class MyCombo : ComboBox {
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
if (this.DesignMode && this.Items.Count == 0) {
this.Items.Add("one");
this.Items.Add("two");
this.Items.Add("three");
}
}
}
nobugz  Tuesday, July 15, 2008 6:33 PM
If you dont need the items there in Design-Time, then you can make your Combo implement ISupportInitialize and add the items in the EndInit method.

hth,
Lior.
Lior Salem  Tuesday, July 15, 2008 7:25 PM

You can use google to search for other answers

Custom Search

More Threads

• Resizing a toolstrip
• Icon leak in File Explorer demo?
• Setup Form to be Only Loaded Once
• VB.NET 9 vs C# 3.5 TimeZoneInfo.GetSystemTimeZones().OrderBy
• Deploying Vb.Net application
• How to make a function sleep?
• Maintaining separate menus for MDI Parent and Child
• Delagates
• Announcing vbdotnetforums.com
• component in IE binging to .NET 1.1