简体   繁体   中英

Winforms ComboBox User Control Binding

I have a user control that inherited from Combobox control. I want to bind data in the constructor of the user control. but when I add it to form and run the project it shows duplicated items.

When I add control to my winform its add items in the Designer file of form and when I run project it added again in constructor of user control.

public partial class CheckSeriesBox : ComboBox
{
    private static List<string> CheckSeries;

    public CheckSeriesBox()
    {
        InitializeComponent();

        CheckSeries = new List<string>();
        SetCheckSeries();

        this.Items.AddRange(CheckSeries.ToArray());
        this.SelectedIndex = 0;
    }

    public static List<string> SetCheckSeries()
    {
        CheckSeries.Add("A");
        CheckSeries.Add("B");
    }
}

http://social.msdn.microsoft.com/forums/vstudio/en-US/3e35b534-7d3f-4832-8859-b5cb838bd62a/extended-combobox-adds-items-twice

public partial class CheckSeriesBox : ComboBox
{
    private static List<string> CheckSeries;

    public CheckSeriesBox()
    {
        InitializeComponent();

        CheckSeries = new List<string>();
        SetCheckSeries();

        if (DesignMode)
        {
             this.Items.AddRange(CheckSeries.ToArray());
        }
    }

    public static List<string> SetCheckSeries()
    {
        CheckSeries.Add("A");
        CheckSeries.Add("B");
    }

    protected new bool DesignMode
    {
        get
        {
            if (base.DesignMode)
            {
                return true;
            }
            else
            {
                Control parent = this.Parent;
                while ((parent != null))
                {
                    System.ComponentModel.ISite site = parent.Site;
                    if ((site != null) && site.DesignMode)
                    {
                        return true;
                    }
                    parent = parent.Parent;
                }
                return false;
            }
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM