Timer merupakan sebuah tool yang disediakan oleh visual c++
sebagai tool pengolah data yang berkaitan dengan waktu. Dalam menggunakan
timer, kita harus mengaktifkannya terlebih dahulu dengan mengubah nilai
properties “Enabled” nya menjadi “true”.
Selain mengatur properties enable nya, yang harus kita
perhatikan jika kita menggunakan timer adalah pengaturan properties “interval”
nya. Interval ini digunakan untuk mendefinisikan tunda waktu timer untuk
menjalankan program yang ada didalam fungsi “timer_tick”. Misalnya kita atur
interval timer adalah 1000 (nilai 1000 tersebut memiliki satuan waktu milidetik
atau 1 detik), kemudian didalam fungsi timer_tick (fungsi yang akan kita jumpai
jika kita double klik pada timer) kita buat sebuah program untuk menampilkan
sebuah tulisan pada label, maka program menampilkan tulisan pada label tersebut
akan dijalankan setiap satu detik sekali.
Untuk lebih jelasnya,
dibawah ini akan dijelaskan mengenai beberapa cara menggunakan timer pada
visual c++ (GUI).
timer1->Interval = 1000;
Sintak diatas berfungsi untuk mendefinisikan nilai interval
dari timer. Nilai interval tersebut dapat kita atur sesuai dengan keinginan.
timer1->Enabled = false;
Sintak
diatas berfungsi untuk mengatur enable dari timer. Enable disini artinya adalah
mengaktifkan atau menonaktifkan timer yang akan kita atur. Jika ingin
mengaktifkan maka nilai enabled nya adalah “true” sedangkan untuk menonaktifkan adalah “false”.
#pragma once
namespace timer {
using namespace
System;
using namespace
System::ComponentModel;
using namespace
System::Collections;
using namespace
System::Windows::Forms;
using namespace
System::Data;
using namespace
System::Drawing;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public
System::Windows::Forms::Form
{
int a, jam, menit, detik, jam1, men1, det1;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Timer^ timer1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::GroupBox^ groupBox1;
private: System::Windows::Forms::RadioButton^ radioButton1;
private: System::Windows::Forms::RadioButton^ radioButton2;
private: System::Windows::Forms::GroupBox^ groupBox2;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label7;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::Label^ label6;
private: System::Windows::Forms::Label^ label8;
private: System::Windows::Forms::RadioButton^ radioButton3;
private: System::Windows::Forms::GroupBox^ groupBox3;
private: System::Windows::Forms::TextBox^ textBox5;
private: System::Windows::Forms::Label^ label13;
private: System::Windows::Forms::TextBox^ textBox4;
private: System::Windows::Forms::Label^ label12;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::Label^ label11;
private: System::Windows::Forms::Label^ label10;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Label^ label9;
private: System::Windows::Forms::Button^ button3;
protected:
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma 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>
void InitializeComponent(void)
{
this->components = (gcnew
System::ComponentModel::Container());
this->timer1 = (gcnew
System::Windows::Forms::Timer(this->components));
this->button1 = (gcnew
System::Windows::Forms::Button());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->label3 = (gcnew
System::Windows::Forms::Label());
this->groupBox1 = (gcnew
System::Windows::Forms::GroupBox());
this->label8 = (gcnew
System::Windows::Forms::Label());
this->radioButton1 = (gcnew
System::Windows::Forms::RadioButton());
this->radioButton2 = (gcnew
System::Windows::Forms::RadioButton());
this->groupBox2 = (gcnew
System::Windows::Forms::GroupBox());
this->textBox1 = (gcnew
System::Windows::Forms::TextBox());
this->label7 = (gcnew
System::Windows::Forms::Label());
this->button2 = (gcnew
System::Windows::Forms::Button());
this->label4 = (gcnew
System::Windows::Forms::Label());
this->label5 = (gcnew
System::Windows::Forms::Label());
this->label6 = (gcnew
System::Windows::Forms::Label());
this->radioButton3 = (gcnew
System::Windows::Forms::RadioButton());
this->groupBox3 = (gcnew
System::Windows::Forms::GroupBox());
this->textBox5 = (gcnew
System::Windows::Forms::TextBox());
this->label13 = (gcnew
System::Windows::Forms::Label());
this->textBox4 = (gcnew
System::Windows::Forms::TextBox());
this->label12 = (gcnew
System::Windows::Forms::Label());
this->textBox3 = (gcnew
System::Windows::Forms::TextBox());
this->label11 = (gcnew
System::Windows::Forms::Label());
this->label10 = (gcnew
System::Windows::Forms::Label());
this->textBox2 = (gcnew
System::Windows::Forms::TextBox());
this->label9 = (gcnew
System::Windows::Forms::Label());
this->button3 = (gcnew
System::Windows::Forms::Button());
this->groupBox1->SuspendLayout();
this->groupBox2->SuspendLayout();
this->groupBox3->SuspendLayout();
this->SuspendLayout();
//
// timer1
//
this->timer1->Tick += gcnew
System::EventHandler(this,
&Form1::timer1_Tick);
//
// button1
//
this->button1->Location =
System::Drawing::Point(6, 19);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(227, 39);
this->button1->TabIndex = 0;
this->button1->Text = L"aktifkan
timer";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location =
System::Drawing::Point(122, 128);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(18,
19);
this->label1->TabIndex = 2;
this->label1->Text = L"0";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label2->Location =
System::Drawing::Point(51, 128);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(65,
19);
this->label2->TabIndex = 3;
this->label2->Text = L"Counter";
//
// label3
//
this->label3->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label3->Location =
System::Drawing::Point(6, 61);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(227,
19);
this->label3->TabIndex = 4;
this->label3->Text = L"Timer tidak aktif";
this->label3->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// groupBox1
//
this->groupBox1->Controls->Add(this->label8);
this->groupBox1->Controls->Add(this->button1);
this->groupBox1->Controls->Add(this->label1);
this->groupBox1->Controls->Add(this->label2);
this->groupBox1->Controls->Add(this->label3);
this->groupBox1->Location =
System::Drawing::Point(12, 51);
this->groupBox1->Name = L"groupBox1";
this->groupBox1->Size =
System::Drawing::Size(239, 159);
this->groupBox1->TabIndex = 5;
this->groupBox1->TabStop = false;
this->groupBox1->Text = L"Mengaktifkan timer dari button";
//
// label8
//
this->label8->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label8->Location =
System::Drawing::Point(6, 95);
this->label8->Name = L"label8";
this->label8->Size = System::Drawing::Size(227,
19);
this->label8->TabIndex = 5;
this->label8->Text = L"Interval
timer : 1000";
this->label8->TextAlign =
System::Drawing::ContentAlignment::MiddleCenter;
//
// radioButton1
//
this->radioButton1->AutoSize = true;
this->radioButton1->Checked = true;
this->radioButton1->Location = System::Drawing::Point(17,
14);
this->radioButton1->Name = L"radioButton1";
this->radioButton1->Size =
System::Drawing::Size(168, 17);
this->radioButton1->TabIndex = 6;
this->radioButton1->TabStop = true;
this->radioButton1->Text = L"Mengaktifkan timer dari button";
this->radioButton1->UseVisualStyleBackColor = true;
this->radioButton1->CheckedChanged += gcnew System::EventHandler(this,
&Form1::radioButton1_CheckedChanged);
//
// radioButton2
//
this->radioButton2->AutoSize = true;
this->radioButton2->Location =
System::Drawing::Point(259, 14);
this->radioButton2->Name = L"radioButton2";
this->radioButton2->Size =
System::Drawing::Size(120, 17);
this->radioButton2->TabIndex = 8;
this->radioButton2->Text = L"Setting interval timer";
this->radioButton2->UseVisualStyleBackColor = true;
this->radioButton2->CheckedChanged += gcnew System::EventHandler(this,
&Form1::radioButton2_CheckedChanged);
//
// groupBox2
//
this->groupBox2->Controls->Add(this->textBox1);
this->groupBox2->Controls->Add(this->label7);
this->groupBox2->Controls->Add(this->button2);
this->groupBox2->Controls->Add(this->label4);
this->groupBox2->Controls->Add(this->label5);
this->groupBox2->Controls->Add(this->label6);
this->groupBox2->Enabled = false;
this->groupBox2->Location =
System::Drawing::Point(259, 51);
this->groupBox2->Name = L"groupBox2";
this->groupBox2->Size =
System::Drawing::Size(207, 159);
this->groupBox2->TabIndex = 7;
this->groupBox2->TabStop = false;
this->groupBox2->Text = L"Mengatur interval timer";
//
// textBox1
//
this->textBox1->Location =
System::Drawing::Point(117, 29);
this->textBox1->Name = L"textBox1";
this->textBox1->Size =
System::Drawing::Size(84, 20);
this->textBox1->TabIndex = 9;
this->textBox1->Text = L"1";
//
// label7
//
this->label7->AutoSize = true;
this->label7->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label7->Location =
System::Drawing::Point(6, 27);
this->label7->Name = L"label7";
this->label7->Size = System::Drawing::Size(105,
19);
this->label7->TabIndex = 5;
this->label7->Text = L"Interval
timer";
//
// button2
//
this->button2->Location =
System::Drawing::Point(6, 61);
this->button2->Name = L"button2";
this->button2->Size =
System::Drawing::Size(195, 39);
this->button2->TabIndex = 0;
this->button2->Text = L"aktifkan
timer";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this,
&Form1::button2_Click);
//
// label4
//
this->label4->AutoSize = true;
this->label4->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label4->Location =
System::Drawing::Point(113, 137);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(18,
19);
this->label4->TabIndex = 2;
this->label4->Text = L"0";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label5->Location =
System::Drawing::Point(29, 137);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(65,
19);
this->label5->TabIndex = 3;
this->label5->Text = L"Counter";
//
// label6
//
this->label6->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label6->Location =
System::Drawing::Point(6, 103);
this->label6->Name = L"label6";
this->label6->Size = System::Drawing::Size(183,
19);
this->label6->TabIndex = 4;
this->label6->Text = L"Timer tidak aktif";
this->label6->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// radioButton3
//
this->radioButton3->AutoSize = true;
this->radioButton3->Location =
System::Drawing::Point(472, 14);
this->radioButton3->Name = L"radioButton3";
this->radioButton3->Size =
System::Drawing::Size(190, 17);
this->radioButton3->TabIndex = 10;
this->radioButton3->Text = L"Aktifkan lampu sesuai setingan jam";
this->radioButton3->UseVisualStyleBackColor = true;
this->radioButton3->CheckedChanged += gcnew System::EventHandler(this,
&Form1::radioButton3_CheckedChanged);
//
// groupBox3
//
this->groupBox3->Controls->Add(this->textBox5);
this->groupBox3->Controls->Add(this->label13);
this->groupBox3->Controls->Add(this->textBox4);
this->groupBox3->Controls->Add(this->label12);
this->groupBox3->Controls->Add(this->textBox3);
this->groupBox3->Controls->Add(this->label11);
this->groupBox3->Controls->Add(this->label10);
this->groupBox3->Controls->Add(this->textBox2);
this->groupBox3->Controls->Add(this->label9);
this->groupBox3->Controls->Add(this->button3);
this->groupBox3->Enabled = false;
this->groupBox3->Location =
System::Drawing::Point(472, 51);
this->groupBox3->Name = L"groupBox3";
this->groupBox3->Size = System::Drawing::Size(260,
159);
this->groupBox3->TabIndex = 9;
this->groupBox3->TabStop = false;
this->groupBox3->Text = L"Setting timer";
//
// textBox5
//
this->textBox5->Location =
System::Drawing::Point(220, 60);
this->textBox5->Name = L"textBox5";
this->textBox5->Size =
System::Drawing::Size(34, 20);
this->textBox5->TabIndex = 16;
this->textBox5->Text = L"1";
//
// label13
//
this->label13->AutoSize = true;
this->label13->Location =
System::Drawing::Point(182, 63);
this->label13->Name = L"label13";
this->label13->Size = System::Drawing::Size(32,
13);
this->label13->TabIndex = 15;
this->label13->Text = L"Detik";
//
// textBox4
//
this->textBox4->Location =
System::Drawing::Point(136, 60);
this->textBox4->Name
= L"textBox4";
this->textBox4->Size =
System::Drawing::Size(34, 20);
this->textBox4->TabIndex = 14;
this->textBox4->Text = L"0";
//
// label12
//
this->label12->AutoSize = true;
this->label12->Location = System::Drawing::Point(97,
63);
this->label12->Name = L"label12";
this->label12->Size = System::Drawing::Size(33,
13);
this->label12->TabIndex = 13;
this->label12->Text = L"Menit";
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(43,
60);
this->textBox3->Name = L"textBox3";
this->textBox3->Size =
System::Drawing::Size(34, 20);
this->textBox3->TabIndex = 12;
this->textBox3->Text = L"0";
//
// label11
//
this->label11->AutoSize = true;
this->label11->Location =
System::Drawing::Point(14, 63);
this->label11->Name = L"label11";
this->label11->Size = System::Drawing::Size(26,
13);
this->label11->TabIndex = 11;
this->label11->Text = L"Jam";
//
// label10
//
this->label10->BackColor =
System::Drawing::Color::Black;
this->label10->Location =
System::Drawing::Point(114, 129);
this->label10->Name = L"label10";
this->label10->Size = System::Drawing::Size(48,
24);
this->label10->TabIndex = 10;
//
// textBox2
//
this->textBox2->Enabled = false;
this->textBox2->Location =
System::Drawing::Point(117, 29);
this->textBox2->Name = L"textBox2";
this->textBox2->Size =
System::Drawing::Size(137, 20);
this->textBox2->TabIndex = 9;
this->textBox2->Text = L"1000";
//
// label9
//
this->label9->AutoSize = true;
this->label9->Font = (gcnew
System::Drawing::Font(L"Tahoma",
12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label9->Location =
System::Drawing::Point(6, 27);
this->label9->Name = L"label9";
this->label9->Size = System::Drawing::Size(105,
19);
this->label9->TabIndex = 5;
this->label9->Text = L"Interval
timer";
//
// button3
//
this->button3->Location
= System::Drawing::Point(6, 87);
this->button3->Name = L"button3";
this->button3->Size =
System::Drawing::Size(248, 39);
this->button3->TabIndex = 0;
this->button3->Text = L"aktifkan
timer";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this,
&Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions =
System::Drawing::SizeF(6, 13);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(744,
222);
this->Controls->Add(this->radioButton3);
this->Controls->Add(this->groupBox3);
this->Controls->Add(this->radioButton2);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->radioButton1);
this->Controls->Add(this->groupBox1);
this->Name = L"Form1";
this->Text = L"Timer
by Rifqi";
this->groupBox1->ResumeLayout(false);
this->groupBox1->PerformLayout();
this->groupBox2->ResumeLayout(false);
this->groupBox2->PerformLayout();
this->groupBox3->ResumeLayout(false);
this->groupBox3->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void
timer1_Tick(System::Object^ sender,
System::EventArgs^ e) {
if(radioButton1->Checked){
a++;
label1->Text=a.ToString();
}
else if(radioButton2->Checked){
a++;
label4->Text=a.ToString();
}
else if(radioButton3->Checked){
detik++;
if(detik==60){
menit++;
detik=0;
}
if(menit==60){
jam++;
menit=0;
}
if(jam==jam1
&& menit==men1 && detik==det1){
timer1->Enabled=false;
label10->BackColor =
System::Drawing::Color::Red;
button3->Text="Reset";
}
}
}
private: System::Void
radioButton1_CheckedChanged(System::Object^
sender, System::EventArgs^ e) {
groupBox1->Enabled=true;
groupBox2->Enabled=false;
a=0;
label4->Text="0";
label6->Text="Timer
Tidak Aktif";
button2->Text="aktifkan
timer";
button3->Text="aktifkan
timer";
label10->BackColor =
System::Drawing::Color::Black;
}
private: System::Void
radioButton2_CheckedChanged(System::Object^
sender, System::EventArgs^ e) {
groupBox1->Enabled=false;
groupBox2->Enabled=true;
timer1->Enabled = false;
a=0;
label1->Text="0";
label3->Text="Timer
Tidak Aktif";
button1->Text="aktifkan
timer";
button3->Text="aktifkan
timer";
label10->BackColor =
System::Drawing::Color::Black;
}
private: System::Void
radioButton3_CheckedChanged(System::Object^
sender, System::EventArgs^ e) {
groupBox1->Enabled=false;//mengubah enabled groupBox
groupBox2->Enabled=false;
groupBox3->Enabled=true;
timer1->Enabled = false;
a=0;
detik=0;
menit=0;
jam=0;
label1->Text="0";
label4->Text="0";
label3->Text="Timer
Tidak Aktif";
label6->Text="Timer
Tidak Aktif";
button1->Text="aktifkan
timer";
button2->Text="aktifkan
timer";
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if(button1->Text=="aktifkan timer"){
timer1->Interval = 1000;
timer1->Enabled = true;
a=0;
label1->Text="0";
label3->Text="Timer
Aktif";
button1->Text="non
aktifkan timer";
}
else{
timer1->Enabled = false;
a=0;
label3->Text="Timer
Tidak Aktif";
button1->Text="aktifkan
timer";
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
if(textBox1->Text==""){
MessageBox::Show("Masukkan
nilai interval pada textBox terlebih dahulu!!!");
}
else{
if(button2->Text=="aktifkan timer"){
timer1->Interval = System::Int32::Parse(textBox1->Text);//ambil data dari textBox
timer1->Enabled = true;//mengaktifkan timer
a=0;//inisialisasi
nilai a
label4->Text="0";//mengubah text pada label
label6->Text="Timer
Aktif";//mengubah text pada label
button2->Text="non
aktifkan timer";//mengubah text pada
button
}
else{
timer1->Enabled = false; //me-non-aktifkan
timer
a=0; //inisialisasi nilai a
label6->Text="Timer
Tidak Aktif";//mengubah text pada label
button2->Text="aktifkan
timer";//mengubah text pada button
label10->BackColor =
System::Drawing::Color::Black;//mengubah backColor
label
}
}
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
if(textBox3->Text=="" || textBox4->Text=="" || textBox5->Text==""){
MessageBox::Show("Lengkapi
data pada textBox terlebih dahulu!!!");//messageBox
}
else{
if(button3->Text=="aktifkan timer"){
timer1->Interval =
System::Int32::Parse(textBox2->Text);//ambil nilai
dari textBox
timer1->Enabled = true;//mengaktifkan timer
//inisialisasi nilai
variabel
a=0;
detik=0;
menit=0;
jam=0;
//ambil nilai dari
textBox
jam1=System::Int32::Parse(textBox3->Text);
men1=System::Int32::Parse(textBox4->Text);
det1=System::Int32::Parse(textBox5->Text);
button3->Text="Stop
timer";//mengubah text pada button
}
else if(button3->Text=="Reset"){
//inisialisasi nilai
variabel
a=0;
detik=0;
menit=0;
jam=0;
button3->Text="aktifkan
timer";//mengubah text pada button
label10->BackColor =
System::Drawing::Color::Black;//setting backColor
label
}
else{
timer1->Enabled = false;//me-non-aktifkan timer
//inisialisasi nilai variabel
a=0;
detik=0;
menit=0;
jam=0;
button3->Text="aktifkan
timer";//mengubah text pada button
}
}
}
};
}
Tampilan aplikasi
:
0 komentar:
Posting Komentar