Source

Let's Play TDD .NET / Finances.UI / ApplicationForm.cs

Full commit
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Finances.Domain;

namespace Finances.UI
{
    public partial class ApplicationForm : Form
    {
        public static readonly string TITLE = "Financial Projector";
        public static readonly Point INITIAL_POSITION = new Point(400, 300);
        public static readonly Size INITIAL_SIZE = new Size(900, 400);

        private IApplicationModel applicationModel;

        public ApplicationForm()
        {
            addComponents();
        }

        public ApplicationForm(IApplicationModel applicationModel)
        {

            this.applicationModel = applicationModel;
            Text = TITLE;
            Location = INITIAL_POSITION;
            Size = INITIAL_SIZE;
            addComponents();
        }

        private void addComponents()
        {
            Panel contentPane = new Panel();
            contentPane.Dock = DockStyle.Fill;
            contentPane.Controls.Add(ForecastTable());
            contentPane.Controls.Add(StartingBalanceField());
           
            this.Controls.Add(contentPane);
        }

        public DollarsTextField StartingBalanceField()
        {
            // TODO: need to test that startingBalanceField is initialized with correct value
            DollarsTextField field = new DollarsTextField(new Dollars(666));
            field.Dock = DockStyle.Top;
            field.TextChanged += (obj, ev) =>
                                     {
                                         try
                                         {
                                             int value = int.Parse(field.Text);
                                             applicationModel.SetStartingBalance(field.GetDollars());
                                             

                                         }
                                         catch (FormatException)
                                         {

                                             //TODO: Nedd to string out error handling
                                             Console.WriteLine("ERROR: " + field.Text);
                                         }
                                     };
         
            return field;
        }

        private Control ForecastTable()
        {
            ForecastTable table =  new ForecastTable(applicationModel.StockMarketTableModel());
            table.Dock = DockStyle.Fill;
            table.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            table.RowHeadersVisible = false;
            table.AllowUserToResizeRows = false;
            table.ClearSelection();
            return table;
        }
    }
}