


Detailed introduction to the graphic code of C# unit converter
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 < '0') || (e.KeyChar > '9'))//允许输入0-9数字 { e.Handled = true; } } }
The above is the detailed content of Detailed introduction to the graphic code of C# unit converter. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
