Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ComboBox DisplayMember, ValueMember, and DataSource Method

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading