PBKK - Tugas 2
Simple Application
A. Permulaan
1. Install Visual Stuido Microsoft di
2. Buat Project baru
1. Hello World
- Code
using Gtk;
using System;
class Hello
{
static void Main()
{
Application.Init();
Window myWin = new Window("Hello World");
myWin.Resize(200, 200);
Label myLabel = new Label();
myLabel.Text = "Hello World!!!!";
myWin.Add(myLabel);
myWin.ShowAll();
Application.Run();
}
}
- Hasil
2. Calculator
- Code
using Gtk;
using System;
public class Calculator
{
static decimal result;
static decimal a;
static decimal b;
static string stringA;
static string stringB;
static int modify;
static bool solved;
// The following methods should be subscribed to the events fired by all the buttons
static void resultAction()
{
Program.CalcScreen.Text = result.ToString();
solved = true; modify = 0; stringA = ""; stringB = "";
}
//Multiplication methods
static void doMultiplication(object obj, EventArgs e)
{
result = a * b;
resultAction();
}
static EventHandler Multiplication = new EventHandler(doMultiplication);
static void setMultiplication(object obj, EventArgs e)
{
modify = 1;
Program.Equal.Clicked += Multiplication;
Program.CalcScreen.Text += " X ";
if (solved)
{
a = result;
}
solved = false;
}
public static EventHandler TimesHandler = new EventHandler(Calculator.setMultiplication);
//Addition methods
static void doAddition(object obj, EventArgs e)
{
result = a + b;
resultAction();
}
static EventHandler Addition = new EventHandler(doAddition);
static void setAddition(object obj, EventArgs e)
{
modify = 1;
Program.Equal.Clicked += Addition;
Program.CalcScreen.Text += " + ";
if (solved)
{
a = result;
}
solved = false;
}
public static EventHandler PlusHandler = new EventHandler(Calculator.setAddition);
//Division methods
static void doDivision(object obj, EventArgs e)
{
result = a / b;
resultAction();
}
static EventHandler Division = new EventHandler(doDivision);
static void setDivision(object obj, EventArgs e)
{
modify = 1;
Program.Equal.Clicked += Division;
Program.CalcScreen.Text += " / ";
if (solved)
{
a = result;
}
solved = false;
}
public static EventHandler DividedHandler = new EventHandler(Calculator.setDivision);
//Subtraction methods
static void doSubtraction(object obj, EventArgs e)
{
result = a - b;
resultAction();
}
static EventHandler Subtraction = new EventHandler(doSubtraction);
static void setSubtraction(object obj, EventArgs e)
{
modify = 1;
Program.Equal.Clicked += Subtraction;
Program.CalcScreen.Text += " - ";
if (solved)
{
a = result;
}
solved = false;
}
public static EventHandler MinusHandler = new EventHandler(Calculator.setSubtraction);
//clickButton definition and subscription preparation(instantiation as a delegate)
static void clickNumButton(object obj, EventArgs e)
{
Button btn = obj as Button;
if (modify == 0)
{
stringA += btn.Label;
if (String.Compare(stringA.Substring(stringA.Length - 1), ".") == 1)
{
a = decimal.Parse(stringA);
}
//Console.WriteLine("A: " + a + " string:" + stringA);
if (!solved)
{
Program.CalcScreen.Text += btn.Label;
}
else
{
Program.CalcScreen.Text = btn.Label;
}
solved = false;
}
else
{
stringB += btn.Label;
if (String.Compare(stringB.Substring(stringB.Length - 1), ".") == 1) {
b = decimal.Parse(stringB);
}
//Console.WriteLine("B: " + b + " string:" + stringB );
if (!solved)
{
Program.CalcScreen.Text += btn.Label;
}
else
{
Program.CalcScreen.Text = btn.Label;
}
solved = false;
}
}
public static EventHandler clickNumber = new EventHandler(clickNumButton);
}
public class Program
{
static Table MyTable;
static Button One;
static Button Two;
static Button Three;
static Button Four;
static Button Five;
static Button Six;
static Button Seven;
static Button Eight;
static Button Nine;
static Button Zero;
static Button DecPoint;
static Button Plus;
static Button Minus;
static Button Times;
static Button Divided;
public static Entry CalcScreen;
public static Button Equal;
static Window MyWindow;
static void Main()
{
// Calls the static method Gtk.Application.init - initiates a gtk application
Application.Init();
// initialize the window object with an instance of MyWindow. Creates a window in which to put widgets, that the user will eventually interact with
MyWindow = new Window("GTK# Application");
MyWindow.SetDefaultSize(200, 500);
// All of the things that willl be added to the window
// Create a table, add it to the window
MyTable = new Table(7, 5, true);
MyWindow.Add(MyTable);
One = new Button("1");
MyTable.Attach(One, 0, 1, 3, 4);
One.Clicked += Calculator.clickNumber;
Two = new Button("2");
MyTable.Attach(Two, 1, 2, 3, 4);
Two.Clicked += Calculator.clickNumber;
Three = new Button("3");
MyTable.Attach(Three, 2, 3, 3, 4);
Three.Clicked += Calculator.clickNumber;
Four = new Button("4");
MyTable.Attach(Four, 0, 1, 2, 3);
Four.Clicked += Calculator.clickNumber;
Five = new Button("5");
MyTable.Attach(Five, 1, 2, 2, 3);
Five.Clicked += Calculator.clickNumber;
Six = new Button("6");
MyTable.Attach(Six, 2, 3, 2, 3);
Six.Clicked += Calculator.clickNumber;
Seven = new Button("7");
MyTable.Attach(Seven, 0, 1, 1, 2);
Seven.Clicked += Calculator.clickNumber;
Eight = new Button("8");
MyTable.Attach(Eight, 1, 2, 1, 2);
Eight.Clicked += Calculator.clickNumber;
Nine = new Button("9");
MyTable.Attach(Nine, 2, 3, 1, 2);
Nine.Clicked += Calculator.clickNumber;
Zero = new Button("0");
MyTable.Attach(Zero, 0, 1, 4, 5);
Zero.Clicked += Calculator.clickNumber;
DecPoint = new Button(".");
MyTable.Attach(DecPoint, 1, 2, 4, 5);
DecPoint.Clicked += Calculator.clickNumber;
// Equal
Equal = new Button("=");
MyTable.Attach(Equal, 3, 4, 5, 6);
// Multiplication button and subscriptions
Times = new Button("X");
MyTable.Attach(Times, 3, 4, 1, 2);
Times.Clicked += Calculator.TimesHandler;
// Addition button and subscriptions
Plus = new Button("+");
MyTable.Attach(Plus, 3, 4, 2, 3);
Plus.Clicked += Calculator.PlusHandler;
// Division button and subscriptions
Divided = new Button("/");
MyTable.Attach(Divided, 3, 4, 3, 4);
Divided.Clicked += Calculator.DividedHandler;
// Subtraction button and subscriptions
Minus = new Button("-");
MyTable.Attach(Minus, 3, 4, 4, 5);
Minus.Clicked += Calculator.MinusHandler;
// Text Fields
CalcScreen = new Entry();
MyTable.Attach(CalcScreen, 0, 1, 0, 1);
//Subscribe the window close event handler to the MyWindow.DeleteEvent event
DeleteEventHandler CloseHandler = new DeleteEventHandler(window_close);
MyWindow.DeleteEvent += CloseHandler;
MyWindow.ShowAll();
Application.Run();
}
static void window_close(object obj, DeleteEventArgs e)
{
Application.Quit();
e.RetVal = true;
}
}
- Hasil
Komentar
Posting Komentar