Hi All,

firstly thankyou for taking the time to read this, and in advance thanks for the help :)
im a newbie to this site, and also to programming.

my project is a BMI calculator, so far, i have coded the (input) textboxes, made it switchable from metric to imperial (kind of)
the problem i am having is, i need (on btn click) to have an answer (frm either imperial or metric) to go into the Answer textbox, but its not working, the metric will do this (im guessing its because it is what i coded first) but the imperial give a "NaN" statement, but if i comment out the metric codes, the imperial code works fine,
so my frustration begins.......
i have created a radio btn group box, so that i can switch between Imperial & Metric at any given time, and once 1 is selcted it disables and hides the other, great, thats just what i want, now what i am after is for that bit of code to work. the next step is for me to convert 1 input (i.e metric) and with the radio btn pressed it will convert to Imperial.

any ideas guys, on either?

Many thanks

Hi Michael,

im not trying to do that quite yet, that will come a little later, what i have is 3 group boxes
1 = imperial (with textboxes: feet, inches, stone & pounds)
2 = Metric (with textboxes: meters & kilograms)
3 = radio buttons (imperial & metric)

what i am tring to do is, whilst 1 radio button is selected (metric or imperial) i want to totally diasble the other, which i thought i had done

    private void rbtnImp_CheckedChanged(object sender, EventArgs e)
        //Setting Imperial button to take control of form, plus hidding Metric functions and disabling them//
        if (rbtnImp.Checked == true)
            gboxImp.Enabled = true;
            gboxMet.Enabled = false;                
        if (gboxMet.Enabled == false)
            gboxMet.Visible = false;
            gboxImp.Visible = true;

    private void rbtnMet_CheckedChanged(object sender, EventArgs e)
        //Setting Metric button to take control of form, plus hidding Imperial functions and disabling them//
        if (rbtnMet.Checked == true)
            gboxMet.Enabled = true;
            gboxImp.Enabled = false;
        if (gboxImp.Enabled == false)
            gboxImp.Visible = false;
            gboxMet.Visible = true;

on the form it does disable, but apperantly not in the code,
whilst 1 set is disabled, i want to run the calculator, but i get a "NaN" error.
heres the calculator code:

   private void btnCalc_Click(object sender, EventArgs e)
        //Converting metric input to numbers//
        meters = double.Parse(tboxMetMtrs.Text);
        kilos = double.Parse(tboxMetKilos.Text);

        //Converting imperial input to numbers//
        feet = double.Parse(tboxImpFoot.Text);
        inches = double.Parse(tboxImpInch.Text);
        stone = double.Parse(tboxImpStne.Text);
        pounds = double.Parse(tboxImpPnds.Text);

        //Converting feet & stone to inches & pounds//
        feet = feet * 12;
        stone = stone * 14;
        inches = inches + feet;
        pounds = pounds + stone;

        //Imperial answer set to 2 decimal places//
        answer = pounds / (inches * inches) * 703;
        lblans.Text = Math.Round(answer, 2).ToString();

        //Metric answer set to 2 decimal places//
        answer = kilos / (meters * meters);
        tboxAnswer.Text = Math.Round(answer, 2).ToString();


all i can assum is that the calculator doesn't like it having to answers? is there a way i can totally disable 1 (until needed) and perhaps when i click on the other radio button, it could reset the tboxanswer display?

thought i should add the top bit in, just so can get the full picture :)

public partial class Form1 : Form
    public Form1()
        //setting radio button imperial to be auto selected//
        rbtnImp.Checked = true;

    //Declaring global variables//
    double meters;
    double kilos;
    double pounds;
    double stone;
    double feet;
    double inches;
    double answer;
//This is for imperial radio box selected
//If Imperial si autoselected at start copy this to form_load event
private void rbImperial_CheckedChanged(object sender, EventArgs e)
            tbMeters.Enabled = false;
            tbKilograms.Enabled = false;

            tbFeet.Enabled = true;
            tbInch.Enabled = true;
            tbPound.Enabled = true;
            tbStone.Enabled = true;
        //This is for Metric radio box selected
        private void rbMetric_CheckedChanged(object sender, EventArgs e)
            tbMeters.Enabled = true;
            tbKilograms.Enabled = true;

            tbFeet.Enabled = false;
            tbInch.Enabled = false;
            tbPound.Enabled = false;
            tbStone.Enabled = false;

Edited 4 Years Ago by Michael27

For numbers please use NumericUpDown control, it is easier and you avoid string to double conversion
Why did you put conversion and calculation in one button you should split that, and as soon as posible learn about events

Edited 4 Years Ago by Michael27

Hi Michael,
i tried that code, and it didnt do anything different,
and i put the coversation and calculation in one button to make it more user friendly, as i thought C# could handle it??

Hi again Michael,
right, ive re-looked at what i was trying to achieve, and come to the conclusion, that i was totally over thinking what i needed to do (focusing on the output answer strings).
so i scrapped that idea for now, because i dont think it is necessary for what i am trying to achieve.

what i am working on now is converting metric into imperial (what you spotted at the beginning - good analysis there mate :) ).

so far i have got it to convert from:-
meters to feet (e.g 1.8m = 5.91ft - rounded down to 2d.p)
kilograms to stone (e.g 80kg = 12.6st - rounded down to 2d.p)

now i just have to try and figure out how to get anything after the deciaml point to convert to either inches or pounds, hmmmm should be interesting :) any clue? lol

cheers again

now i just have to try and figure out how to get anything after the deciaml point to convert to either inches or pounds

So if you have for example 23.45 you would need 0.45?
Did i understand that right?

Hi Michael,
yea that is what i mean, i have sine found the '%' operator, so not doing to bad with it (kind of)

Hi Arunkumars,
yea i have used the math.round feature later on in the calculator,
the problem i am having now, is i dont wont it to round down, i just want it to get rid of everything after the decimal point, but if i convert it (Convert.ToInt32 - for example) it rounds 5.9 to 6, i dont want that, i just want to keep the 5 and nothing else, any idea?


hi guys,
ive cracked it :)

        MtrstoInch = meters * ConMet_to_Inch;

        lblDebug.Text = Convert.ToString(MtrstoInch);

        tboxImpFoot.Text = Math.Truncate(MtrstoInch / FoottoInch).ToString();
        tboxImpInch.Text = Convert.ToString(Convert.ToInt32(MtrstoInch % FoottoInch));
This article has been dead for over six months. Start a new discussion instead.