从 .dat 文件填充动态文本框
Filling dynamic textboxes from .dat file
做了一个学期作业的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)
{
}
}
/* 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];
这将使您了解如何执行此操作。简单地调试它并理解它:) 然后你可以继续将它分配给文本框..
当然这可能会有所改善,但无论如何我现在要睡觉了
做了一个学期作业的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)
{
}
}
/* 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];
这将使您了解如何执行此操作。简单地调试它并理解它:) 然后你可以继续将它分配给文本框..
当然这可能会有所改善,但无论如何我现在要睡觉了