Jumat, 24 April 2015

Membuat Program Konversi Suhu di Visual Studio 2010

Kali ini kita akan membuat program konversi suhu. Dalam program ini, kita menggunakan fungsi If dan statement Val, ini rupa programnya.....
Dan bentuk formnya begini.....
Anda belum tahu cara memasukkan masing - masing elemen (ciat.... kayak avatar)? klik di Dasar - Dasar Visual Studio 2010
TextBox1 tempat isi (eh.... input) angka suhu yang ingin diketahui, ComboBox1 dan berisi pilihan suhu yang ingin diketahui dan ComboBox2 berisi suhu yang hendak diinputkan. Hasilnya akan keluar di TextBox2.

Dan agar terlihat seperti program yang saya tampilkan di paling atas, klik ganda di Form, dan ini Statementnya :
Me.Text = "Konverter Suhu"GroupBox1.Text = "Merubah Suhu"Label1.Text = "Dari"Label2.Text = "Ke"Label3.Text = "Hasil"Label4.Text = ""Button1.Text = "Hitung"Button2.Text = "Hapus"

Ah.....
Kita bicarakan elemen baru, si ComboBox, dalam ComboBox ada daftar pilihan, pilihannya kita yang tentukan, :D
Cara untuk menentukan pilihan apa saja yang akan ada dalam ComboBox adalah :
Pertama - Klik ComboBox, arahkan pandangan ke Properties dan cari Items
Lalu saya klik tombol kecil di sebelah kata (Collection) dan akan muncul window baru, karena saya membuat program untuk konversi suhu maka saya memasukkan 4 suhu (Celci, Reamu, Fahr, dan Kelv). Kita lakukan hal yang sama untuk ComboBox2. Dalam menentukan pilihan, satu baris untuk satu pilihan saja ya....
Oke, berikutnya kita debugging, untuk melihat apa yang telah kita perbuat tadi.....
Berikutnya kita akan menentukan aksi Tombol Hitung (Button1), ini akan jadi panjang........

Untuk Konversi Celcius ke Reamur, Fahrenheit dan Kelvin. Statementnya :
If ComboBox1.SelectedItem = "°R" And ComboBox2.SelectedItem = "°C" ThenTextBox2.Text = (4 / 5) * Val(TextBox1.Text)Label4.Text = ComboBox1.TextElseIf ComboBox1.SelectedItem = "°F" And ComboBox2.SelectedItem = "°C" ThenTextBox2.Text = (9 / 5) * Val(TextBox1.Text) + 32Label4.Text = ComboBox1.TextElseIf ComboBox1.SelectedItem = "°K" And ComboBox2.SelectedItem = "°C" ThenTextBox2.Text = Val(TextBox1.Text) + 273Label4.Text = ComboBox1.Text
Untuk Reamur ke Celcius, Fahrenheit dan Kelvin (diletakkan setelah mengetik yang barusan):
ElseIf ComboBox1.SelectedItem = "°C" And ComboBox2.SelectedItem = "°R" Then
TextBox2.Text = (5 / 4) * Val(TextBox1.Text)
Label4.Text = ComboBox1.Text
ElseIf ComboBox1.SelectedItem = "°F" And ComboBox2.SelectedItem = "°R" Then
TextBox2.Text = (9 / 4) * Val(TextBox1.Text) + 32
Label4.Text = ComboBox1.Text
ElseIf ComboBox1.SelectedItem = "°K" And ComboBox2.SelectedItem = "°R" Then
TextBox2.Text = (5 / 4) * Val(TextBox1.Text) + 273
Label4.Text = ComboBox1.Text 
Berikutnya, Fahrenheit Ke Celsius, Reamur dan Kelvin :
ElseIf ComboBox1.SelectedItem = "°C" And ComboBox2.SelectedItem = "°F" Then
TextBox2.Text = (5 / 9) * (Val(TextBox1.Text) - 32)
Label4.Text = ComboBox1.Text
ElseIf ComboBox1.SelectedItem = "°R" And ComboBox2.SelectedItem = "°F" Then
TextBox2.Text = (4 / 9) * (Val(TextBox1.Text) - 32)
Label4.Text = ComboBox1.Text
ElseIf ComboBox1.SelectedItem = "°K" And ComboBox2.SelectedItem = "°F" Then
TextBox2.Text = (5 / 9) * (Val(TextBox1.Text) - 32) + 273
Label4.Text = ComboBox1.Text 
Dan yang terakhir, Kelvin ke Celcius, Reamur dan Fahrenheit :
ElseIf ComboBox1.SelectedItem = "°C" And ComboBox2.SelectedItem = "°K" ThenTextBox2.Text = Val(TextBox1.Text) - 273Label4.Text = ComboBox1.TextElseIf ComboBox1.SelectedItem = "°R" And ComboBox2.SelectedItem = "°K" ThenTextBox2.Text = (4 / 5) * (Val(TextBox1.Text) - 273)Label4.Text = ComboBox1.TextElseIf ComboBox1.SelectedItem = "°F" And ComboBox2.SelectedItem = "°K" ThenTextBox2.Text = (9 / 5) * (Val(TextBox1.Text) - 273) + 32Label4.Text = ComboBox1.TextEnd If

Berikutnya kita ke tombol Hapus (Button2), statementnya :
TextBox1.Text = ""
TextBox2.Text = ""
ComboBox1.ResetText()
ComboBox2.ResetText()
Label4.Text = ""
 

Debugging terakhir, testing program.....
Saya masukkan angka 45 di TextBox1 lalu di ComboBox2 memilih °C. Di ComboBox1 saya Memilih °K lalu klik tombol hitung dan Hasilnya.....
Jika kurang akurat, anda bisa menghitung sendiri kan....?
Sekian artikel ini, Semoga bermanfaat.

3 komentar: