0

I'm developing a sine cosine zedgraph control where user are allowed to select the .txt file contain data from the explorer then generate the graph. Here's the code:

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;

    }

And now i have like a button click that will allow user to open dialog file and select txt file contain and save it to my array.

private void load_data_from_PC_Click(object sender, EventArgs e)
     {
         //save data to array
     }

Need help please.TQ

Ren
  • 765
  • 4
  • 15
  • 42
  • 3
    Duplicate of [Reading a text file using OpenFileDialog in windows forms](http://stackoverflow.com/questions/16136383/reading-a-text-file-using-openfiledialog-in-windows-forms), which was also suggested in your [previous question](http://stackoverflow.com/questions/21720616/load-txt-data-from-file-explorer-to-chart-in-c-sharp). – CodeCaster Feb 13 '14 at 10:07

2 Answers2

2

hope this helps you

you can use the code inside your event

// 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();
        }
Shiva Saurabh
  • 1,281
  • 2
  • 25
  • 47
  • working.But if my text file have like x,y array ex: 1,31 2,32 3,32 4,30 5,26 How can i save it to my funct to create the graph? – Ren Feb 14 '14 at 00:45
1

From MSDN. Removed 'All files' option.

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);
    }
}
Marcus
  • 8,230
  • 11
  • 61
  • 88