如何在我的形式
问题描述:
使用一个单独的枚举类我有这样如何在我的形式
public void SetOperationDropDown()
{
int ? cbSelectedValue = null;
if(cmbOperations.Items.Count == 0)
{
//ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-.
cmbOperations.SelectedItem = "-SELECT OPERATIONS-";
//This is for adding four operations with value in operation dropdown
cmbOperations.Items.Insert(0, "PrimaryKeyTables");
cmbOperations.Items.Insert(1, "NonPrimaryKeyTables");
cmbOperations.Items.Insert(2, "ForeignKeyTables");
cmbOperations.Items.Insert(3, "NonForeignKeyTables");
cmbOperations.Items.Insert(4, "UPPERCASEDTables");
cmbOperations.Items.Insert(5, "lowercasedtables");
}
else
{
if(!string.IsNullOrEmpty("cmbOperations.SelectedValue"))
cbSelectedValue = Convert.ToInt32(cmbOperations.SelectedValue);
}
//Load the combo box cmbOperations again
if(cbSelectedValue != null)
cmbOperations.SelectedValue = cbSelectedValue.ToString();
}
但我需要做的,如果我想在一个单独的枚举类来定义这个函数,然后调用什么功能。
答
这是不是很清楚你实际上想要获得什么。如果你想在一个enum
来定义您的硬编码字符串,你可以这样定义它:
enum Tables
{
PrimaryKeyTables,
NonPrimaryKeyTables,
ForeignKeyTables,
NonForeignKeyTables,
UPPERCASEDTables,
lowercasedtables,
}
要知道,你的string.IsNullOrEmpty("cmbOperations.SelectedValue");
总是会返回false
,因为你正在测试指定的字符串。你可能会想,而不是测试:
cmbOperations.SelectedItem != null
要指定您从您的选择Tables
枚举,你可以这样做:
Tables cbSelectedValue = (Tables)Enum.Parse(typeof(Tables), cmbOperations.SelectedItem.ToString());
你想要做什么??? – 2010-11-30 07:40:07