2014-10-22 100 views
0

嗨我想創建一個動態通用輸入框功能,它允許用戶從輸入列表中選擇一個輸入,然後返回它。對於我想要使用字典的項目列表。C#通用字典功能

的聲明,我大致如下的思路思考:

public static DialogResult InputBox<KeyType, ValueType>(string aTitle, string aPromptText, Dictionary<KeyType, ValueType> aDictionary, ref KeyValuePair<KeyType, ValueType> aReturnPair) 

我已經有兩個類似的功能,一個日期和一個字符串。

但是我想知道的是字典有可能嗎? 如何處理鑄件?我是否必須傳遞鍵和值的類型,還是可以在運行時檢索它?

也是它可以指定「功能」來檢索的名稱,用於顯示例如我有以下結構:

public struct Person 
{ 
    public int Age; 
    public string Name; 
    public string Surname; 
} 

我怎麼能告訴函數使用Person.Surname用於顯示目的?

用法示例:

Dictionary<int, Person> Persons = new Dictionary<int,Person>(); 
KeyValuePair<int, Person> lPair= new KeyValuePair<int,Person> 
DialogResult dialogResult = DialogResult.Cancel 
While (dialogResult == DialogResult.Cancel) 
{ 
    dialogResult = CustomForm.InputBox<int, Person>("title", "prompt", Persons, ref lPair, Person.Surname); 
} 

字符串輸入功能

public static DialogResult InputBox(string aTitle, string aPromptText, ref string aValue, Boolean aPassword = false) 
    { 
     Form lForm = new Form(); 
     Label lLabel = new Label(); 
     TextBox lTextBox = new TextBox(); 
     Button lButtonOk = new Button(); 
     Button lButtonCancel = new Button(); 

     lForm.Text = aTitle; 
     lLabel.Text = aPromptText; 
     lTextBox.Text = aValue; 

     if (aPassword) 
     { 
      lTextBox.PasswordChar = '*'; 
     } 
     lButtonOk.Text = "OK"; 
     lButtonCancel.Text = "Cancel"; 
     lButtonOk.DialogResult = DialogResult.OK; 
     lButtonCancel.DialogResult = DialogResult.Cancel; 

     lLabel.SetBounds(9, 20, 372, 13); 
     lTextBox.SetBounds(12, 36, 372, 20); 
     lButtonOk.SetBounds(228, 72, 75, 23); 
     lButtonCancel.SetBounds(309, 72, 75, 23); 

     lLabel.AutoSize = true; 
     lTextBox.Anchor = lTextBox.Anchor | AnchorStyles.Right; 
     lButtonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 
     lButtonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 

     lForm.ClientSize = new Size(396, 107); 
     lForm.Controls.AddRange(new Control[] { lLabel, lTextBox, lButtonOk, lButtonCancel }); 
     lForm.ClientSize = new Size(Math.Max(300, lLabel.Right + 10), lForm.ClientSize.Height); 
     lForm.FormBorderStyle = FormBorderStyle.FixedDialog; 
     lForm.StartPosition = FormStartPosition.CenterScreen; 
     lForm.MinimizeBox = false; 
     lForm.MaximizeBox = false; 
     lForm.AcceptButton = lButtonOk; 
     lForm.CancelButton = lButtonCancel; 

     DialogResult dialogResult = lForm.ShowDialog(); 
     aValue = lTextBox.Text; 
     return dialogResult; 
    } 
+0

你在哪裏需要使用字典? – 2014-10-22 08:32:22

+0

這個想法是爲了字典而重寫字符串函數。我希望能夠給函數一個字典,然後它會提示用戶輸入一個部分。這是我爲我們的所有應用程序創建的庫。這就是爲什麼我想要的關鍵和價值動態 – blackwolfsa 2014-10-22 09:02:25

+0

請檢查我的編輯 – 2014-10-22 09:09:26

回答

0

我設法創造一個在這裏被完成的功能

public static DialogResult InputBox<KeyType, ValueType>(string aTitle, string aPromptText, Dictionary<KeyType, ValueType> aDictionary, ref KeyValuePair<KeyType, ValueType> aReturnPair, Func<ValueType, string> aDelegate) 
    { 
     Form lForm = new Form(); 
     Label lLabel = new Label(); 
     ComboBox lComboBox = new ComboBox(); 
     Button lButtonOk = new Button(); 
     Button lButtonCancel = new Button(); 

     lForm.Text = aTitle; 
     lLabel.Text = aPromptText; 
     foreach (KeyValuePair<KeyType, ValueType> lPair in aDictionary) 
     { 
      lComboBox.Items.Add(aDelegate(lPair.Value)); 
     } 


     lButtonOk.Text = "OK"; 
     lButtonCancel.Text = "Cancel"; 
     lButtonOk.DialogResult = DialogResult.OK; 
     lButtonCancel.DialogResult = DialogResult.Cancel; 

     lLabel.SetBounds(9, 20, 372, 13); 
     lComboBox.SetBounds(12, 36, 372, 20); 
     lButtonOk.SetBounds(228, 72, 75, 23); 
     lButtonCancel.SetBounds(309, 72, 75, 23); 

     lLabel.AutoSize = true; 
     lComboBox.Anchor = lComboBox.Anchor | AnchorStyles.Right; 
     lButtonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 
     lButtonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 

     lForm.ClientSize = new Size(396, 107); 
     lForm.Controls.AddRange(new Control[] { lLabel, lComboBox, lButtonOk, lButtonCancel }); 
     lForm.ClientSize = new Size(Math.Max(300, lLabel.Right + 10), lForm.ClientSize.Height); 
     lForm.FormBorderStyle = FormBorderStyle.FixedDialog; 
     lForm.StartPosition = FormStartPosition.CenterScreen; 
     lForm.MinimizeBox = false; 
     lForm.MaximizeBox = false; 
     lForm.AcceptButton = lButtonOk; 
     lForm.CancelButton = lButtonCancel; 

     DialogResult dialogResult = lForm.ShowDialog(); 
     if ((dialogResult == DialogResult.OK) && (lComboBox.SelectedIndex > -1) && (lComboBox.SelectedIndex < aDictionary.Count)) 
     { 
      foreach (KeyValuePair<KeyType, ValueType> lPair in aDictionary) 
      { 
       if (string.Compare(aDelegate(lPair.Value),lComboBox.Items[lComboBox.SelectedIndex].ToString())== 0) 
       { 
        aReturnPair = lPair; 
       } 
      } 

     }    
     return dialogResult; 
    } 

你把它叫做如下:

DialogResult dialogResult = CustomForm.InputBox<int, Person>("title", "prompt", Persons, ref lPair, (Person) => Person.Name); 
+0

是的,你需要使用Func解決它,如果你發現我的答案正確,請將它標記爲答案:) – 2014-10-22 12:31:07

2

您可以使用委託函數,拿一本字典作爲參數和一個字符串返回給用戶,這樣的事情:

Func<Dictionary<Object, Object>,string> userOutput 

通過將該參數添加到函數中,您可以編寫鑄造的邏輯你怎麼想提示輸出給用戶,你這樣稱呼它:

txtTextBox.Text = userOutput(dictionary); 
+0

我沒有得到你的意思。我想和我的字符串輸入函數一樣。我已將它附加到主要帖子中。 – blackwolfsa 2014-10-22 08:22:57

+0

嗯我現在看到你的意思謝謝。但是,如果我想按如下所示調用它,我將如何進行聲明:Person.Surname這是在原始帖子中的示例。 – blackwolfsa 2014-10-22 09:32:34

+0

我已經添加了一個示例,我想如何使用它 – blackwolfsa 2014-10-22 09:47:46