从.dat文件中填充动态文本框

时间:2015-05-21 19:44:42

标签: c# file-management

我为一个学期作业制作了一个n变量线性方程求解器。它运行正常(我已经从网站上获得了一些帮助)只剩下一个功能,即加载来自' .dat'文件并以正确的顺序在动态文本框中复制它们。

锯齿状数组保存变量,例如,如果我有' 2x + y = 5',' 3x-y = 4'在我的数组中,它应该改为{2,1,5}和{3,-1,4}," ismeretlenek"表示变量的数量。

如何填写' .dat'中的这些文本框?文件?

ismeretlen = (int)numericUpDown1.Value;

TB = new TextBox[ismeretlen][];

for(int i = 0; i < ismeretlen; i++)
    TB[i] = new TextBox[ismeretlen + 1];


int height = 20;
int width = 40;
int curX = 10;
int curY = 10;

for(int i = 0; i < ismeretlen; i++)
{
    for(int j = 0; j < ismeretlen + 1; j++)
    {
        TextBox txtbox = new TextBox();
        txtbox = new TextBox();
        txtbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        txtbox.Left = curX;
        txtbox.Top = curY;
        txtbox.Width = width;
        txtbox.Height = height;
        txtbox.Font = new Font(txtbox.Font.FontFamily, 14);
        txtbox.BackColor = Color.Azure;
        txtbox.TextAlign = HorizontalAlignment.Center;
        txtbox.Text = "0";

        TB[i][j] = txtbox;
        this.panel1.Controls.Add(TB[i][j]);

        curX += width + 15;
    }

    curX = 10;
    curY = curY + height + 20;
    }

}

private void Save_Click(object sender, EventArgs e)
{
    for (int a = 0; a < ismeretlen; a++)
        segéd[a] = new double[ismeretlen + 1];

    for (int a = 0; a < ismeretlen; a++)
    {
        for (int b = 0; b < ismeretlen + 1; b++)
        {
            segéd[a][b] = double.Parse(TB[a][b].Text);

        }
    }

    string file = ismeretlen+"ismeretlen.dat";
    FileStream fs = new FileStream(file, FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);
    for (int r = 0; r < ismeretlen; r++)
        for (int t = 0; t < ismeretlen + 1; t++)
           bw.Write(segéd[r][t]);

    bw.Close();
    fs.Close();

}

private void Load_Click(object sender, EventArgs e)
{

}

}

1 个答案:

答案 0 :(得分:-1)

        /* example:
         * 2x+y=5
         * 3x-y=4
         */

        var outofFile = new List<string> { "2x+y=5", "3x-y=4" };

        var terms = outofFile.Select(x => x.Split(new[] { '+', '-', '=' }));

        // gets all the numbers to a List<List<double>
        var numbers = terms.Select(equalation => equalation.Aggregate(new List<double>(), (List<double> listPerLine, string term) => // term is something like '2x' or '4'
        {
            double number;
            if (!double.TryParse(term, out number)) // if no variable inside just a number you can convert it and return it
            {
                // cut the variable away eg. 2x -> 2
                var numberstring = term.Substring(0, term.Length - 1);
                if (numberstring.Length == 0)
                    number = 1; // eg. if there is only y -> '' -> 1
                else
                    number = double.Parse(numberstring); // eg 2x -> 2 
            }
            listPerLine.Add(number);
            return listPerLine;
        })).ToList();

        /* now you can access the values like this
         *  (indizes zero based)
         * numbers[equalation indizes][inner coeffizient indizes]
         * 
         * check out the values
         * 
         * that way you can assign them to the textboxes.
         */

        var _0_0 = numbers[0][0];
        var _0_1 = numbers[0][1];
        var _0_2 = numbers[0][2];
        var _1_0 = numbers[1][0];
        var _1_1 = numbers[1][1];
        var _1_2 = numbers[1][2];

这将让您了解如何执行此操作。只需调试并理解它:)然后您可以继续将其分配给文本框..

当然这可能会有所改善,但无论如何我现在要睡觉了