TrungNV
TrungNV

Reputation: 299

[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?

Upvotes: -1

Views: 146

Answers (1)

Karen Payne
Karen Payne

Reputation: 5157

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.

Upvotes: 0

Related Questions