我正在尝试定义在Windows窗体上填充ComboBox控件的通用方法。我通常使用Dictionary来填充我的组合框,将它们绑定到数据源。由于我有一些,我想创建一个方法,我可以调用键,值对的任意组合。这是我的尝试:
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";
}
}
我希望能够这样称呼它:
PopulateDropdown(cbPrinters, this.inputData.Printers);
其中 this.inputData.Printers
是一个字典,其键/值类型为字符串,字符串,但我也有其他组合,如字符串,整数或字符串,对象。
我收到的错误是无法将字符串,字符串转换为Type,Type。我怎样才能解决这个问题?
答案 0 :(得分:6)
您需要使用通用参数声明您的方法:
private void PopulateDropdown<TKey,TValue>(ComboBox control, Dictionary<TKey, TValue> dict)
{
}
答案 1 :(得分:-1)
您必须向该功能添加模板:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Everything - everything you'll want is here.</title>
</head>
<body>
<script src="js/chronometer.js" charset="utf-8"></script>
<div class="w3-row w3-container">
<div class="w3-col m3 w3-text-red">
<p> </p>
</div>
<div class="w3-col m6 w3-center w3-text-white w3-xxlarge">
<p>
<i>Everything you'll want is here.</i>
</p>
</div>
<div class="w3-col m3 w3-text-red">
<p> </p>
</div>
</div>
<div class="w3-container w3-row">
<div class="w3-col m3 w3-text-red">
<p> </p>
</div>
<div class="w3-col m6 w3-center w3-xlarge w3-white w3-text-grey">
<p>
Chronometer
</p>
</div>
<div class="w3-col m3 w3-text-red">
<p> </p>
</div>
</div>
<div class="w3-container w3-row">
<div class="w3-col m3 w3-text-red">
<p> </p>
</div>
<div class="w3-col m6 w3-center w3-white w3-text-grey">
<input id="counter" type="text" name="name" value="00:00:00" readonly="readonly" class="w3-text-grey w3-center">
<br>
<p> </p>
<input id="start" type="button" name="name" value="Start!" class="w3-btn w3-green" onclick="startchrono()">
<input id="reset" type="button" name="name" value="Reset!" class="w3-btn w3-blue" onclick="reset();">
<p> </p>
</div>
<div class="w3-col m3 w3-text-red">
<p> </p>
</div>
</div>
</body>
</html>