C# 当我单击加载数据时,它会提示错误。您在使用Visual studio吗?是的,VS 2013 express。@Ren:您试图从流中读取两次,请检查我的答案。哪一行引发该异常?不确定是哪一行,因为我可以执行此代码,唯一的问题是,当我单击“加载数据”时,它会
C# 当我单击加载数据时,它会提示错误。您在使用Visual studio吗?是的,VS 2013 express。@Ren:您试图从流中读取两次,请检查我的答案。哪一行引发该异常?不确定是哪一行,因为我可以执行此代码,唯一的问题是,当我单击“加载数据”时,它会,c#,zedgraph,C#,Zedgraph,当我单击加载数据时,它会提示错误。您在使用Visual studio吗?是的,VS 2013 express。@Ren:您试图从流中读取两次,请检查我的答案。哪一行引发该异常?不确定是哪一行,因为我可以执行此代码,唯一的问题是,当我单击“加载数据”时,它会提示错误。您使用的是Visual studio吗?是的,VS 2013 express。@Ren:您试图从流中读取两次,检查我的答案。@任:不客气:)很高兴能帮助你。@任:不客气:)很高兴能帮助你。@任:不客气:)很高兴能帮助你。@任:不客气:
当我单击加载数据时,它会提示错误。您在使用Visual studio吗?是的,VS 2013 express。@Ren:您试图从流中读取两次,请检查我的答案。哪一行引发该异常?不确定是哪一行,因为我可以执行此代码,唯一的问题是,当我单击“加载数据”时,它会提示错误。您使用的是Visual studio吗?是的,VS 2013 express。@Ren:您试图从流中读取两次,检查我的答案。@任:不客气:)很高兴能帮助你。@任:不客气:)很高兴能帮助你。@任:不客气:)很高兴能帮助你。@任:不客气:)很高兴能帮助你。
private void Load_data_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "C:\\DataArray";
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
string filename = openFileDialog1.FileName;
var lineCount = 0;
using (var reader = File.OpenText(@filename))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
for(var count = 0; count < lineCount; ++count)
{
var data = reader.ReadLine().Split(',');
GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
}
ShowGraphData(lineCount);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
public void ShowGraphData(long lTotalData)
{
double[] dx = new double[lTotalData];
double[] dy = new double[lTotalData];
for (long li = 0; li < lTotalData; li++)
{
dx[li] = GlobalDataClass.dDataArray[li, 0];
dy[li] = GlobalDataClass.dDataArray[li, 1];
}
zedGraphControlStickiness.GraphPane.CurveList.Clear();
GraphPane StickinessPane = zedGraphControlStickiness.GraphPane;
// PointPairList holds the data for plotting, X and Y arrays
PointPairList spl1 = new PointPairList(dx, dy);
// Add cruves to myPane object
LineItem ProductionCurve = StickinessPane.AddCurve("Insertion Force", spl1, Color.Blue, SymbolType.None);
ProductionCurve.Line.Width = 2.0F;
zedGraphControlStickiness.AxisChange();
zedGraphControlStickiness.Invalidate();
zedGraphControlStickiness.Refresh();
GlobalDataClass.iTotalReadingPoint = lTotalData;
}
while (reader.ReadLine() != null) //<-- reading 1st time
{
lineCount++;
} //stream becomes null here
for(var count = 0; count < lineCount; ++count)
{
var data = reader.ReadLine().Split(','); //<-- reading 2nd time
GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
}
string filename = openFileDialog1.FileName;
var lineCount = 0;
using (var reader in File.ReadLines(@filename))
{
var data=reader.Split(',');
lineCount ++;
GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
}
ShowGraphData(lineCount);
while (reader.ReadLine() != null)
{
lineCount++;
}
for(var count = 0; count < lineCount; ++count)
{
var data = reader.ReadLine().Split(',');
GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
}
string line;
while ((line = reader.ReadLine()) != null)
{
var data = line.Split(',');
GlobalDataClass.dDataArray[lineCount, 0] = double.Parse(data[0]);
GlobalDataClass.dDataArray[lineCount, 1] = double.Parse(data[1]);
lineCount++;
}
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
var count = 0;
using (var reader = File.OpenText(@openFileDialog1.FileName))
{
var str = reader.ReadLine();
while (str != null)
{
var data = str.Split(',');
GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
str = reader.ReadLine();
count++;
}
}
ShowGraphData(count - 1);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}