Wednesday, March 20, 2013

C# 2012 : Body Mass Index

  Free Body Mass Index Calculator:

use the code for form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Advance_Body_Mass_Calculator_Deenesh
{
    public partial class Form1 : Form
    {
        //Global variables

        decimal Weight;
        decimal Height;
        decimal BodyMassIndex;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (IsValidData())
            {
                Weight = Convert.ToDecimal(txtWeight.Text);
                Height = Convert.ToDecimal(txtHeight.Text);

                BodyMassIndex = (Weight * 703) / (Height * Height);

                txtMassIndex.Text = Convert.ToString(Math.Round(BodyMassIndex, 4) + " lbs/ Inch Square");
            }
        }
        public bool IsValidData()
        {
            return
                IsPresent(txtWeight, "Weight")
                && IsPresent(txtHeight, "Height")
                && IsDecimal(txtWeight, "Weight")
                && IsDecimal(txtHeight, "Height")
                && IsWithinRange(txtWeight, "Weight", 1 , 100000)
                && IsWithinRange(txtHeight, "Height", 1 , 100);

        }
        public bool IsPresent(TextBox dc, string name)
        {
            if (dc.Text == "")
            {
                MessageBox.Show(name + " should not be empty.", "Entry Error");
                dc.Focus();
                return false;
            }
            return true;
        }

        public bool IsDecimal(TextBox dc, string name)

        {
            try
            {
                Convert.ToDecimal(dc.Text);
                return true;
            }
            catch (FormatException)
            {
                MessageBox.Show(name + "  must be a decimal value. " + " eg. 1234", "Invalid Data Error");
                dc.Focus();
                return false;
            }
        }
        public bool IsWithinRange(TextBox dc, string name, decimal min, decimal max)
        {
            decimal digit = Convert.ToDecimal(dc.Text);
            if (digit < min || digit > max)
            {
                MessageBox.Show(name + " must be between " + min + " and " + max + "!", "Range Error");
                dc.Focus();
                return false;
            }
            return true;
       
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void digitChange(object sender, EventArgs e)
        {
            txtMassIndex.Clear();
        }
    }
}

--Use the code for form1.designer.cs

namespace Advance_Body_Mass_Calculator_Deenesh
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnCalculate = new System.Windows.Forms.Button();
            this.btnClose = new System.Windows.Forms.Button();
            this.txtWeight = new System.Windows.Forms.TextBox();
            this.txtHeight = new System.Windows.Forms.TextBox();
            this.txtMassIndex = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // btnCalculate
            //
            this.btnCalculate.Location = new System.Drawing.Point(23, 262);
            this.btnCalculate.Margin = new System.Windows.Forms.Padding(4);
            this.btnCalculate.Name = "btnCalculate";
            this.btnCalculate.Size = new System.Drawing.Size(112, 32);
            this.btnCalculate.TabIndex = 2;
            this.btnCalculate.Text = "Calculate";
            this.btnCalculate.UseVisualStyleBackColor = true;
            this.btnCalculate.Click += new System.EventHandler(this.button1_Click);
            //
            // btnClose
            //
            this.btnClose.Location = new System.Drawing.Point(246, 262);
            this.btnClose.Margin = new System.Windows.Forms.Padding(4);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(81, 32);
            this.btnClose.TabIndex = 3;
            this.btnClose.Text = "Close";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            //
            // txtWeight
            //
            this.txtWeight.Location = new System.Drawing.Point(150, 18);
            this.txtWeight.Margin = new System.Windows.Forms.Padding(4);
            this.txtWeight.Name = "txtWeight";
            this.txtWeight.Size = new System.Drawing.Size(177, 24);
            this.txtWeight.TabIndex = 0;
            this.txtWeight.TextChanged += new System.EventHandler(this.digitChange);
            //
            // txtHeight
            //
            this.txtHeight.Location = new System.Drawing.Point(150, 84);
            this.txtHeight.Margin = new System.Windows.Forms.Padding(4);
            this.txtHeight.Name = "txtHeight";
            this.txtHeight.Size = new System.Drawing.Size(177, 24);
            this.txtHeight.TabIndex = 1;
            this.txtHeight.TextChanged += new System.EventHandler(this.digitChange);
            //
            // txtMassIndex
            //
            this.txtMassIndex.Location = new System.Drawing.Point(153, 187);
            this.txtMassIndex.Margin = new System.Windows.Forms.Padding(4);
            this.txtMassIndex.Name = "txtMassIndex";
            this.txtMassIndex.ReadOnly = true;
            this.txtMassIndex.Size = new System.Drawing.Size(187, 24);
            this.txtMassIndex.TabIndex = 4;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(20, 18);
            this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(91, 18);
            this.label1.TabIndex = 5;
            this.label1.Text = "Weight (lbs):";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(20, 87);
            this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(95, 18);
            this.label2.TabIndex = 6;
            this.label2.Text = "Height (inch):";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(10, 190);
            this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(125, 18);
            this.label3.TabIndex = 7;
            this.label3.Text = "Body Mass Index:";
            //
            // Form1
            //
            this.AcceptButton = this.btnCalculate;
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(353, 314);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtMassIndex);
            this.Controls.Add(this.txtHeight);
            this.Controls.Add(this.txtWeight);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnCalculate);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Margin = new System.Windows.Forms.Padding(4);
            this.Name = "Form1";
            this.Text = "Advance Body Mass Calculator";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnCalculate;
        private System.Windows.Forms.Button btnClose;
        private System.Windows.Forms.TextBox txtWeight;
        private System.Windows.Forms.TextBox txtHeight;
        private System.Windows.Forms.TextBox txtMassIndex;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
    }
}


Thank you!

4 comments:

  1. This blog site is pretty good! How was it made . I view something genuinely interesting about your site so I saved to my bookmarks . You can visit my site.
    calculate bmi

    ReplyDelete
  2. Thank you. I build this using C# program. Its pretty easy to make actually. Get the height and weight, use the formula , calculate and then display it. :)

    ReplyDelete
  3. Hello. Could you help how to make a sleep calculator in winforms?

    ReplyDelete
  4. Hello. Could you help how to make a sleep calculator in winforms?

    ReplyDelete