开发者

How to use a separate Enum class in my form

I have a function like this

        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.Ite开发者_如何学编程ms.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();
        } 

But what I need to do if I want this function to be defined in a separate enum class and then being called.


It is not very clear what you actually want to obtain. If you want your hardcoded strings to be defined in an enum you could define it like this:

enum Tables
{
    PrimaryKeyTables,
    NonPrimaryKeyTables,
    ForeignKeyTables,
    NonForeignKeyTables,
    UPPERCASEDTables,
    lowercasedtables,
}

Be aware that your string.IsNullOrEmpty("cmbOperations.SelectedValue"); always will return false since you are testing the specified string. You might want to test this instead:

cmbOperations.SelectedItem != null

To assign your Tables enum from your selection you can do like this:

Tables cbSelectedValue = (Tables)Enum.Parse(typeof(Tables), cmbOperations.SelectedItem.ToString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜