简体   繁体   中英

[WinForm]Combobox in Tabpage

I have problem when create tab page and add combo box dynamic. Problem is, when select one value in combo box in a tab page, value of combo box in all tab page changed as value just selected in combo box.

How to prevent, value of combo box in other tab page auto change?

Suppose you were to setup the ComboBox controls like the following (does not matter how the controls are created) each ComboBox does not affect others.

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        Shown += OnShown;
    }

    private void OnShown(object? sender, EventArgs e)
    {
        // changing one does not affect others
        comboBox1.DataSource = MonthNames;
        comboBox2.DataSource = MonthNames;
    }

    private List<string> MonthNames 
        => DateTimeFormatInfo.CurrentInfo.MonthNames.Take(12).ToList();
}

While this approach when changing one ComboBox will change the other one.

public partial class Form1 : Form
{
    private readonly BindingSource _bindingSource = new ();
    public Form1()
    {
        InitializeComponent();

        Shown += OnShown;
    }

    private void OnShown(object? sender, EventArgs e)
    {
        _bindingSource.DataSource = MonthNames;
        comboBox1.DataSource = _bindingSource;
        comboBox2.DataSource = _bindingSource;
    }

    private List<string> MonthNames 
        => DateTimeFormatInfo.CurrentInfo.MonthNames.Take(12).ToList();
}

In short the above should provide insight even if the way controls are created focus on how each ComboBox is loaded.

If the above does not help than you need to provide a small code sample, enough so we know how you are setting up the controls.

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