处理ComboBox值的最佳方法 [英] Best way to handle ComboBox Values
问题描述
嘿伙计们,
处理需要翻译成整数的ComboBox Text值的最佳方法是什么。
例如,如果我有一个包含15个左右项目的Combobox,标记为项目A,项目B,项目C等,并且每个项目都需要转换为整数,例如如项目A = 1016,项目B = 2422,项目C = 8471.
这是一个很长的Else IF列表的最佳方法吗? br />
Hey guys,
Whats the best way to handle ComboBox Text values that need to translate into an integer.
For example if I have a Combobox with 15 or so items, labeled "Item A", "Item B", "Item C", etc. and each one of those need to be translated to an integer, such as Item A = 1016, Item B = 2422, Item C = 8471.
Is the best way to do that a big long list of "Else IF"?
if (comboBox1.SelectedIndex = 0)
{
itemNum = 1016;
}
else if (comboBox1.SelectedIndex = 1)
{
itemNum = 2422;
}
else if (comboBox1.SelectedIndex = 2)
{
itemNum = 8471;
}
它有效,但如果我有很多物品,它似乎效率不高。
It works, but it just doesn't seem to be very efficient if I have a lot of items.
推荐答案
与SAK和Idenizeni提供的精细代码示例相比,这里是WinForms中的懒惰程序员的方式:
For contrast to the fine code examples provided by SAK and Idenizeni, here's the "lazy programmer's way" in WinForms:
private int ItemNumber;
private List<string> cmbItemList = new List<string> { "A", "B", "C", "D" };
private List<int> cmbItemCode = new List<int> { 1016, 2422, 8471, 1234 };
private void FormTemplate_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(cmbItemList.Cast<object>().ToArray());
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ItemNumber = cmbItemCode[comboBox1.SelectedIndex];
// for testing only
Console.WriteLine(ItemNumber);
}
我建议这样做:不要在组合框项目中存储字符串。谁告诉你,你需要有弦乐?列表元素的运行时类型是System.Object
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox。 objectcollection.add.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ system.windows.forms.combobox.objectcollection.a dd.aspx [ ^ ]。
使用您可以设计的某些类或结构的任何自定义对象来封装所需的每个项目的所有细节按您的功能。唯一的问题是:每个项目的UI中会显示什么?答案很简单:无论函数ToString()
返回。因此,您必须覆盖System.Object.ToString()
。例如:
Here is what I advise to do: don't store strings in the combo box items. Who told you that you need to have strings? The runtime type of the list element isSystem.Object
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.add.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.add.aspx[^].
Use any custom object, of some class or structure you can design to encapsulate all the detail on each item required by your functionality. The only problem is: what will be displayed in the UI for each item? The answer is simple: whatever the functionToString()
returns. So, you have to overrideSystem.Object.ToString()
. For example:
internal class MyComboBoxItem {
internal MyComboBoxItem(string title, int itemNumber /* , some more property initial values... */) {
this.Title = title;
this.ItemNumber = itemNumber;
// more initializations
} //MyComboBoxItem
internal int ItemNumber { get; set; }
internal string Title { get; private set; }
public override string ToString() {
return Title;
} //System.Object.ToString
//...
} //class MyComboBoxItem
现在,你可以添加任意数量的类对象如上所示。在运行期间,无论实际的项目数是多少,您都可以将所有switch语句(不受支持,完全不受支持)减少到几行:
Now, you can add any number of the objects of the class shown above. During runtime, you will be able to reduce all your switch statement (unsupportable, totally unsupportable) to just a few lines, regardless of the actual number of items:
myComboBox.Items.Add(new MyComboBoxItem("Some item title", someIntegerValueForItemNumber /* , some more property initial values... */));
//...
if (myComboBox.SelectedItem != null) {
MyComboBoxItem item = (MyComboBoxItem)myComboBox.SelectedItem;
// now, all non-private members of items are accessible
int itemNum = item.ItemNumber;
// and so on...
} //if
Are你明白了吗?
// create a class to use for the item pairs
class ComboBoxItem
{
public ComboBoxItem(int itemID, string itemText)
{
ItemID = itemID;
ItemText = itemText;
}
public int ItemID { get; set; }
public string ItemText { get; set; }
}
// in our WPF window populate a list of combobox items, set combobox properties and reference list of item
public MainWindow()
{
InitializeComponent();
// set combobox properties
comboBox1.SelectedValuePath = "ItemID";
comboBox1.DisplayMemberPath = "ItemText";
comboBox1.ItemsSource = CreateItemPairs();
comboBox1.SelectedIndex = -1;
}
// handle selection changed event
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int itemNum = 0;
if (comboBox1.SelectedIndex > -1)
{
Int32.TryParse(comboBox1.SelectedValue.ToString(), out itemNum);
MessageBox.Show(itemNum.ToString());
}
}
// populate items list
private List<ComboBoxItem> CreateItemPairs()
{
List<ComboBoxItem> comboItems = new List<ComboBoxItem>();
comboItems.Add(new ComboBoxItem(1111, "Item A"));
comboItems.Add(new ComboBoxItem(2222, "Item B"));
comboItems.Add(new ComboBoxItem(3333, "Item C"));
return comboItems;
}
这篇关于处理ComboBox值的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!