使用文本框中的数字填充字典,并将文本框作为键

时间:2016-07-21 13:46:12

标签: c# winforms dictionary

我正在编写一个代码,我希望将多个TextBox控件中的数字转换为集合。对其进行排序,然后更改包含前3个最高值的文本框的背景颜色。这是字典

Dictionary<System.Windows.Forms.TextBox, string> all_cycles = new Dictionary<System.Windows.Forms.TextBox, string>(10);
for (var i = 1; i < 5; i++)
{
    all_cycles.Add(((System.Windows.Forms.TextBox)this.Controls.Find("txtendc" + Convert.ToString(i), true)[0]),this.Text);
}

我知道使用“this.text”我不会得到文本框值,这就是我要问的原因。 我也尝试创建一个只包含文本框值的数组,然后我用它来填充字典。但它总是丢弃一个索引超出了数组的范围。或索引超出范围异常。这会超出范围异常:

List<System.Windows.Forms.TextBox> txtendc = new List<System.Windows.Forms.TextBox>(10);
for (var i = 1; i < 5; i++)
{
    txtendc.Add((System.Windows.Forms.TextBox)this.Controls.Find("txtendc" + Convert.ToString(i), true)[0]);
}
int[] endc_content = new int[10];
for (var i = 1;i < 5; i++)
{
    endc_content[i]= int.Parse(txtendc[i].Text);
}

我对着色没有任何问题,只是填写字典。如果您有比字典集更好的解决方案,请告诉我。 感谢

编辑:如果重要的话,整个代码都在计时器刻度事件中

3 个答案:

答案 0 :(得分:2)

在实例化类时,我会在代码后面创建一个包含所有TextBox的列表。然后你可以使用Linq获得前3名。请记住,您需要确认所有文本框实际上都包含数字 - 确保您的程序在其中一个文本中输入文本时不会崩溃。如果表单中的所有TextBox都将保存这些数字,您可以执行以下操作来验证和排序一个方法(未测试):

private List<TextBox> myTextBoxes { get; set; }

public Form1()
{
    InitializeComponent();
    foreach (Control c in this.Controls)
    {
        if (c.GetType() == typeof(TextBox))
            myTextBoxes.Add((TextBox)c);
    }
}

private IEnumerable<TextBox> getTop3()
{
    return myTextBoxes.Where(tb => tb.Text.AsEnumerable().All(char.IsDigit)).Select(tb => tb).OrderByDescending(tb => Double.Parse(tb.Text)).Take(3);
}

Linq查询的第一步将文本转换为可枚举的char,并确保所有字符都包含数字(而不是字母)。第二个选择这些文本框。第三个将它们解析为双打并比较它们,最高数字首先。最后一个采用列表中的前三个(包含最多3个数字的文本框)

编辑:根据您的评论,代码可以简化为:

public Form1()
{
    InitializeComponent();

}

private IEnumerable<TextBox> getTop3(string textboxPrefix)
{
    List<TextBox> textBoxesToSort = new List<TextBox>();
    foreach (Control c in this.Controls)
        if (c.GetType() == typeof(TextBox) && c.Name.StartsWith(textboxPrefix))
            textBoxesToSort.Add((TextBox)c);

    return textBoxesToSort.OrderByDescending(tb => Double.Parse(tb.Text)).Take(3);
}

答案 1 :(得分:1)

您可以根据值对这些TextBox控件进行排序,然后从列表中选择前3位:

var result = this.Controls.OfType<TextBox>().Select(x =>
                 {
                     try { return new { Key = x, Value = Convert.ToInt32(x.Text) }; }
                     catch { return null; }
                 })
                 .Where(x=>x!=null)
                 .OrderByDescending(x => x.Value)
                 .Take(3)
                 .ToDictionary(x => x.Key, x => x.Value);

要测试结果,请显示一个包含值的消息框:

MessageBox.Show(string.Join("\n",
    result.Select((x, i) => string.Format("{0}:{1}", i+1, x.Value))));

以上示例将应用于TextBox控件的任何列表,例如我使用了表单的所有TextBox控件。

答案 2 :(得分:0)

请试试这个,希望有所帮助:

List<System.Windows.Forms.TextBox> all_cycles = new List<System.Windows.Forms.TextBox>(10);
        int[] indexOfTextBox = new int[3];
        foreach(var cycle in all_cycles)
        {
            int value = Convert.ToInt16(cycle.Text);
            if (value > indexOfTextBox[0])
            {
                indexOfTextBox[0] = value;
            }
            else if (value > indexOfTextBox[1])
            {
                indexOfTextBox[1] = value;
            }
            else if (value > indexOfTextBox[2])
            {
                indexOfTextBox[2] = value;
            }
        }
        all_cycles[indexOfTextBox[0]].BackColor = ConsoleColor.Red;
        all_cycles[indexOfTextBox[1]].BackColor = ConsoleColor.Blue;
        all_cycles[indexOfTextBox[2]].BackColor = ConsoleColor.Green;