简体   繁体   English

C#通用字典功能

[英]C# Generic dictionary function

Hi I would like to create a dynamic generic input box function which allows the user to select an input from a list of inputs and then return this. 嗨,我想创建一个动态的通用输入框函数,该函数允许用户从输入列表中选择一个输入,然后将其返回。 For the list of Items I want to use a dictionary. 对于项目列表,我想使用字典。

For the declaration I was thinking along the lines of the following: 对于声明,我正在考虑以下方面:

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

I already have two similar functions, one for dates and one for strings. 我已经有两个类似的功能,一个用于日期,另一个用于字符串。

But I want to know is the dictionary one possible? 但是我想知道这本字典可能吗? How do I handle the casting? 如何处理选角? Do I have to pass the type of the key and value as well or can I retrieve this during run time? 我是否还必须传递键和值的类型,还是可以在运行时检索它?

Also is it possible to specify the "function" to use to retrieve the name to use for display For example I have the following struct: 也可以指定“函数”以用于检索要用于显示的名称。例如,我具有以下结构:

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

How can I tell the function to use Person.Surname for display purposes? 如何告诉函数使用Person.Surname进行显示?

Example usage: 用法示例:

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

String input function 字符串输入功能

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

You can use delegates function that take a dictionary as parameter and return a string to the user, something like this: 您可以使用将字典作为参数并向用户返回字符串的委托函数,如下所示:

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

By adding this parameter to the function you can write the logic of casting and how you want to prompt the output to the user, you call it like this: 通过将此参数添加到函数中,您可以编写强制转换的逻辑以及如何向用户提示输出,您可以这样调用它:

txtTextBox.Text = userOutput(dictionary);

I managed to create one Here is the completed function 我设法创建了一个这是完成的功能

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

You call it as follows: 您将其称为如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM