C # Unit Converter's graphic code detailed introduction

Source: Internet
Author: User
This article is mainly for you to introduce in detail the C # Unit converter simple case, a simple WinForm application, with a certain reference value, interested in small partners can refer to

After several days of study, wrote a simple WinForm application, put out the source code, in case of a rainy day.

After the software is launched, the interface looks like this:

, the program consists of 6 labels, 8 combobox, 8 textbox, and 4 button. The right 4 textbox sets the ReadOnly property to True.

When the software starts, you can let the ComboBox display the default entries, you need to use the Combobox.selectedindex statement, by default, combobox.selectedindex= "-1" (that is, no items are displayed by default), and 1 to 0 to display the first item. Put the code in the form's Load event. 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 result to a string type, and assign it to TextBox.Text, code instance:

  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 = = "Kelvin") {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 = = "Kelvin") {d2= (d1-32)/1.8+273.15;    Textbox8.text = convert.tostring (D2);     } if (str1 = = "Kelvin" && str2 = = "Celsius") {d2 = d1-273.15;   Textbox8.text = convert.tostring (D2); } if (str1 = = "Kelvin" && str2 = = "Fahrenheit") {D2 = (d1-273.15) * 1.8 + 32;    Textbox8.text = convert.tostring (D2); }   }  }

Prevents the input box from entering keys other than backspace, numeric, and decimal (the temperature conversion can enter a minus sign), preventing the user from entering non-numeric characters to make the program error. Add the relevant code in the KeyPress event, code example:

  private void Textbox1_keypress (object sender, KeyPressEventArgs e)  {   if (e.keychar! = ' \b ' && e.keychar!) = 46)//Allow input of backspace and decimal key   {    if (E.keychar < ' 0 ') | | (E.keychar > ' 9 ')) Allow input 0-9 number     {     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.