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

📄 ratesofreturnform.cs

📁 中文名:Windows Forms 程序设计 英文名:Windows Forms Programming in c# 作者: Chris Sells 翻译: 荣耀 蒋贤哲 出版社:人民
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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;

using MruControlLibrary;

namespace MDIClient {
  partial class RatesOfReturnForm : Form {

    BindingList<PeriodReturn> periodReturns;
    
    public RatesOfReturnForm() {
      InitializeComponent();

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

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

    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
      // Let FormClosing event handler cancel this if it's not 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);
  }
  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);
    
    // Let file document take care of saving
    e.Handled = false;
  }

    private void PeriodReturnBindingSource_ListChanged(object sender, ListChangedEventArgs e) {

      // Don't calculate if no period returns
      int periods = this.PeriodReturnBindingSource.List.Count - 1;
      if( periods < 0 ) return;
      
      // Don't calculate if a new, null, period return is entered
      if( e.ListChangedType == ListChangedType.ItemAdded ) return;

      //// Don't calculate if changed item doesn't have a return rate
      if( e.NewIndex > periods ) return;

      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();

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

⌨️ 快捷键说明

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