Monday, 7 May 2012

3. Trader - Get indices


Program will keep active indices in plain text file entered manually by text editor  named ActiveIndices.txt. This file be read in listIndices when checkBox activeOnly is selected. When activeOnly is not selected then NonActiveIndices.txt will be read in listIndices.
We need to add using System.IO; since we'll use file reading and writing.
Public workingPath string is used to define working path on our disk. Later we can easily change path as appropriate. Also, we need to enter  Internet symbols to our text files.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Stocker
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
        public string workingPath; // we need this variable as data path to disk
        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            Environment.Exit(0);        // double click on form to end application- just for quick exit when testing
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Stocker2012";  // put title on form bar
            this.Width = 1000;          // set form width
            this.Height = 500;          // set form height
            this.Top = 0;               // set position of form
            this.Left = 0;
            activeOnly.Checked = true;  // by default we would like to work only with active indicies
            workingPath=@"P:\Private\Analizator\"; // this is my working directory on disk P:
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
           
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);        // quit application
        }

        private void getIndicies_Click(object sender, EventArgs e)
        {
            string aPath;                       // working string to make path
            if (activeOnly.Checked == true)     // upon a checkbox state
                aPath = workingPath + "ActiveIndices.txt";  // if checked - this file
            else aPath = workingPath + "NonActiveIndices.txt"; // if not - other file

            listIndices.Items.Clear();  // clear listbox before filling
           
            using (FileStream fs = File.OpenRead(aPath))    // prepare filestream
            using (TextReader reader = new StreamReader(fs)) // use textreader
                while (reader.Peek() > -1)                      // read while end of file is reached
                    listIndices.Items.Add(reader.ReadLine());   // add line to listbox

            listIndices.Sorted = true;              // all is read and it is time to sort items

        }
       
    }
}




No comments:

Post a Comment