亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#單位轉(zhuǎn)換器簡(jiǎn)單案例

 更新時(shí)間:2022年01月19日 08:08:36   作者:夏絮風(fēng)  
這篇文章主要為大家詳細(xì)介紹了C#單位轉(zhuǎn)換器簡(jiǎn)單案例,一個(gè)簡(jiǎn)單的winform應(yīng)用程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

經(jīng)過(guò)幾天學(xué)習(xí),寫(xiě)出了一個(gè)簡(jiǎn)單的winform應(yīng)用程序,貼出源碼,以備不時(shí)之需。

軟件啟動(dòng)后的界面如下圖所示:

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

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

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

按下確定按鈕,執(zhí)行轉(zhuǎn)換函數(shù),計(jì)算結(jié)果轉(zhuǎn)換為string類型,并將其賦值給textBox.Text,代碼實(shí)例:

  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 == "開(kāi)氏度")
    {
     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 == "開(kāi)氏度")
    { 
     d2=(d1-32)/1.8+273.15;
     textBox8.Text = Convert.ToString(d2);
    }
    if (str1 == "開(kāi)氏度" && str2 == "攝氏度")
    {
     d2 = d1 - 273.15;
     textBox8.Text = Convert.ToString(d2);
    }
    if (str1 == "開(kāi)氏度" && str2 == "華氏度")
    {
     d2 = (d1 - 273.15) * 1.8 + 32;
     textBox8.Text = Convert.ToString(d2);
    }
   }
  }

使輸入框禁止輸入除退格鍵、數(shù)字鍵和小數(shù)點(diǎn)鍵之外的按鍵(溫度的轉(zhuǎn)換可以輸入負(fù)號(hào)),防止用戶輸入非數(shù)字字符使程序發(fā)生錯(cuò)誤。在keypress事件中添加相關(guān)代碼,代碼實(shí)例:

  private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  {
   if (e.KeyChar != '\b' && e.KeyChar != 46)//允許輸入退格鍵和小數(shù)點(diǎn)鍵
   {
    if ((e.KeyChar < '0') || (e.KeyChar > '9'))//允許輸入0-9數(shù)字 
    {
     e.Handled = true;
    }
   } 
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論