2010-11-30 32 views
0

使用一个单独的枚举类我有这样如何在我的形式

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

但我需要做的,如果我想在一个单独的枚举类来定义这个函数,然后调用什么功能。

+0

你想要做什么??? – 2010-11-30 07:40:07

回答

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