VB.Net ComboBox - 基于另一个组合框中的选择的枚举和限制值

发布于 2024-12-06 00:05:43 字数 1575 浏览 1 评论 0原文

我正在从事的项目广泛使用枚举来保存单位符号。像这样...

Public Enum AreaSym
    unknown = 0
    in2
    mm2
End Enum

当我意识到我的枚举名称不能为我的组合框上的下拉菜单起好名字时,我的问题就开始了。我从这个 C# CodeProject http://www.codeproject 找到并实现了一个 TypeConverter .com/KB/cs/LocalizingEnums.aspx?msg=3746572 ...它允许您在 .Resx 中分配枚举值友好名称文件,并且它重载 GetValues 函数,以便从 .Resx 文件返回 Enums 友好名称。这很有效,因为我可以将组合框数据源直接分配给枚举。

ComboBox1.DataSource = System.Enum.GetValues(GetType(AreaSym))

但是,我意识到,当以这种方式使用数据源时,我必须操作数据源以在组合框中创建更改。例如,如果我不想向用户显示未知,我需要另一种方式来加载我的组合框。这是我决定制作一个 List(Of KeyValuePair(AreaSym, String)) 的时候,

Public Function AreaSymbols() As List(Of KeyValuePair(Of AreaSym, String))
    AreaSymbols = New List(Of KeyValuePair(Of AreaSym, String))
    AreaSymbols.Add(New KeyValuePair(Of AreaSym, String)(AreaSym.in2, "in²"))
    AreaSymbols.Add(New KeyValuePair(Of AreaSym, String)(AreaSym.mm2, "mm²"))
    Return AreaSymbols
End Function

这很棒,因为现在我可以使用列表准确控制组合框中的内容。

ComboBox1.DataSource = UnitNameList.AreaSymbols
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "Key"

但是,我发现使用数据源阻止了我做两件事。

  1. 加载表单时,我希望组合框本身显示为空且没有文本,或者显示我在文本属性中输入的文本。当前使用数据源,它始终填充列表中的第一项。

  2. 我希望能够根据另一个列表中的项目选择轻松限制列表中的项目。典型的例子是两个组合框填充了完全相同的值 (1,2,3) & (1,2,3)。当您在 ComboBox 1 中选择 1 时,ComboBox 2 中只有 (2,3) 可供选择。

我意识到,如果我使用 Items 而不是数据源,我可能可以轻松做到这一点,我只是不确定什么最好的方法是因为我需要拥有枚举和字符串的键、值对......

The project I'm working on makes extensive use of Enum's to hold unit symbols. Like this...

Public Enum AreaSym
    unknown = 0
    in2
    mm2
End Enum

My Problems began when I realized that my Enum names didn't make good names for the drop downs on my ComboBoxes. I found and implemented a TypeConverter from this C# CodeProject http://www.codeproject.com/KB/cs/LocalizingEnums.aspx?msg=3746572 ... It allows you to assign the Enum Values friendly names in a .Resx file, and it overloads the GetValues function so that it returns the Enums friendly names from the .Resx file. This works well as I can then assign the combobox data source directly to the enum.

ComboBox1.DataSource = System.Enum.GetValues(GetType(AreaSym))

However, I realized that when using a Datasource in this manner, I have to manipulate the data source to create a change in the ComboBox. Such as, if I don't want to display unknown to the user, I'd need another way to load my ComboBoxes. This is when I decided to make a List(Of KeyValuePair(AreaSym, String))

Public Function AreaSymbols() As List(Of KeyValuePair(Of AreaSym, String))
    AreaSymbols = New List(Of KeyValuePair(Of AreaSym, String))
    AreaSymbols.Add(New KeyValuePair(Of AreaSym, String)(AreaSym.in2, "in²"))
    AreaSymbols.Add(New KeyValuePair(Of AreaSym, String)(AreaSym.mm2, "mm²"))
    Return AreaSymbols
End Function

This is great because now I can control exactly what goes into the combo box using the list.

ComboBox1.DataSource = UnitNameList.AreaSymbols
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "Key"

However, I've found that using a DataSource has stopped me from doing two things.

  1. When the form loads, I want the ComboBox itself to show as either empty with no text, or to show the Text I've input in the Text Property. Currently using the DataSource, it always is populated with the first item in the list.

  2. I'd like to be able to easily limit the items in the list, based on the selection of items in another list. Classic example would be two ComboBoxes filled with the exact same values (1,2,3) & (1,2,3). When you choose 1 in ComboBox one, only (2,3) are available for selection in ComboBox 2.

I realize that I could probably easily do that if I was using Items, instead of a data source, I'm just not sure what the best way to do that would be because of my need to have the key, value pair of enums and strings...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文