Hi Guys,

wondered if someone could help me please, i have been scratching my head on this for a couple of days, and have tried multiple ways of solving this (based on my beginners knowledge)

what i am tring to do is to get info from 1 set of textboxes (i.e tbox_imperial_feet & tbox_imperial_inch to convert to tbox_metric_mtrs and vica versa) now although i have sussed out the code to convert them.

the problem i am having is that i can convert meters to feet (conversions are done by selecting radio buttons (either Imperial or Metric) without problem, but i when i type in feet & inches it wont convert to meters, its as if i have blocked out the feet and inches textboxes,
can any see where i have gone wrong? or have any ideas?

many thanks in advance
Ade

p.s heres my code (so far)

 public Form1()
        {
            InitializeComponent();

            //*************************************************//
            // Setting radio button metric to be auto selected //
            //*************************************************//

            rbtn_Metric.Checked = true;                        
            tbox_Metric_Mtrs.Enabled = true;            
            tbox_Metric_Kilos.Enabled = true;
            gbox_Metric.Visible = true;
            gbox_Metric.Enabled = true;

        }


        //****************************//
        // Declaring global variables //
        //****************************//

        const double Const_Meter_To_Inch = 39.370078;
        const double Const_Kilo_To_Pounds = 2.20462;
        const double Const_Inch_To_Meter = 0.0254;
        const double Const_Dbl_Feet_to_Inch = 12;
        const int Const_Foot_To_Inch = 12;
        const int Const_Pounds_To_Stone = 14;

        bool bool_Clear_Display = false;

        double dbl_Meters;
        double dbl_Kilos;
        double dbl_Pounds;
        double dbl_Stone;
        double dbl_Feet;
        double dbl_Inches;
        double dbl_Answer;
        double dbl_Meters_To_Feet;
        double dbl_Kilo_To_Stone;
        double dbl_Meters_To_Inch;
        double dbl_Kilo_To_Pounds;
        double dbl_Temp_Feet_to_Inch;
        double dbl_feet_Inch_Answer;
        double dbl_Inch_to_Meter_Answer;



        private void rbtn_Metric_CheckedChanged(object sender, EventArgs e)
        {
            //*************************************************************************************************//
            //Setting Metric button to take control of form, plus hidding Imperial functions and disabling them//
            //*************************************************************************************************//

            if (rbtn_Metric.Checked == true)
            {
                gbox_Metric.Visible = true;
                gbox_Metric.Enabled = true;                
                tbox_Metric_Mtrs.Enabled = true;
                tbox_Metric_Kilos.Enabled = true;
            }

            if (gbox_Imperial.Enabled == false)
            {
                gbox_Imperial.Visible = false;
                gbox_Metric.Visible = true;
                tbox_Metric_Mtrs.Enabled = true;              
                tbox_Metric_Kilos.Enabled = true;



                //*************************************************************//
                // Setting textboxes to 0 if imperial rabio button is selected //
                //*************************************************************//


                if (string.IsNullOrEmpty(tbox_Imperial_Feet.Text))
                {
                    tbox_Imperial_Feet.Text = Convert.ToString("0");
                }
                if (string.IsNullOrEmpty(tbox_Imperial_Inch.Text))
                {
                    tbox_Imperial_Inch.Text = Convert.ToString("0");
                }
                if (string.IsNullOrEmpty(tbox_Imperial_Pounds.Text))
                {
                    tbox_Imperial_Pounds.Text = Convert.ToString("0");
                }
                if (string.IsNullOrEmpty(tbox_Imperial_Stone.Text))
                {
                    tbox_Imperial_Stone.Text = Convert.ToString("0");
                }





                //**************************************************//
                // Passing Metric textbox info to a double variable //
                //**************************************************//

                dbl_Feet = double.Parse(tbox_Imperial_Feet.Text);
                dbl_Inches = double.Parse(tbox_Imperial_Inch.Text);
                dbl_Stone = double.Parse(tbox_Imperial_Stone.Text);
                dbl_Pounds = double.Parse(tbox_Imperial_Pounds.Text);


                //*******************************//
                // CONVERTING IMPERIAL TO METRIC //
                //*******************************//
                //    Feet & Inches To Meters    //
                //*******************************//

                dbl_Temp_Feet_to_Inch = dbl_Feet * Const_Dbl_Feet_to_Inch;

                lbl_Debug.Text = Convert.ToString(dbl_Temp_Feet_to_Inch);

                dbl_feet_Inch_Answer = dbl_Temp_Feet_to_Inch + dbl_Inches;

                lbl_Debug_2.Text = Convert.ToString(dbl_feet_Inch_Answer);

                dbl_Inch_to_Meter_Answer = dbl_feet_Inch_Answer * Const_Inch_To_Meter;

                lbl_Debug.Text = Convert.ToString(dbl_Inch_to_Meter_Answer);

                tbox_Metric_Mtrs.Text = Math.Round(dbl_Inch_to_Meter_Answer, 2).ToString();
            }


        }
        private void rbtn_Imperial_CheckedChanged(object sender, EventArgs e)
        {
            //**************************************************************************************************//
            // Setting Imperial button to take control of form, plus hiding Metric functions and disabling them //
            //**************************************************************************************************//

            if (rbtn_Imperial.Checked == true)
            {
                gbox_Imperial.Enabled = true;
                gbox_Metric.Enabled = false;               
                tbox_Metric_Mtrs.Enabled = false;
                tbox_Metric_Kilos.Enabled = false;

            }

            if (gbox_Metric.Enabled == false)
            {
                gbox_Metric.Visible = false;
                gbox_Imperial.Visible = true;
                tbox_Imperial_Feet.Enabled = true;
                tbox_Imperial_Inch.Enabled = true;
                tbox_Imperial_Pounds.Enabled = true;
                tbox_Imperial_Stone.Enabled = true;



            //*************************************************************//
            // Setting textboxes to 0 if metric rabio button is selected //
            //*************************************************************//


                if (string.IsNullOrEmpty(tbox_Metric_Kilos.Text))
                {
                    tbox_Metric_Kilos.Text = Convert.ToString("0");
                }                
                if (string.IsNullOrEmpty(tbox_Metric_Mtrs.Text))
                {
                    tbox_Metric_Mtrs.Text = Convert.ToString("0");
                }


            }

            //**************************************************//
            // Passing Metric textbox info to a double variable //
            //**************************************************//

            //dbl_Meters = double.Parse(tbox_Metric_Meters.Text);
            dbl_Meters = double.Parse(tbox_Metric_Mtrs.Text);

            dbl_Kilos = double.Parse(tbox_Metric_Kilos.Text);


            //*******************************//
            // CONVERTING METRIC TO IMPERIAL //
            //*******************************//
            //    Meters to Feet & Inches    //
            //*******************************//

            dbl_Meters_To_Inch = dbl_Meters * Const_Meter_To_Inch;

            //lbl_Debug.Text = Convert.ToString(dbl_Meters_To_Inch);

            tbox_Imperial_Feet.Text = Math.Truncate(dbl_Meters_To_Inch / Const_Foot_To_Inch).ToString();
            tbox_Imperial_Inch.Text = Convert.ToString(Convert.ToInt32(dbl_Meters_To_Inch % Const_Foot_To_Inch));


            //*******************************//
            //    Kilos to Stone & Pounds    //
            //*******************************//

            dbl_Kilo_To_Pounds = dbl_Kilos * Const_Kilo_To_Pounds;

            lbl_Debug_2.Text = Convert.ToString(dbl_Kilo_To_Pounds);

            tbox_Imperial_Stone.Text = Math.Truncate(dbl_Kilo_To_Pounds / Const_Pounds_To_Stone).ToString();
            tbox_Imperial_Pounds.Text = Convert.ToString(Convert.ToInt32(dbl_Kilo_To_Pounds % Const_Pounds_To_Stone));
        }



    }
}

Recommended Answers

All 3 Replies

Your conversion code seems to be sound. The only anomaly i can see is that depending on which radio button is checked, only the metric tbox's are enabled or disabled. Perhaps commenting out all the statements enabling or diabling the tbox's, and then seeing how that impacts your problem, might give a better insight into where the problem is. Since your tbox's are always gonna have a value do you really need to be enabling and diabling them? If there is a value in the answer box already it can easily be ignored since you'll be replacing that value with the answer. Just a few thoughts hope they help.

Hi Tinstaafl,

i commented out the textboxes (not sure y i only enabled/disabled the metric ones - think ive been looking at the same code for too long now lol) but after i comment out the textboxes i get the same problem, entering a value into the metric textbox it will convert to imperial, but not the other way around, any ideas? tis a lil frustrating haha

Noticed something else. If you take a look at the 2 code snippets from above(lines 136-139 and lines 54-57) they don't follow the same pattern even though they're supposed to essentially do the same thing only in reverse. Perhaps if line 56 was gbox_Imperial.Enabled = false; that might make a difference.

            if (rbtn_Imperial.Checked == true)
            {
                gbox_Imperial.Enabled = true;
                gbox_Metric.Enabled = false; 



            if (rbtn_Metric.Checked == true)
            {
                gbox_Metric.Visible = true;
                gbox_Metric.Enabled = true;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.