I am trying to define a generic method for populating ComboBox controls on Windows Forms. I am usually using Dictionary to populate my combo boxes by binding them to the Data Source. Since I have a handful of those, I wanted to make a method that I can just call on any combination of a key, value pairs. Here's my attempt:
private void PopulateDropdown(ComboBox control, Dictionary<Type, Type> dict)
{
if (dict.Count > 0)
{
control.DataSource = new BindingSource(dict, null);
control.DisplayMember = "Key";
control.ValueMember = "Value";
}
}
I would like to be able to call it like so:
PopulateDropdown(cbPrinters, this.inputData.Printers); where this.inputData.Printers is a Dictionary with key/value types as string, string but I also have other combinations like string, int or string,object.
I am getting an error that cannot convert string, string to Type, Type. How can I fix this?
You need to declare your method with generic parameters:
private void PopulateDropdown<TKey,TValue>(ComboBox control, Dictionary<TKey, TValue> dict)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With