C#Foreach从错误的顺序读取文本框

时间:2018-03-24 19:57:26

标签: c# foreach textbox

所以我试图将文本框中的坐标加载到双打列表中。我已经编辑了他们的TabIndex以便它们按顺序排列,但是当我尝试加载数字时,它正在读取一些奇怪的odrer中的文本框。 2-> 4-> 8-> 7-> 5-> 6-> 3-> 1任何人都可以帮助我吗? sur_x和sur_y是列表的名称。

    public Boolean Nacitanie_hodnot()
    {
        // premazanie listov a vlozenie prvej hodnoty
        sur_x.Clear();
        sur_y.Clear();
        sur_x.Add(0);
        sur_y.Add(0);

        int i = 1;
        // nacitanie hodnot
        /*Ax = double.Parse(suradnica_Ax.Text, CultureInfo.InvariantCulture.NumberFormat);
        Ay = double.Parse(suradnica_Ay.Text, CultureInfo.InvariantCulture.NumberFormat);
        Bx = double.Parse(suradnica_Bx.Text, CultureInfo.InvariantCulture.NumberFormat);
        By = double.Parse(suradnica_By.Text, CultureInfo.InvariantCulture.NumberFormat);

        Cx = double.Parse(suradnica_Cx.Text, CultureInfo.InvariantCulture.NumberFormat);
        Cy = double.Parse(suradnica_Cy.Text, CultureInfo.InvariantCulture.NumberFormat);
        Dx = double.Parse(suradnica_Dx.Text, CultureInfo.InvariantCulture.NumberFormat);
        Dy = double.Parse(suradnica_Dy.Text, CultureInfo.InvariantCulture.NumberFormat);*/
        foreach (Control control in suradnice_vrcholov.Controls)
        {
            if ((control is TextBox) && (control.Text == "Zadaj"))
            {
                return false;
            }

            else if ((control is TextBox) && ((i % 2) != 0))
            {
                sur_x.Add(double.Parse(control.Text, CultureInfo.InvariantCulture.NumberFormat));
                i++;
            }

            else if ((control is TextBox) && ((i % 2) == 0))
            {
                sur_y.Add(double.Parse(control.Text, CultureInfo.InvariantCulture.NumberFormat));
                i++;
            }
        }
        return true;
    }

1 个答案:

答案 0 :(得分:3)

这是你想要达到的目的:?

foreach (Control control in suradnice_vrcholov.Controls.OfType<TextBox>().OrderBy(c => c.TabIndex))