嗨我想創建一個動態通用輸入框功能,它允許用戶從輸入列表中選擇一個輸入,然後返回它。對於我想要使用字典的項目列表。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;
}
你在哪裏需要使用字典? – 2014-10-22 08:32:22
這個想法是爲了字典而重寫字符串函數。我希望能夠給函數一個字典,然後它會提示用戶輸入一個部分。這是我爲我們的所有應用程序創建的庫。這就是爲什麼我想要的關鍵和價值動態 – blackwolfsa 2014-10-22 09:02:25
請檢查我的編輯 – 2014-10-22 09:09:26