tugas 1 PBKK
Nama : Muhammad Febriansyah
NRP : 5025211164
Kelas : Pemrograman Berbasis Kerangka Kerja (B)
Tahun Ajaran : 2023
di buat kalkulator dengan menggunakan .NET Framework pada tools Visual Studio. dengan operasi yang dapat digunakan yaitu penjumlahan, pengurangan, perkalian, dan pembagian. Serta terdapat tombol C untuk mengkapus.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinForms_tugas1;
namespace WinForms_tugas1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.cs
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 WinForms_tugas1
{
public partial class Form1 : Form
{
Double Result_Value = 0;
String Operator_Performed = " ";
bool PerformedOp = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button15_Click(object sender, EventArgs e)
{
// numbers button and point
if (textBox_Result.Text == "0" || PerformedOp)
textBox_Result.Clear();
PerformedOp = false;
Button button = (Button)sender;
if (button.Text == ".")
{
if (!textBox_Result.Text.Contains("."))
textBox_Result.Text += button.Text;
}
else
textBox_Result.Text += button.Text;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Operator_click_Event(object sender, EventArgs e)
{
// +, -, *, / operators
Button button = (Button)sender;
if (Result_Value != 0)
{
button16.PerformClick();
Operator_Performed = button.Text;
label_Show_Op.Text = Result_Value + " " + Operator_Performed;
PerformedOp = true;
}
else
{
Operator_Performed = button.Text;
Result_Value = Double.Parse(textBox_Result.Text);
label_Show_Op.Text = Result_Value + " " + Operator_Performed;
PerformedOp = true;
}
}
private void button5_Click(object sender, EventArgs e)
{
//CLEAR ENTRY BUTTON
textBox_Result.Text = "0";
}
private void button6_Click(object sender, EventArgs e)
{
//CLEAR BUTTON
textBox_Result.Text = "0";
Result_Value = 0;
label_Show_Op.Text = " ";
}
private void button16_Click(object sender, EventArgs e)
{
// EQUALS BUTTON
switch (Operator_Performed)
{
case "+":
textBox_Result.Text = (Result_Value + Double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
textBox_Result.Text = (Result_Value - Double.Parse(textBox_Result.Text)).ToString();
break;
case "*":
textBox_Result.Text = (Result_Value * Double.Parse(textBox_Result.Text)).ToString();
break;
case "/":
textBox_Result.Text = (Result_Value / Double.Parse(textBox_Result.Text)).ToString();
break;
default:
break;
}
Result_Value = Double.Parse(textBox_Result.Text);
label_Show_Op.Text = " ";
}
}
}
Form1.Designer.cs
namespace WinForms_tugas1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
button1 = new Button();
button2 = new Button();
button3 = new Button();
button4 = new Button();
button5 = new Button();
button6 = new Button();
button7 = new Button();
button8 = new Button();
button9 = new Button();
button10 = new Button();
button12 = new Button();
button13 = new Button();
button14 = new Button();
button15 = new Button();
button20 = new Button();
button18 = new Button();
button16 = new Button();
button17 = new Button();
textBox_Result = new TextBox();
label_Show_Op = new Label();
SuspendLayout();
//
// button1
//
button1.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button1.Location = new Point(22, 137);
button1.Margin = new Padding(5, 6, 5, 6);
button1.Name = "button1";
button1.Size = new Size(67, 77);
button1.TabIndex = 0;
button1.Text = "7";
button1.UseVisualStyleBackColor = true;
button1.Click += button15_Click;
//
// button2
//
button2.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button2.Location = new Point(107, 137);
button2.Margin = new Padding(5, 6, 5, 6);
button2.Name = "button2";
button2.Size = new Size(67, 77);
button2.TabIndex = 1;
button2.Text = "8";
button2.UseVisualStyleBackColor = true;
button2.Click += button15_Click;
//
// button3
//
button3.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button3.Location = new Point(277, 137);
button3.Margin = new Padding(5, 6, 5, 6);
button3.Name = "button3";
button3.Size = new Size(67, 77);
button3.TabIndex = 2;
button3.Text = "/";
button3.UseVisualStyleBackColor = true;
button3.Click += Operator_click_Event;
//
// button4
//
button4.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button4.Location = new Point(192, 137);
button4.Margin = new Padding(5, 6, 5, 6);
button4.Name = "button4";
button4.Size = new Size(67, 77);
button4.TabIndex = 3;
button4.Text = "9";
button4.UseVisualStyleBackColor = true;
button4.Click += button15_Click;
//
// button5
//
button5.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button5.Location = new Point(362, 137);
button5.Margin = new Padding(5, 6, 5, 6);
button5.Name = "button5";
button5.Size = new Size(67, 0);
button5.TabIndex = 4;
button5.Text = "CE";
button5.UseVisualStyleBackColor = true;
button5.Click += button5_Click;
//
// button6
//
button6.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button6.Location = new Point(362, 135);
button6.Margin = new Padding(5, 6, 5, 6);
button6.Name = "button6";
button6.Size = new Size(75, 173);
button6.TabIndex = 9;
button6.Text = "C";
button6.UseVisualStyleBackColor = true;
button6.Click += button6_Click;
//
// button7
//
button7.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button7.Location = new Point(192, 235);
button7.Margin = new Padding(5, 6, 5, 6);
button7.Name = "button7";
button7.Size = new Size(67, 77);
button7.TabIndex = 8;
button7.Text = "6";
button7.UseVisualStyleBackColor = true;
button7.Click += button15_Click;
//
// button8
//
button8.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button8.Location = new Point(277, 235);
button8.Margin = new Padding(5, 6, 5, 6);
button8.Name = "button8";
button8.Size = new Size(67, 77);
button8.TabIndex = 7;
button8.Text = "*";
button8.UseVisualStyleBackColor = true;
button8.Click += Operator_click_Event;
//
// button9
//
button9.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button9.Location = new Point(107, 235);
button9.Margin = new Padding(5, 6, 5, 6);
button9.Name = "button9";
button9.Size = new Size(67, 77);
button9.TabIndex = 6;
button9.Text = "5";
button9.UseVisualStyleBackColor = true;
button9.Click += button15_Click;
//
// button10
//
button10.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button10.Location = new Point(22, 235);
button10.Margin = new Padding(5, 6, 5, 6);
button10.Name = "button10";
button10.Size = new Size(67, 77);
button10.TabIndex = 5;
button10.Text = "4";
button10.UseVisualStyleBackColor = true;
button10.Click += button15_Click;
//
// button12
//
button12.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button12.Location = new Point(192, 333);
button12.Margin = new Padding(5, 6, 5, 6);
button12.Name = "button12";
button12.Size = new Size(67, 77);
button12.TabIndex = 13;
button12.Text = "3";
button12.UseVisualStyleBackColor = true;
button12.Click += button15_Click;
//
// button13
//
button13.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button13.Location = new Point(277, 333);
button13.Margin = new Padding(5, 6, 5, 6);
button13.Name = "button13";
button13.Size = new Size(67, 77);
button13.TabIndex = 12;
button13.Text = "-";
button13.UseVisualStyleBackColor = true;
button13.Click += Operator_click_Event;
//
// button14
//
button14.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button14.Location = new Point(107, 333);
button14.Margin = new Padding(5, 6, 5, 6);
button14.Name = "button14";
button14.Size = new Size(67, 77);
button14.TabIndex = 11;
button14.Text = "2";
button14.UseVisualStyleBackColor = true;
button14.Click += button15_Click;
//
// button15
//
button15.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button15.Location = new Point(22, 333);
button15.Margin = new Padding(5, 6, 5, 6);
button15.Name = "button15";
button15.Size = new Size(67, 77);
button15.TabIndex = 10;
button15.Text = "1";
button15.UseVisualStyleBackColor = true;
button15.Click += button15_Click;
//
// button20
//
button20.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button20.Location = new Point(22, 431);
button20.Margin = new Padding(5, 6, 5, 6);
button20.Name = "button20";
button20.Size = new Size(160, 87);
button20.TabIndex = 15;
button20.Text = "0";
button20.UseVisualStyleBackColor = true;
button20.Click += button15_Click;
//
// button18
//
button18.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button18.Location = new Point(277, 431);
button18.Margin = new Padding(5, 6, 5, 6);
button18.Name = "button18";
button18.Size = new Size(67, 77);
button18.TabIndex = 17;
button18.Text = "+";
button18.UseVisualStyleBackColor = true;
button18.Click += Operator_click_Event;
//
// button16
//
button16.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button16.Location = new Point(362, 333);
button16.Margin = new Padding(5, 6, 5, 6);
button16.Name = "button16";
button16.Size = new Size(75, 185);
button16.TabIndex = 19;
button16.Text = "=";
button16.UseVisualStyleBackColor = true;
button16.Click += button16_Click;
//
// button17
//
button17.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point);
button17.Location = new Point(192, 431);
button17.Margin = new Padding(5, 6, 5, 6);
button17.Name = "button17";
button17.Size = new Size(67, 77);
button17.TabIndex = 18;
button17.Text = ".";
button17.UseVisualStyleBackColor = true;
button17.Click += button15_Click;
//
// textBox_Result
//
textBox_Result.Font = new Font("Microsoft Sans Serif", 18F, FontStyle.Regular, GraphicsUnit.Point);
textBox_Result.Location = new Point(22, 63);
textBox_Result.Margin = new Padding(5, 6, 5, 6);
textBox_Result.Name = "textBox_Result";
textBox_Result.Size = new Size(412, 48);
textBox_Result.TabIndex = 20;
textBox_Result.Text = "0";
textBox_Result.TextAlign = HorizontalAlignment.Right;
//
// label_Show_Op
//
label_Show_Op.AutoSize = true;
label_Show_Op.Font = new Font("Microsoft Sans Serif", 9F, FontStyle.Bold, GraphicsUnit.Point);
label_Show_Op.Location = new Point(22, 27);
label_Show_Op.Margin = new Padding(5, 0, 5, 0);
label_Show_Op.Name = "label_Show_Op";
label_Show_Op.Size = new Size(0, 22);
label_Show_Op.TabIndex = 21;
label_Show_Op.Click += label1_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(454, 546);
Controls.Add(label_Show_Op);
Controls.Add(textBox_Result);
Controls.Add(button16);
Controls.Add(button17);
Controls.Add(button18);
Controls.Add(button20);
Controls.Add(button12);
Controls.Add(button13);
Controls.Add(button14);
Controls.Add(button15);
Controls.Add(button6);
Controls.Add(button7);
Controls.Add(button8);
Controls.Add(button9);
Controls.Add(button10);
Controls.Add(button5);
Controls.Add(button4);
Controls.Add(button3);
Controls.Add(button2);
Controls.Add(button1);
FormBorderStyle = FormBorderStyle.FixedDialog;
Margin = new Padding(5, 6, 5, 6);
MaximizeBox = false;
Name = "Form1";
SizeGripStyle = SizeGripStyle.Show;
StartPosition = FormStartPosition.CenterScreen;
Text = "Calculator";
Load += Form1_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button button10;
private Button button12;
private Button button13;
private Button button14;
private Button button15;
private Button button20;
private Button button18;
private Button button16;
private Button button17;
private TextBox textBox_Result;
private Label label_Show_Op;
}
}
Comments
Post a Comment