如何在我的表单中使用单独的Enum类

时间:2010-11-30 07:34:54

标签: c# visual-studio winforms

我有这样的功能

        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();
        } 

但是如果我希望在一个单独的枚举类中定义该函数然后被调用,我需要做什么。

1 个答案:

答案 0 :(得分:1)

目前还不是很清楚你真正想要获得什么。如果您希望在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());