⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ratesofreturnform.cs

📁 中文名:Windows Forms 程序设计 英文名:Windows Forms Programming in c# 作者: Chris Sells 翻译: 荣耀 蒋贤哲 出版社:人民
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Windows.Forms;

namespace SDIRatesOfReturn {

  partial class RatesOfReturnForm : Form {

    BindingList<PeriodReturn> periodReturns;

    public RatesOfReturnForm() {
      InitializeComponent();

      periodReturns = new BindingList<PeriodReturn>();
      this.PeriodReturnBindingSource.DataSource = this.periodReturns;
    }

    // For opening document from command line arguments
    public bool OpenDocument(string filename) {
      return this.fileDocument.Open(filename);
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
      MessageBox.Show("RatesOfReturn by Chris Sells, 2003\r\nEnjoy.", "About Rates Of Return");
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
      // Let FileDocument component decide if this is OK
      this.Close();
    }

    private void fileDocument_NewDocument(object sender, EventArgs e) {

      // Reset list
      this.periodReturns.Clear();
      this.periodReturns.Add(new PeriodReturn("start", 0M, 1000M));

      // Set up the data so that the FileDocument can write the data
      // by itself (we still need to read, though)
      this.fileDocument.Data = this.periodReturns;
    }

    private void fileDocument_ReadDocument(object sender, FileDocumentControlLibrary.SerializeDocumentEventArgs e) {
      // Deserialize object using the FileDocument's formatter
      IFormatter formatter = this.fileDocument.Formatter;
      this.periodReturns = (BindingList<PeriodReturn>)formatter.Deserialize(e.Stream);
      this.PeriodReturnBindingSource.DataSource = this.periodReturns;

      // Set up the data so that the FileDocument can write the data
      // by itself (we still need to read, though)
      this.fileDocument.Data = this.periodReturns;
      
      //// For SOAP formatter
      //foreach( PeriodReturn periodReturn in (PeriodReturn[])formatter.Deserialize(e.Stream) ) {
      //  this.PeriodReturnBindingSource.List.Add(periodReturn);
      //}

      // Calculate returns on reload
      int periods = this.PeriodReturnBindingSource.List.Count - 1;
      if( periods < 0 ) return;
      this.CalculateReturns(periods);

      // Add to MRU menu
      this.mruMenuManager.Add(e.Filename);
    }
    private void fileDocument_WriteDocument(object sender, FileDocumentControlLibrary.SerializeDocumentEventArgs e) {
      //// Serialize object
      //IFormatter formatter = this.fileDocument.Formatter;
      //formatter.Serialize(e.Stream, this.periodReturns);

      //// For SOAP formatter
      //PeriodReturn[] returns = new PeriodReturn[this.PeriodReturnBindingSource.List.Count];
      //this.PeriodReturnBindingSource.List.CopyTo(returns, 0);
      //formatter.Serialize(e.Stream, returns);

      // Add to MRU menu
      this.mruMenuManager.Add(e.Filename);

      // Let file document take care of saving
      e.Handled = false;
    }

    private void PeriodReturnBindingSource_ListChanged(object sender, ListChangedEventArgs e) {

      // Calculate if period return was changed or deleted
      if( (e.ListChangedType == ListChangedType.ItemChanged) ||
          (e.ListChangedType == ListChangedType.ItemDeleted) ) {

        // Don't calculate if no period returns
        int periods = this.PeriodReturnBindingSource.List.Count - 1;
        if( periods < 0 ) return;

        // Update the dirty bit
        this.fileDocument.Dirty = true;

        this.CalculateReturns(periods);
      }
    }

    private void CalculateReturns(int periods) {

      // Calculate average and annual returns

      // Make sure first row rate of return stays as it is
      PeriodReturn firstPeriodReturn = (PeriodReturn)this.PeriodReturnBindingSource.List[0];
      firstPeriodReturn.ReturnRate = 0M;
      if( periods == 0 ) return;

      // Recalc returns
      decimal principle = (decimal)firstPeriodReturn.Principle;
      decimal sumOfReturns = 0;
      decimal productOfReturns = 1;
      foreach( PeriodReturn periodReturn in this.PeriodReturnBindingSource.List ) {

        sumOfReturns += (decimal)periodReturn.ReturnRate;
        productOfReturns *= (decimal)(1 + periodReturn.ReturnRate);

        // Update the read-only row properties
        principle *= (1 + (decimal)periodReturn.ReturnRate);
        periodReturn.Principle = principle;
      }

      decimal averageReturn = sumOfReturns / periods;
      decimal annualizedReturn = (decimal)(Math.Pow((double)productOfReturns, 1.0 / (double)periods)) - 1.0M;

      this.averageToolStripStatusLabel.Text = string.Format("Average Rate of Return= {0:#.000%}", averageReturn);
      this.annualizedToolStripStatusLabel.Text = string.Format("Annualized Rate of Return = {0:#.000%}", annualizedReturn);

      this.dataGridView.Refresh();
    }

    private void mruMenuManager_MruMenuItemClick(object sender, MruControlLibrary.MruMenuItemClickEventArgs e) {
      this.fileDocument.Open(e.Filename);
    }

    private void mruMenuManager_MruMenuItemFileMissing(object sender, MruControlLibrary.MruMenuItemFileMissingEventArgs e) {
      DialogResult res = MessageBox.Show("Remove " + e.Filename + "?", "Remove?", MessageBoxButtons.YesNo);
      if( res == DialogResult.Yes ) {
        e.RemoveFromMru = true;
      }
    }

    private void dragAndDropFileComponent_FileDropped(object sender, DragAndDropFileControlLibrary.FileDroppedEventArgs e) {
      foreach( string filename in e.Filenames ) {
        // Only open files with the appropriate extension
        string extension = Path.GetExtension(filename);
        if( extension == ".ror" ) {
          OpenDocument(filename);
        }
        else {
          MessageBox.Show("Can't open files of type " + extension);
        }
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -