C#:如何通过 Enum 作为键将 HashTable 绑定到 ComboBox?
public static Hashtable m_results = new Hashtable();
private BindingSource m_bindResults = new BindingSource();
// in static constructor
m_results.Add(MyResultTypes.Failed, "Failed");
m_results.Add(MyResultTypes.Pending, "Is Pending");
m_results.Add(MyResultTypes.Completed, "Was Completed");
m_results.Add(MyResultTypes.Cancel, "Cancel it");
m_defaultResult = MyResultTypes.Pending;
// in instance constructor
m_bindResults.DataSource = m_results;
comboResult.DataSource = m_bindResults;
comboResult.ValueMember = "Key";
comboResult.DisplayMember = "Value";
comboResult.SelectedValue = m_defaultTimeoutResult;
上面的代码不起作用:)它使用字符串作为哈希表中的键而不是枚举 MyResultTypes,并且它可以工作。现在发生的情况是组合框填充了哈希表的值(如我想要的),但未选择默认选定的值。
在此示例中如何使用枚举?谢谢
编辑:抱歉,ComboTOResult 是comboResult,错过了
编辑2:抱歉,它确实有效。我的坏
public static Hashtable m_results = new Hashtable();
private BindingSource m_bindResults = new BindingSource();
// in static constructor
m_results.Add(MyResultTypes.Failed, "Failed");
m_results.Add(MyResultTypes.Pending, "Is Pending");
m_results.Add(MyResultTypes.Completed, "Was Completed");
m_results.Add(MyResultTypes.Cancel, "Cancel it");
m_defaultResult = MyResultTypes.Pending;
// in instance constructor
m_bindResults.DataSource = m_results;
comboResult.DataSource = m_bindResults;
comboResult.ValueMember = "Key";
comboResult.DisplayMember = "Value";
comboResult.SelectedValue = m_defaultTimeoutResult;
Above code diesn't work :) It use to use strings for keys in hashtable instead of enum MyResultTypes, and it was working. What happens now is the combo box gets filled with values of a hashtable (as I want), but the default selected value isn't being selected.
How can I use enums in this example? thanks
Edit: Sorry, ComboTOResult was comboResult, missed it
Edit 2: Sorry, it does work. My bad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我将最后一行更改为
ComboTOResult 时对我有用,也许是一个不同的框?
Works for me when I change the last line to
ComboTOResult maybe a different box?