C#:如何通过 Enum 作为键将 HashTable 绑定到 ComboBox?

发布于 2024-09-05 03:21:55 字数 864 浏览 10 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2024-09-12 03:21:55

当我将最后一行更改为

comboResult.SelectedValue = m_defaultResult; 

ComboTOResult 时对我有用,也许是一个不同的框?

Works for me when I change the last line to

comboResult.SelectedValue = m_defaultResult; 

ComboTOResult maybe a different box?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文