CONVERSOR DE GRAUS EN J# AMB VISUAL STUDIO 2005

Construim el següent formulari

diseño.JPGDibujo.JPG

 

El codi del programa sería el següent en J#

package conversor_de_grados;

 

import System.Collections.Generic.*;

import System.Data.*;

import System.Drawing.*;

import System.ComponentModel.*;

import System.Windows.Forms.*;

 

/**

 * Summary description for Form1.

 */

public class Form1 extends System.Windows.Forms.Form

{

      //Declarem les variables:

      double centi, faren, kelvin;

      String Txtcenti, Txtfaren, Txtkelvin;

      int referencia;

 

 

      private RadioButton radioButton1;

      private RadioButton radioButton2;

      private RadioButton radioButton3;

      private TextBox textBox1;

      private TextBox textBox2;

      private TextBox textBox3;

      private Button button1;

      private Button button2;

      /**

       * Required designer variable.

       */

      private System.ComponentModel.IContainer components;

 

      public Form1()

      {

            //

            // Required for Windows Form Designer support

            //

            InitializeComponent();

 

            //Inicialitzem els valors de les variables:

 

            centi = 0;

            faren = 0;

            kelvin = 0;

            Txtcenti = "0";

            Txtfaren = "32";

            Txtkelvin = "273";

            textBox1.set_Text(Txtcenti);

            textBox2.set_Text(Txtfaren);

            textBox3.set_Text(Txtkelvin);

            referencia = 0;

 

      }

 

      /* #region Windows Form Designer generated code

     

      #endregion */

 

      private void button2_Click(Object sender, System.EventArgs e)

      {

            if (referencia == 0)

            {

                  textBox1.set_Text("Escoja una referencia");

            }

            else

            {

 

                  switch (referencia)

                  {

                        case 1:

                             centi = Double.valueOf(textBox1.get_Text()).doubleValue();

                             faren = (centi * 9 / 5) + 32;

                             kelvin = centi + 273;

                             break;

                        case 2:

                             faren = Double.valueOf(textBox2.get_Text()).doubleValue();

                             centi = (faren - 32)*5 / 9;

                             kelvin = centi + 273;

                             break;

                        case 3:

                             kelvin = Double.valueOf(textBox3.get_Text()).doubleValue();

                             centi = kelvin - 273;

                             faren = (centi * 9 / 5) + 32;

                             break;

                  }

                  Txtcenti = String.valueOf(centi).toString();

                  Txtfaren = String.valueOf(faren).toString();

                  Txtkelvin = String.valueOf(kelvin).toString();

                  textBox1.set_Text(Txtcenti);

                  textBox2.set_Text(Txtfaren);

                  textBox3.set_Text(Txtkelvin);

            }

      }

 

      private void radioButton1_CheckedChanged(Object sender, System.EventArgs e)

      {

            if (radioButton1.get_Checked() == true)

            {

                  referencia = 1;

            }

      }

 

      private void radioButton2_CheckedChanged(Object sender, System.EventArgs e)

      {

            if (radioButton2.get_Checked() == true)

            {

                  referencia = 2;

            }

      }

 

      private void radioButton3_CheckedChanged(Object sender, System.EventArgs e)

      {

            if (radioButton3.get_Checked() == true)

            {

                  referencia = 3;

            }

      }

 

      private void button1_Click(Object sender, System.EventArgs e)

      {

            centi = 0;

            faren = 0;

            kelvin = 0;

            Txtcenti = "0";

            Txtfaren = "32";

            Txtkelvin = "273";

            textBox1.set_Text(Txtcenti);

            textBox2.set_Text(Txtfaren);

            textBox3.set_Text(Txtkelvin);

            referencia = 0;

 

      }

}