我正在开发一个正弦余弦zedgraph控件,允许用户选择.txt文件包含来自资源管理器的数据,然后生成图形。这是代码:
private void GraphSetup()
{
// Lets generate sine and cosine wave
int i;
double[] x = new double[2000];
double[] y = new double[2000];
for(i=0; i<2000; i++)
{
GlobalDataClass.dDataArray[i, 0] = 0.0;
GlobalDataClass.dDataArray[i, 1] = 0.0;
}
GlobalDataClass.dDataArray[0, 0] = 0;
zedGraphControlStickiness.GraphPane.CurveList.Clear();
GraphPane StickinessPane = zedGraphControlStickiness.GraphPane;
StickinessPane.CurveList.Clear();
// PointPairList holds the data for plotting, X and Y arrays
PointPairList spl1 = new PointPairList(x, y);
// Add cruves to myPane object
LineItem myCurve1 = StickinessPane.AddCurve("Insertion Force", spl1, Color.Blue, SymbolType.None);
myCurve1.Line.Width = 2.0F;
StickinessPane.XAxis.Title.Text = "Sample ";
StickinessPane.YAxis.Title.Text = "Force ";
StickinessPane.Title.Text = "Pressure Measurement";
// zedGraphControlStickiness.AxisChange();
zedGraphControlStickiness.Invalidate();
zedGraphControlStickiness.Refresh();
}
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;
}
private void GraphSetup()
{
// Lets generate sine and cosine wave
int i;
double[] x = new double[2000];
double[] y = new double[2000];
for(i=0; i<2000; i++)
{
GlobalDataClass.dDataArray[i, 0] = 0.0;
GlobalDataClass.dDataArray[i, 1] = 0.0;
}
GlobalDataClass.dDataArray[0, 0] = 0;
zedGraphControlStickiness.GraphPane.CurveList.Clear();
GraphPane StickinessPane = zedGraphControlStickiness.GraphPane;
StickinessPane.CurveList.Clear();
// PointPairList holds the data for plotting, X and Y arrays
PointPairList spl1 = new PointPairList(x, y);
// Add cruves to myPane object
LineItem myCurve1 = StickinessPane.AddCurve("Insertion Force", spl1, Color.Blue, SymbolType.None);
myCurve1.Line.Width = 2.0F;
StickinessPane.XAxis.Title.Text = "Sample ";
StickinessPane.YAxis.Title.Text = "Force ";
StickinessPane.Title.Text = "Pressure Measurement";
// zedGraphControlStickiness.AxisChange();
zedGraphControlStickiness.Invalidate();
zedGraphControlStickiness.Refresh();
}
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;
}
现在我有一个按钮点击,允许用户打开对话框文件并选择包含txt文件并将其保存到我的阵列。
private void load_data_from_PC_Click(object sender, EventArgs e)
{
//save data to array
}
需要帮助.TQ
答案 0 :(得分:2)
希望这可以帮助你
您可以使用活动中的代码
// Create an instance of the open file dialog box.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
// Open the selected file to read.
System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
{
// Read the first line from the file and write it the textbox.
tbResults.Text = reader.ReadLine();
}
fileStream.Close();
}
答案 1 :(得分:1)
来自MSDN。删除了“所有文件”选项。
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
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)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}