TUGAS 1 PBKK : Membuat Aplikasi Desktop Sederhana
Tugas 1
5025201003 - Rahmat Faris Akbar
PBKK (D)
Pada tugas kali ini, kita akan mencoba membuat aplikasi dekstop sederhana menggunakan framework .NET dan bahasa pemrograman C#. Aplikasi yang akan kita buat adalah kalkulator sederhana yang kita namakan sebagai SimpCalc. Kalkulator ini nantinya akan memiliki beberapa fitur operasi matematika sederhana yaitu tambah, kurang, kali, dan bagi. Selain itu, SimpCalc juga memliki fungsi CE (Clelar Entry) dan Clear untuk menghapus inputan. Pada aplikasi SimpCalc ini hanya bisa memproses satu operasi saja dalam sekali inputan agar mempermudah proses pembuatan.
Langkah Pembuatan :
- Membuat file baru pada IDE Visual Studio dengan menggunakan template project C# Windows Form Application
- Membuat antarmuka aplikasi yang akan dibuat (saat ini kita membuat kalkulator sederhana)
- Tool yang digunakan adalah buttons, textbox, dan label
- Edit properties dan event setiap komponen sesuai kebutuhan
-
Membuat program matematis untuk setiap tombolnya. Di sini terdapat
beberapa fungsi yaitu:
- button_click : pada tombol angka dan tanda koma
- operator_click : pada tombol operator (*, /, -, +)
- button_CE : pada tombol CE / Clear Entry
- button_C : pada tombol C / Clear
- button_R : pada tombol "="
Source Code :
(Link Repository)
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 Simple_Calculator
{
public partial class Form1 : Form
{
Double resultValue = 0;
String operationPerformed = "";
bool isOperationPerformed = false;
public Form1()
{
InitializeComponent();
}
private void button_click(object sender, EventArgs e)
{
if ((textBox_Result.Text == "0") || (isOperationPerformed))
textBox_Result.Clear();
isOperationPerformed = false;
Button button = (Button)sender;
if (button.Text == ".")
{
if (!textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;
}
else
textBox_Result.Text = textBox_Result.Text + button.Text;
}
private void operator_click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultValue != 0)
{
button15.PerformClick();
operationPerformed = button.Text;
labelCurrentOperation.Text = resultValue + " " + operationPerformed;
isOperationPerformed = true;
}
else
{
operationPerformed = button.Text;
resultValue = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = resultValue + " " + operationPerformed;
isOperationPerformed = true;
}
}
private void buttonCE_Click(object sender, EventArgs e)
{
textBox_Result.Text = "0";
}
private void buttonC_Click(object sender, EventArgs e)
{
textBox_Result.Text = "0";
resultValue = 0;
}
private void buttonR_Click(object sender, EventArgs e)
{
switch (operationPerformed)
{
case "+":
textBox_Result.Text = (resultValue + Double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString();
break;
case "*":
textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString();
break;
case "/":
textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
break;
default:
break;
}
resultValue = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = "";
}
}
}
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 Simple_Calculator | |
{ | |
public partial class Form1 : Form | |
{ | |
Double resultValue = 0; | |
String operationPerformed = ""; | |
bool isOperationPerformed = false; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void button_click(object sender, EventArgs e) | |
{ | |
if ((textBox_Result.Text == "0") || (isOperationPerformed)) | |
textBox_Result.Clear(); | |
isOperationPerformed = false; | |
Button button = (Button)sender; | |
if (button.Text == ".") | |
{ | |
if (!textBox_Result.Text.Contains(".")) | |
textBox_Result.Text = textBox_Result.Text + button.Text; | |
} | |
else | |
textBox_Result.Text = textBox_Result.Text + button.Text; | |
} | |
private void operator_click(object sender, EventArgs e) | |
{ | |
Button button = (Button)sender; | |
if (resultValue != 0) | |
{ | |
button15.PerformClick(); | |
operationPerformed = button.Text; | |
labelCurrentOperation.Text = resultValue + " " + operationPerformed; | |
isOperationPerformed = true; | |
} | |
else | |
{ | |
operationPerformed = button.Text; | |
resultValue = Double.Parse(textBox_Result.Text); | |
labelCurrentOperation.Text = resultValue + " " + operationPerformed; | |
isOperationPerformed = true; | |
} | |
} | |
private void buttonCE_Click(object sender, EventArgs e) | |
{ | |
textBox_Result.Text = "0"; | |
} | |
private void buttonC_Click(object sender, EventArgs e) | |
{ | |
textBox_Result.Text = "0"; | |
resultValue = 0; | |
} | |
private void buttonR_Click(object sender, EventArgs e) | |
{ | |
switch (operationPerformed) | |
{ | |
case "+": | |
textBox_Result.Text = (resultValue + Double.Parse(textBox_Result.Text)).ToString(); | |
break; | |
case "-": | |
textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString(); | |
break; | |
case "*": | |
textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString(); | |
break; | |
case "/": | |
textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString(); | |
break; | |
default: | |
break; | |
} | |
resultValue = Double.Parse(textBox_Result.Text); | |
labelCurrentOperation.Text = ""; | |
} | |
} | |
} |
Tampilan Aplikasi SimpCalc :
Referensi :
============== CONTOH APLIKASI 2 ==============
Selain Kalkulator Sederhana, kita juga dapat membuat aplikasi
Webccam Capture menggunakan bahasa C#. Namun, untuk
aplikasi ini membutuhkan Framework dari luar yaitu AForge.Net untuk melakukan pemrosesan gambar pada C#. Fitur dari aplikasi ini yaitu memilih camera yang digunakan, menyalakan camera, menangkap gambar dari rekaman kamera, melakukan save terhadap gambar yang telah ditangkap, serta menutup aplikasi.
Source Code : (Link Repository)
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;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;
namespace WebcamCaptureApp
{
public partial class Form1 : Form
{
private FilterInfoCollection captureDevice;
private VideoCaptureDevice videoSource;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
captureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach(FilterInfo deviceList in captureDevice)
{
comboBoxWebcamList.Items.Add(deviceList.Name);
}
comboBoxWebcamList.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
}
private void buttonStart_Click(object sender, EventArgs e)
{
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource.WaitForStop();
pictureBox1.Image = null;
pictureBox1.Invalidate();
}
videoSource = new VideoCaptureDevice(captureDevice[comboBoxWebcamList.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);
videoSource.Start();
}
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void buttonCapture_Click(object sender, EventArgs e)
{
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
}
private void buttonSaveImage_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "Save Image As";
saveFileDialog.Filter = "Image files (*.jpg, *.png) | *.jpg, *.png";
ImageFormat imageFormat = ImageFormat.Png;
if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
string ext = System.IO.Path.GetExtension(saveFileDialog.FileName);
switch (ext)
{
case ".jpg":
imageFormat = ImageFormat.Jpeg;
break;
case ".png":
imageFormat = ImageFormat.Png;
break;
}
pictureBox2.Image.Save(saveFileDialog.FileName, imageFormat);
}
}
private void buttonExit_Click(object sender, EventArgs e)
{
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource.WaitForStop();
pictureBox1.Image = null;
pictureBox1.Invalidate();
pictureBox2.Image = null;
pictureBox2.Invalidate();
}
Application.Exit(null);
}
}
}
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; | |
using AForge; | |
using AForge.Video; | |
using AForge.Video.DirectShow; | |
using System.Drawing.Imaging; | |
namespace WebcamCaptureApp | |
{ | |
public partial class Form1 : Form | |
{ | |
private FilterInfoCollection captureDevice; | |
private VideoCaptureDevice videoSource; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
captureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice); | |
foreach(FilterInfo deviceList in captureDevice) | |
{ | |
comboBoxWebcamList.Items.Add(deviceList.Name); | |
} | |
comboBoxWebcamList.SelectedIndex = 0; | |
videoSource = new VideoCaptureDevice(); | |
} | |
private void buttonStart_Click(object sender, EventArgs e) | |
{ | |
if (videoSource.IsRunning) | |
{ | |
videoSource.SignalToStop(); | |
videoSource.WaitForStop(); | |
pictureBox1.Image = null; | |
pictureBox1.Invalidate(); | |
} | |
videoSource = new VideoCaptureDevice(captureDevice[comboBoxWebcamList.SelectedIndex].MonikerString); | |
videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame); | |
videoSource.Start(); | |
} | |
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs) | |
{ | |
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone(); | |
} | |
private void buttonCapture_Click(object sender, EventArgs e) | |
{ | |
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone(); | |
} | |
private void buttonSaveImage_Click(object sender, EventArgs e) | |
{ | |
SaveFileDialog saveFileDialog = new SaveFileDialog(); | |
saveFileDialog.Title = "Save Image As"; | |
saveFileDialog.Filter = "Image files (*.jpg, *.png) | *.jpg, *.png"; | |
ImageFormat imageFormat = ImageFormat.Png; | |
if(saveFileDialog.ShowDialog() == DialogResult.OK) | |
{ | |
string ext = System.IO.Path.GetExtension(saveFileDialog.FileName); | |
switch (ext) | |
{ | |
case ".jpg": | |
imageFormat = ImageFormat.Jpeg; | |
break; | |
case ".png": | |
imageFormat = ImageFormat.Png; | |
break; | |
} | |
pictureBox2.Image.Save(saveFileDialog.FileName, imageFormat); | |
} | |
} | |
private void buttonExit_Click(object sender, EventArgs e) | |
{ | |
if (videoSource.IsRunning) | |
{ | |
videoSource.SignalToStop(); | |
videoSource.WaitForStop(); | |
pictureBox1.Image = null; | |
pictureBox1.Invalidate(); | |
pictureBox2.Image = null; | |
pictureBox2.Invalidate(); | |
} | |
Application.Exit(null); | |
} | |
} | |
} |
Comments
Post a Comment