2010-11-30 34 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());