Home > Software design >  ComboBox DisplayMember, ValueMember, and DataSource Method
ComboBox DisplayMember, ValueMember, and DataSource Method

Time:12-16

I am working on a c# winforms program that has three comboboxes, each combobox has a different displaymember, valuemember, and datasource. I would like to try to create a method in which I can use to set the member values and datasource that works for all three comboboxes

here is what I am currently using for the comboboxes

private void kitLoad (string kitDisplay, string kitValue, object kitSource)
{
     kits_comboBox.DisplayMember = kitDisplay;
     kits_comboBox.ValueMember = kitValue;
     kits_comboBox.DataSource = kitSource;
}
private void pspLoad(string pspDisplay, string pspValue, object pspSource)
{
     psp_comboBox.DisplayMember = pspDisplay;
     psp_comboBox.ValueMember = pspValue;
     psp_comboBox.DataSource = pspSource;
}
private void plateLoad(string plateDisplay, string plateValue, object plateSource)
{
     plates_comboBox.DisplayMember = plateDisplay;
     plates_comboBox.ValueMember = plateValue;
     plates_comboBox.DataSource = plateSource;
}

It does work, But I feel like I could compress all of that into one method, any help is greatly appreciated.

CodePudding user response:

just pass your combo along

private void LoadCombo(ComboBox cbo, string disp, string val, object data)
{
     cbo.DisplayMember = disp;
     cbo.ValueMember = val;
     cbo.DataSource = data;
}

will work for any combo box

LoadCombo(kits_comboBox, "displayProperty", "valueProperty", data);
  • Related