Home

F# Example: Elementary Addition

   

Introduction

This example application is used to assist in performing elementary additions of numbers from 0 to 99.

Elementary Addition

open System;
open System.Drawing;
open System.Windows.Forms;

let ElementaryAddition : System.Windows.Forms.Form = new Form();
let lblOperand1 : Label   = new Label();
let lblOperand2 : Label   = new Label();
let txtResult   : TextBox = new TextBox();

let lblNewOperationClick (e : EventArgs) : unit =
    let mutable operand1 : int = 0;
    let mutable operand2 : int = 0;
    
    let rnd : Random  = new Random();

    operand1 <- rnd.Next(99);
    operand2 <- rnd.Next(99);
    let result : int = operand1 + operand2;

    lblOperand1.Text <- operand1.ToString();
    lblOperand2.Text <- operand2.ToString();
    txtResult.Text <- "";
    txtResult.Focus() |> ignore;

let LoadForm (e : EventArgs) : unit =
        lblNewOperationClick(e);

let lblCheckAnswerClick (e : EventArgs) =
    let mutable operand1 : int = 0;
    let mutable operand2 : int = 0;
    let mutable result   : int = 0;

    operand1 <- (int lblOperand1.Text);
    operand2 <- (int lblOperand2.Text);

    result <- (int txtResult.Text);

    if result = (operand1 + operand2) then
        ignore(MessageBox.Show("WOW - Good Answer",
                        "Elementary Addition",
                        MessageBoxButtons.OK, MessageBoxIcon.Information));
    else
        MessageBox.Show("PSSST - Wrong Answer",
                        "Elementary Addition",
                        MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore;

    lblNewOperationClick(e);

let lblQuitClick (e : EventArgs) =
    ElementaryAddition.Close();

// Label: Operand 1
lblOperand1.AutoSize <- true;
lblOperand1.Font <- new System.Drawing.Font("Times New Roman", 48.00f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0uy);
lblOperand1.ForeColor <- System.Drawing.Color.Blue;
lblOperand1.Location <- new System.Drawing.Point(17, 20);
lblOperand1.Text <- "00";
lblOperand1.TextAlign <- System.Drawing.ContentAlignment.MiddleCenter;
ElementaryAddition.Controls.Add(lblOperand1);

// Label: Operation
let lblOperation : Label = new Label();
lblOperation.AutoSize <- true;
lblOperation.Font <- new System.Drawing.Font("Arial", 48.00f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0uy);
lblOperation.ForeColor <- System.Drawing.Color.Maroon;
lblOperation.Location <- new System.Drawing.Point(120, 24);
lblOperation.Text <- "+";
lblOperation.TextAlign <- System.Drawing.ContentAlignment.MiddleCenter;
ElementaryAddition.Controls.Add(lblOperation);

// Label: Operand 2
lblOperand2.AutoSize <- true;
lblOperand2.Font <- new System.Drawing.Font("Tahoma", 48.00f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0uy);
lblOperand2.ForeColor <- System.Drawing.Color.Blue;
lblOperand2.Location <- new System.Drawing.Point(184, 24);
lblOperand2.Text <- "00";
lblOperand2.TextAlign <- System.Drawing.ContentAlignment.MiddleCenter;
ElementaryAddition.Controls.Add(lblOperand2);

// Label: Equal
let lblEqual : Label = new Label();
lblEqual.AutoSize <- true;
lblEqual.Font <- new System.Drawing.Font("Arial", 48.00f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0uy);
lblEqual.ForeColor <- System.Drawing.Color.Teal;
lblEqual.Location <- new System.Drawing.Point(291, 24);
lblEqual.Text <- "=";
lblEqual.TextAlign <- System.Drawing.ContentAlignment.MiddleCenter;
ElementaryAddition.Controls.Add(lblEqual);

// Text Box: Result
txtResult.Font <- new System.Drawing.Font("Tahoma", 48.00f, System.Drawing.FontStyle.Bold);
txtResult.Location <- new System.Drawing.Point(366, 24);
txtResult.Size <- new System.Drawing.Size(130, 85);
txtResult.Text <- "000";
ElementaryAddition.Controls.Add(txtResult);

// Label: New Operation
let lblNewOperation : Label = new Label();
lblNewOperation.AutoSize <- true;
lblNewOperation.BackColor <- System.Drawing.Color.Maroon;
lblNewOperation.BorderStyle <- BorderStyle.Fixed3D;
lblNewOperation.Font <- new System.Drawing.Font("Tahoma", 27.75f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0uy);
lblNewOperation.ForeColor <- System.Drawing.Color.White;
lblNewOperation.Location <- new System.Drawing.Point(27, 126);
lblNewOperation.Text <- "New Operation";
lblNewOperation.Click.Add(lblNewOperationClick);
ElementaryAddition.Controls.Add(lblNewOperation);

// Label: Check Answer
let lblCheckAnswer : Label = new Label();
lblCheckAnswer.AutoSize <- true;
lblCheckAnswer.BackColor <- System.Drawing.Color.Maroon;
lblCheckAnswer.BorderStyle <- BorderStyle.Fixed3D;
lblCheckAnswer.Font <- new System.Drawing.Font("Tahoma", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0uy);
lblCheckAnswer.ForeColor <- System.Drawing.Color.White;
lblCheckAnswer.Location <- new System.Drawing.Point(363, 126);
lblCheckAnswer.Text <- "Check";
lblCheckAnswer.Click.Add(lblCheckAnswerClick);
ElementaryAddition.Controls.Add(lblCheckAnswer);

// Label: Quit
let lblQuit : Label = new Label();
lblQuit.AutoSize <- true;
lblQuit.BorderStyle <- BorderStyle.FixedSingle;
lblQuit.Font <- new System.Drawing.Font("Tahoma", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0uy);
lblQuit.Location <- new System.Drawing.Point(376, 188);
lblQuit.Size <- new System.Drawing.Size(100, 47);
lblQuit.Text <- "Quit";
lblQuit.Click.Add(lblQuitClick);
ElementaryAddition.Controls.Add(lblQuit);

// Form: Elementary Addition
ElementaryAddition.MaximizeBox <- false;
ElementaryAddition.MinimizeBox <- false;
ElementaryAddition.Text <- "Elementary Addition";
ElementaryAddition.Load.Add(LoadForm);
ElementaryAddition.ClientSize <- new System.Drawing.Size(520, 257);
ElementaryAddition.FormBorderStyle <- FormBorderStyle.FixedDialog;
ElementaryAddition.StartPosition <- FormStartPosition.CenterScreen;

[<EntryPoint>]
let main argv = 
    Application.Run(ElementaryAddition);
    0
 
   
                   
 

Home Copyright © 2015, FunctionX Home