c#簡單案例--單位轉換器


經過幾天學習,寫出了一個簡單的winform應用程序,貼出源碼,以備不時之需。

軟件啟動后的界面如下圖所示:

如圖,該程序由6個label、8個comboBox、8個textBox和4個button組成。右邊4個textBox設置ReadOnly屬性為true。

軟件啟動時,可以讓comboBox顯示默認項,需要用到comboBox.SelectedIndex語句,默認情況下,comboBox.SelectedIndex="-1"(即默認不顯示任何項),將-1改為0即可顯示第一項。將代碼放到窗體的Load事件里。代碼實例:

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;
        }

  

按下確定按鈕,執行轉換函數,計算結果轉換為string類型,並將其賦值給textBox.Text,代碼實例:

        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);
                }
            }
        }

 

使輸入框禁止輸入除退格鍵、數字鍵和小數點鍵之外的按鍵(溫度的轉換可以輸入負號),防止用戶輸入非數字字符使程序發生錯誤。在keypress事件中添加相關代碼,代碼實例:

        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;
                }
            }  
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM