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 explanation of the simple case of C# unit converter. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft