Home  >  Article  >  Backend Development  >  Detailed explanation of simple case of C# unit converter

Detailed explanation of simple case of C# unit converter

黄舟
黄舟Original
2017-03-07 10:41:551610browse

This article mainly introduces a simple case of C# unit converter in detail, a simple winform application, which has a certain reference value. Interested friends can refer to it

After a few days After studying, I wrote a simple winform application and posted the source code in case of emergency.

The interface after the software is started is as shown below:

As shown in the figure, the program consists of 6 labels, 8 comboBoxes, 8 textBoxes and 4 Composed of buttons. The four textBoxes on the right set the ReadOnly attribute to true.

When the software starts, you can make comboBox display default items. You need to use the comboBox.SelectedIndex statement. By default, comboBox.SelectedIndex="-1" (that is, no items are displayed by default). Change -1 Set to 0 to display the first item. Put the code in the Load event of the form. Code example:

private void MainForm_Load(object sender, EventArgs e)
  {
   comboBox1.SelectedIndex = 0;
   comboBox2.SelectedIndex = 1;
   comboBox3.SelectedIndex = 0;
   comboBox4.SelectedIndex = 1;
   comboBox5.SelectedIndex = 0;
   comboBox6.SelectedIndex = 1;
   comboBox7.SelectedIndex = 0;
   comboBox8.SelectedIndex = 1;
  }

Press the OK button, execute the conversion function, convert the calculation result to string type, and assign it to textBox.Text, code example:

  private void button4_Click(object sender, EventArgs e)
  {
   string str1, str2;
   str1=Convert.ToString(comboBox7.SelectedItem);
   str2=Convert.ToString(comboBox8.SelectedItem);
   double d1, d2;
   if (textBox7.Text == "")
   {
    textBox7.Text = "1";
    d1 = 1;
   }
   else
    d1 = Convert.ToDouble(textBox7.Text);
   if (str1 == str2)
   {
    d2 = d1;
    textBox8.Text = Convert.ToString(d2);
   }
   else
   {
    if(str1 == "摄氏度" && str2 == "华氏度")
    {
     d2=1.8*d1+32;
     textBox8.Text = Convert.ToString(d2);
    }
    if(str1 == "摄氏度" && str2 == "开氏度")
    {
     d2=d1+273.15;
     textBox8.Text = Convert.ToString(d2);
    }
    if(str1 == "华氏度" && str2 == "摄氏度")
    {
     d2=(d1-32)/1.8;
     textBox8.Text = Convert.ToString(d2);
    }
    if(str1 == "华氏度" && str2 == "开氏度")
    { 
     d2=(d1-32)/1.8+273.15;
     textBox8.Text = Convert.ToString(d2);
    }
    if (str1 == "开氏度" && str2 == "摄氏度")
    {
     d2 = d1 - 273.15;
     textBox8.Text = Convert.ToString(d2);
    }
    if (str1 == "开氏度" && str2 == "华氏度")
    {
     d2 = (d1 - 273.15) * 1.8 + 32;
     textBox8.Text = Convert.ToString(d2);
    }
   }
  }

disables the input of keys other than the backspace key, numeric keys and decimal point keys in the input box (a negative sign can be entered for temperature conversion) , to prevent users from entering non-numeric characters and causing program errors. Add relevant code in the keypress event. Code example:

  private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  {
   if (e.KeyChar != '\b' && e.KeyChar != 46)//允许输入退格键和小数点键
   {
    if ((e.KeyChar < &#39;0&#39;) || (e.KeyChar > &#39;9&#39;))//允许输入0-9数字 
    {
     e.Handled = true;
    }
   } 
  }

The above is the detailed explanation of the simple case of C# unit converter. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn