TUGAS - Membuat Aplikasi Kalkulator Sederhana
1. Membuat Project Baru
Pilih Windows Form dan ubah Nama Project menjadi Kalkulator Sederhana
2. Desain Form seperti Layout Berikut
Buat tatanan layout widget kurang lebih seperti gambar berikut menggunakan toolbox yang sudah tersedia. Gunakan label, textbox, dan button. lalu ganti nama label tersebut.
3. Buat Action Listener Button (+, -, -, /, C)
Tekan klik dua kali pada tombol tersebut, maka secara otomatis visual studio akan membuatkan fungsi callback ketika suatu button diclick dengan mouse.
4. Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace kalkulatorsederhana | |
{ | |
public partial class Form1 : Form | |
{ | |
double nilai_1, nilai_2; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void tambah_button_Click(object sender, EventArgs e) | |
{ | |
this.hasil_textbox.Text = (nilai_1 + nilai_2).ToString(); | |
} | |
private void kurang_button_Click(object sender, EventArgs e) | |
{ | |
this.hasil_textbox.Text = (nilai_1 - nilai_2).ToString(); | |
} | |
private void kali_button_Click(object sender, EventArgs e) | |
{ | |
this.hasil_textbox.Text = (nilai_1 * nilai_2).ToString(); | |
} | |
private void bagi_button_Click(object sender, EventArgs e) | |
{ | |
if (nilai_2 == 0) | |
{ | |
this.hasil_textbox.Text = "NULL, dibagi dengan 0"; | |
} | |
else { | |
this.hasil_textbox.Text = (nilai_1 / nilai_2).ToString(); | |
} | |
} | |
private void hapus_button_Click(object sender, EventArgs e) | |
{ | |
this.hasil_textbox.Text = ""; | |
this.nilai1_textbox.Text = ""; | |
this.nilai2_textbox.Text = ""; | |
} | |
private void nilai1_textbox_TextChanged(object sender, EventArgs e) | |
{ | |
if(this.nilai1_textbox.Text!="") | |
this.nilai_1 = double.Parse(this.nilai1_textbox.Text); | |
} | |
private void nilai2_textbox_TextChanged(object sender, EventArgs e) | |
{ | |
if (this.nilai1_textbox.Text != "") | |
this.nilai_2 = double.Parse(this.nilai2_textbox.Text); | |
} | |
} | |
} |
Comments
Post a Comment