C#从文本框中将值从最小值排序到最大值

时间:2015-11-12 19:01:25

标签: c# sorting textbox fifo

当我按下FIFO按钮时,我想从文本框中取值,检测最小的值,然后从tit中的最小数字开始操作直到最大值,然后将所有内容存储在tf

这就是我的程序:

这是我的代码:

private void fifo_Click(object sender, EventArgs e)
{
    int c = 0;
    //Hace tf

    tfA.Text = (Convert.ToInt32(ta.Text) + Convert.ToInt32(tiA.Text)).ToString();
    c = Convert.ToInt32(tfA.Text);
    tfB.Text = (Convert.ToInt32(tb.Text) + c).ToString();
    c = Convert.ToInt32(tfB.Text);
    tfC.Text = (Convert.ToInt32(tc.Text) + c).ToString();
    c = Convert.ToInt32(tfC.Text);
    tfD.Text = (Convert.ToInt32(td.Text) + c).ToString();
    c = Convert.ToInt32(tfD.Text);
    tfE.Text = (Convert.ToInt32(te.Text) + c).ToString();
    c = Convert.ToInt32(tfE.Text);
    tfF.Text = (Convert.ToInt32(tf.Text) + c).ToString();

    // Hace T
    T1.Text = (Convert.ToInt32(tfA.Text) - Convert.ToInt32(tiA.Text)).ToString();
    T2.Text = (Convert.ToInt32(tfB.Text) - Convert.ToInt32(tiB.Text)).ToString();
    T3.Text = (Convert.ToInt32(tfC.Text) - Convert.ToInt32(tiC.Text)).ToString();
    T4.Text = (Convert.ToInt32(tfD.Text) - Convert.ToInt32(tiD.Text)).ToString();
    T5.Text = (Convert.ToInt32(tfE.Text) - Convert.ToInt32(tiE.Text)).ToString();
    T6.Text = (Convert.ToInt32(tfF.Text) - Convert.ToInt32(tiF.Text)).ToString();

    // Hace E
    E1.Text = (Convert.ToInt32(T1.Text) - Convert.ToInt32(ta.Text)).ToString();
    E2.Text = (Convert.ToInt32(T2.Text) - Convert.ToInt32(tb.Text)).ToString();
    E3.Text = (Convert.ToInt32(T3.Text) - Convert.ToInt32(tc.Text)).ToString();
    E4.Text = (Convert.ToInt32(T4.Text) - Convert.ToInt32(td.Text)).ToString();
    E5.Text = (Convert.ToInt32(T5.Text) - Convert.ToInt32(te.Text)).ToString();
    E6.Text = (Convert.ToInt32(T6.Text) - Convert.ToInt32(tf.Text)).ToString();

    //Hace I
    I1.Text = (Convert.ToDecimal(ta.Text) / Convert.ToDecimal(T1.Text)).ToString();
    I2.Text = (Convert.ToDecimal(tb.Text) / Convert.ToDecimal(T2.Text)).ToString();
    I3.Text = (Convert.ToDecimal(tc.Text) / Convert.ToDecimal(T3.Text)).ToString();
    I4.Text = (Convert.ToDecimal(td.Text) / Convert.ToDecimal(T4.Text)).ToString();
    I5.Text = (Convert.ToDecimal(te.Text) / Convert.ToDecimal(T5.Text)).ToString();
    I6.Text = (Convert.ToDecimal(tf.Text) / Convert.ToDecimal(T6.Text)).ToString();

    //X1 2 y 3

    X1.Text = ((Convert.ToDecimal(T1.Text) + Convert.ToDecimal(T2.Text) + Convert.ToDecimal(T3.Text) + Convert.ToDecimal(T4.Text) + Convert.ToDecimal(T5.Text) + Convert.ToDecimal(T6.Text)) / 6).ToString();
    X2.Text = ((Convert.ToDecimal(E1.Text) + Convert.ToDecimal(E2.Text) + Convert.ToDecimal(E3.Text) + Convert.ToDecimal(E4.Text) + Convert.ToDecimal(E5.Text) + Convert.ToDecimal(E6.Text)) / 6).ToString();
    X3.Text = ((Convert.ToDecimal(I1.Text) + Convert.ToDecimal(I2.Text) + Convert.ToDecimal(I3.Text) + Convert.ToDecimal(I4.Text) + Convert.ToDecimal(I5.Text) + Convert.ToDecimal(I6.Text)) / 6).ToString();
    comp1 = Convert.ToDecimal(X3.Text);
}

编辑:我设法做了我想做的事。我只是将值放在一个数组中并对它们进行排序,然后将textbox.Text等于每个。工作得很好。感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我的建议是:

            // list holding the ti value and the three controls: ti, t and tf
            var controls = new List<Tuple<int, Control, Control, Control>>();

            // add all the controls together with the sorting key, the ti value (just an example, but here you add the tiValue and the ti control, t control and tf control
            controls.Add(new Tuple<int, Control, Control, Control>(0, null, null, null));

            // sort everything
            controls.Sort((t1, t2) => t1.Item1.CompareTo(t2.Item1));

            // loop through the controls in the right order and perform the logic
            foreach (var t in controls)
            {
                var tiValue = t.Item1;
                var tiControl = t.Item2;
                var tControl = t.Item3;
                var tfControl = t.Item4;

                // Do the things to the controls and their values...
            }

答案 1 :(得分:0)

无法访问pastebin或imgur我在哪里,但我会给它一个机会。尽管如此,文本框是一个可怕的想法。

public static void SortAndSumTextboxes()
{
    Form form = new Form();

    var tiControls = form.Controls.OfType<TextBox>().Where(tb => Regex.IsMatch(tb.Name, "^ti.$"));
    var tControls = form.Controls.OfType<TextBox>().Where(tb => Regex.IsMatch(tb.Name, "^t.$"));

    var tiSorted = tiControls.Select(tb => int.Parse(tb.Text)).OrderBy(i => i);
    var tSorted = tControls.Select(tb => int.Parse(tb.Text)).OrderBy(i => i);

    int c = 1; // some constant
    var tfValues = tiSorted.Zip(tSorted, (a, b) => a + b + c).ToArray();
    var tfControls = form.Controls.OfType<TextBox>().Where(tb => tb.Name.StartsWith("tf")).OrderBy(tb => tb.Name).ToArray(); ;

    for (int i = 0; i < tfControls.Length; i++)
    {
        tfControls[i].Text = tfValues[i].ToString();
    }
}