示例#1
0
 public stkDlg(StkPrce obj)  //constructor that takes a stock price object and loads the fields with it
 {
     InitializeComponent();
     stkabrvTxtBox.Text   = obj.stkabrv;
     cmpnynmeTextBox.Text = obj.cmpnyname;
     opnprceTxtBox.Text   = obj.opnprce.ToString("#.00");
     hghprceTxtBox.Text   = obj.hghprce.ToString("#.00");
     lwprceTxtBox.Text    = obj.lwprce.ToString("#.00");
     clseprceTxtBox.Text  = obj.clsprce.ToString("#.00");
     dtePicker.Value      = obj.dte;
 }
        private void opnMnuItm_Click(object sender, EventArgs e)
        {
            opndfle = opnFleDlg.FileName;

            if (svedta) // check if the current data in the list box needs to be saved
            {
                if (MessageBox.Show(this, "Do you want to save the current data before opening a new file?", "Open", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    sveMnuItm_Click(sender, e); // if yes is clicked and the users needs to save the data, call the save method
                }
            }

            if (opnFleDlg.ShowDialog() == DialogResult.Cancel)
            {
                return;           //if user selects cancel, return, otherwise execute the folllowing code
            }
            stkBox.Items.Clear(); // clear the data from the listbox

            try
            {
                System.IO.StreamReader stkrdr = new System.IO.StreamReader(opndfle); //read the file
                String[] stkfle;                                                     //create array
                Char[]   dl     = { '\t' };                                          //delimit file by tab
                String   stklne = stkrdr.ReadLine();                                 //read the first line of  the file


                while (stklne != null)         // keep reading until the line returns null(EOF)
                {
                    stkfle = stklne.Split(dl); // seperate the delimited text
                    StkPrce stock = new StkPrce();
                    stock.cmpnyname = stkfle[0];
                    stock.stkabrv   = stkfle[1];
                    stock.opnprce   = Double.Parse(stkfle[2]);

                    int year  = Int32.Parse(stkfle[7]);
                    int month = Int32.Parse(stkfle[8]);
                    int day   = Int32.Parse(stkfle[9]);

                    stock.date = new DateTime(year, month, day);

                    stkBox.Items.Add(stock); // add delimeted text to the list box
                    stklne = stkrdr.ReadLine();
                }



                stkrdr.Close();   // close the file
                stkrdr.Dispose(); // make sure its fully released
            }
            catch (Exception ex)
            {
            }
        }
示例#3
0
        public StkPrce stckprceobj() //method that creates a stock price object then returns it to its  caller
        {
            StkPrce obj = new StkPrce();

            obj.stkabrv   = stkabrvTxtBox.Text;
            obj.cmpnyname = cmpnynmeTextBox.Text;
            obj.opnprce   = Double.Parse(opnprceTxtBox.Text);
            obj.hghprce   = Double.Parse(hghprceTxtBox.Text);
            obj.lwprce    = Double.Parse(lwprceTxtBox.Text);
            obj.clsprce   = Double.Parse(clseprceTxtBox.Text);
            obj.dte       = dtePicker.Value;

            return(obj);
        }
        private void sveasMnuItm_Click(object sender, EventArgs e)
        {
            if (sveFleDlg.ShowDialog() == DialogResult.Cancel) //

            {
                return;                   // return if cancel  is pressed
            }
            opndfle = sveFleDlg.FileName; // save the name of the file and store it



            try
            {
                System.IO.StreamWriter stkwrtr = new System.IO.StreamWriter(opndfle);// write to the file


                for (int i = 0; i < stkBox.Items.Count; i++) //set i to 0, if i is less than the number of items in the list box, increment by 1
                {
                    StkPrce stk = (StkPrce)stkBox.Items[i];  //get the 1st stock price object


                    stkwrtr.Write(stk.cmpnyname);
                    stkwrtr.Write("\t");
                    stkwrtr.Write(stk.stkabrv);
                    stkwrtr.Write("\t");
                    stkwrtr.Write(stk.opnprce);
                    stkwrtr.Write("\t");
                    stkwrtr.Write(stk.hghprce);
                    stkwrtr.Write("\t");
                    stkwrtr.Write(stk.lwprce);
                    stkwrtr.Write("\t");
                    stkwrtr.Write(stk.clsprce);
                    stkwrtr.Write("\t");
                    stkwrtr.Write(stk.dte);
                }

                stkwrtr.Close();   // close the writer
                stkwrtr.Dispose(); // make sure the reader is fully released
            }
            catch (Exception)
            {
                MessageBox.Show(this, "Cant write to file!", "Error");
                return;
            }

            Text = "Stock Market Data"; // reset the text

            svedta = false;             // reset save data
        }
        private void pltBtn_Click(object sender, EventArgs e)
        {
            if (stkBox.Items.Count == 0)// if there are no items in the list box
            {
                MessageBox.Show("There are no stocks in the list to plot", "Error");
                return;
            }


            Graphics gfx = stkdtaPanl.CreateGraphics(); //create graphics object

            try
            {
                int wth  = stkdtaPanl.Width;// set width based on number of stocks in the list
                int hght = stkdtaPanl.Height;

                double mxvlue = Convert.ToDouble(mxvlu());   // use a double to force floating point arithemetic
                double yscle  = hght / mxvlue;               //set the y scale to height divided by max value

                int brspce = wth / stkBox.Items.Count;       //set the space between bars as width divided by number of items in the list
                int brwth  = brspce / 2;                     // set bar width to bar space divided by 2

                int xcrd = brwth;                            //set x coordinate to bar width
                int ycrd = 0;                                // set y coordinate to 0

                for (int i = 0; i < stkBox.Items.Count; i++) //set i to 0, if i is less than the number of items in the list box, increment by 1
                {
                    StkPrce stk   = (StkPrce)(stkBox.Items[i]);
                    Double  yprse = (Double)(stk.clsprce);
                    ycrd = (int)(yprse * yscle);
                    String clsePrceStr = yprse.ToString("0.00");


                    gfx.FillRectangle(sb, xcrd, hght + 30 - ycrd, brwth, ycrd);                             // draw solid rectangle with brush at specified coordinates
                    gfx.DrawString("$" + clsePrceStr, SystemFonts.DefaultFont, sb, xcrd, hght + 10 - ycrd); // draw text above bars using default font
                    ;
                    xcrd += brspce;                                                                         // move x over to next bar
                }

                gfx.Dispose(); // fully dispose of gfx
                gfx = null;    //set graphics to null
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to plot stock(s)", "Error");
            }
        }
        private void addBtn_Click(object sender, EventArgs e)
        {
            try
            {
                stkDlg newstkDlg;
                newstkDlg = new stkDlg();
                if (newstkDlg.ShowDialog() == DialogResult.OK)// if user pressed ok, add stock object to list
                {
                    StkPrce newStk = newstkDlg.stckprceobj();
                    stkBox.Items.Add(newStk);
                }
            }

            catch
            {
                MessageBox.Show("Cannot leave fields empty!", "Error");
            }
        }
        private void addBtn_Click(object sender, EventArgs e)
        {
            try
            {
                stkDlg newstkDlg;
                newstkDlg = new stkDlg();
                if (newstkDlg.ShowDialog() == DialogResult.OK)// if user pressed ok, add stock object to list
                {
                    StkPrce newStk = newstkDlg.stckprceobj();
                    stkBox.Items.Add(newStk); //add new stock to the list
                }
            }

            catch (Exception)
            {
                MessageBox.Show("Bad data, cannot add to the list!", "Error");
            }
        }
        private void updteBtn_Click(object sender, EventArgs e)
        {
            int ndx = stkBox.SelectedIndex;

            if (ndx < 0) // if no stock is selected in the list box

            {
                MessageBox.Show("You must select a stock  from the list first!");
                return;
            }


            try{
                StkPrce old = (StkPrce)stkBox.SelectedItem;


                stkDlg newstkDlg;
                newstkDlg = new stkDlg(old);


                if (newstkDlg.ShowDialog() == DialogResult.OK)
                {
                    StkPrce newStk = newstkDlg.stckprceobj();


                    stkBox.Items.RemoveAt(ndx);
                    stkBox.Items.Insert(ndx, newStk);
                }
            }

            catch (InvalidCastException)
            {
                MessageBox.Show("Unable to update", "Error");
                return;
            }
        }
        private void updteBtn_Click(object sender, EventArgs e)
        {
            int ndx = stkBox.SelectedIndex;

            if (ndx < 0) // if no stock is selected in the list box
            {
                MessageBox.Show("You must select a stock  from the list first!", "Attention");
                return;
            }


            try
            {
                StkPrce old = (StkPrce)stkBox.SelectedItem; //get selected item and covert it to StkPrce and store it


                stkDlg newstkDlg;
                newstkDlg = new stkDlg(old);// construct a StkDlg that takes the selected item as parameters


                if (newstkDlg.ShowDialog() == DialogResult.OK)
                {
                    StkPrce newStk = newstkDlg.stckprceobj();


                    stkBox.Items.RemoveAt(ndx);       // remove the selected stock
                    stkBox.Items.Insert(ndx, newStk); // insert updated stock information at same location in the list
                }
            }

            catch (InvalidCastException)
            {
                MessageBox.Show("Unable to update", "Error");
                return;
            }
        }
        private void opnMnuItm_Click(object sender, EventArgs e)
        {
            if (svedta) // check if the current data in the list box needs to be saved
            {
                if (MessageBox.Show(this, "Do you want to save the current data before opening a new file?", "Open", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    sveMnuItm_Click(sender, e); // if yes is clicked and the users needs to save the data, call the save method
                }
            }

            if (opnFleDlg.ShowDialog() == DialogResult.Cancel)
            {
                return;//if user selects cancel, return, otherwise execute the folllowing code
            }

            stkBox.Items.Clear();

            opndfle = opnFleDlg.FileName; //get and save the file name

            try
            {
                System.IO.StreamReader stkrdr = new System.IO.StreamReader(opndfle); //read the file
                String[] stkfle;                                                     //create array
                Char[]   dl     = { '\t' };                                          //delimit file by tab
                String   stklne = stkrdr.ReadLine();                                 //read the first line of  the file


                while (stklne != null)             // keep reading until the line returns null(EOF)
                {
                    stkfle = stklne.Split(dl);     // seperate the delimited text

                    StkPrce stock = new StkPrce(); //create object
                    stock.cmpnyname = stkfle[0];
                    stock.stkabrv   = stkfle[1];
                    stock.opnprce   = Double.Parse(stkfle[2]);
                    stock.hghprce   = Double.Parse(stkfle[3]);

                    stock.lwprce  = Double.Parse(stkfle[4]);
                    stock.clsprce = Double.Parse(stkfle[5]);



                    String            stringdate = stkfle[6];
                    DateTimeConverter dat        = new DateTimeConverter();

                    //      stock.dte = new DateTime(year, month, day);
                    stock.dte = (DateTime)dat.ConvertFromString(stringdate);


                    stkBox.Items.Add(stock); // add delimited text to the list box
                    stklne = stkrdr.ReadLine();
                }

                stkrdr.Close();             // close the file
                stkrdr.Dispose();           // make sure the reader is fully released
                Text = "Stock Market Data"; // reset the text
            }

            catch (Exception)
            {
                MessageBox.Show(this, "Unable to read file", "Error");
                return;
            }
        }