C # simple case -- unit converter,

Source: Internet
Author: User

C # simple case -- unit converter,

After several days of study, I wrote a simple winform application and pasted the source code to meet the demand.

The interface after the software is started is shown in:

The program consists of 6 labels, 8 comboboxes, 8 textBox, and 4 buttons. Set the ReadOnly attribute to true for the four textBox on the right.

When the software is started, comboBox can be used to display the default items. selectedIndex statement. By default, comboBox. selectedIndex = "-1" (no items are displayed by default). Change-1 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 to execute the conversion function. Convert the calculation result to the 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 = "Celsius" & str2 = "Fahrenheit") {d2 = 1.8 * d1 + 32; textBox8.Text = Convert. toString (d2);} if (str1 = "degree Celsius" & str2 = "") {d2 = d1 + 273.15; textBox8.Text = Convert. toString (d2);} if (str1 = "Fahrenheit" & str2 = "Celsius") {d2 = (d1-32)/1.8; textBox8.Text = Convert. toString (d2);} if (str1 = "Fahrenheit" & 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 = "Fahrenheit") {d2 = (d1-273.15) * 1.8 + 32; textBox8.Text = Convert. toString (d2 );}}}

 

In the input box, do not enter keys other than the backspace key, number key, or decimal point key (for temperature conversion, you can enter a negative number) to prevent program errors caused by non-numeric characters. Add the relevant code to the keypress event. The code example is as follows:

Private void textBox1_KeyPress (object sender, KeyPressEventArgs e) {if (e. KeyChar! = '\ B' & e. KeyChar! = 46) // enter the backspace key and decimal point key {if (e. keyChar <'0') | (e. keyChar> '9') // enter 0-9 digits {e. handled = true ;}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.