Minggu, 15 April 2012

textBox – Visual C++ (GUI)



textBox merupakan sebuah tool pada visual C++ untuk menampilkan data maupun sebagai sebuah tool yang mana isi/data nya dapat kita ambil menjadi sebuah input. Data pada textbox ini bertipe string, yang mana kita tidak bisa sembarangan dalam mengambil dan menampilkan data selain string ke dalam sebuah textbox. Pada postingan kali ini, akan dibahas mengenai beberapa sintak program yang dapat digunakan dalam pengolahan data yang ada pada sebuah textbox, yang mana pengolahan disini meliputi pengambilan data, konversi data, dan menampilkan data kembali ke dalam sebuah textbox.
                                                                  
String ^ a= gcnew String(txtIn->Text);  
txtOut->Text=a;     
Mengambil isi textbox “txtIn” kemudian disimpan kedalam sebuah string yang diberi nama a. Kemudian variabel a tersebut ditampilkan ke textbox “txtOut”.

x = System::Double::Parse(txtIn->Text);
txtOut->Text=x.ToString();
Mengambil isi textbox “txtIn” kemudian dirubah/dikonversi menjadi sebuah data yang bertipe double yang disimpan kedalam sebuah variabel yang diberi nama x. Kemudian variabel x tersebut ditampilkan ke textbox “txtOut”.

y = System::Int32::Parse(txtIn->Text);
txtOut->Text=y.ToString();
Mengambil isi textbox “txtIn” kemudian dirubah/dikonversi menjadi sebuah data yang bertipe integer 32 bit yang disimpan kedalam sebuah variabel yang diberi nama y. Kemudian variabel y tersebut ditampilkan ke textbox “txtOut”.

String ^ str= gcnew String(txtIn->Text);

array<Char>^ c = str->ToCharArray();
                                   
String ^ stra = gcnew String(c[0].ToString());
txtOut->Text = stra;
Mengambil isi textbox “txtIn” kemudian disimpan kedalam sebuah string yang diberi nama str.
Setelah itu, string str dikonversi menjadi data array yang bertipe char, sehingga nantinya dapat diakses perindeks dari arraynya. Pada bagian program diatas, data pada indeks 0 ditampilkan kembali ke textbox “txtOut”.

z = System::Double::Parse(txtIn->Text);
z=Math::Round(z,1);
txtOut->Text=z.ToString();
Mengambil isi textbox “txtIn” kemudian dirubah/dikonversi menjadi sebuah data yang bertipe double yang disimpan kedalam sebuah variabel yang diberi nama z. setelah itu, data pada variabel z dibulatkan menjadi 1 angka dibelakang koma dengan menggunakan sintak “Math::Round(z,1)”. Pengaturan banyaknya angka dibelakang koma dapat disesuaikan dengan kebutuhan, kita cukup mengubah angka 1 menjadi angka yang sesuai dengan jumlah angka dibelakang koma yang kita inginkan dari sebuah variabel. Kemudian data variabel z tersebut ditampilkan ke textbox “txtOut”.


String ^ ab= gcnew String(txtIn->Text);
int k;

k=ab->Length;
txtOut->Text = k.ToString();
Mengambil isi textbox “txtIn” kemudian disimpan kedalam sebuah string yang diberi nama ab. Setelah itu, panjang string tersebut diukur/dihitung dengan menggunakan sintak “ab->Length” yang mana hasil pengukuran tersebut dimasukkan kedalam variabel k yang kemudian ditampilkan ke textbox “txtOut”.

txtOut->Text=txtIn->Text;
Mengambil isi textbox “txtIn” kemudian ditampilkan kembali ke textbox “txtOut”.

txtOut->Text=txtIn->Text;        
txtOut->ForeColor=System::Drawing::Color::Red;
Mengambil isi textbox “txtIn” kemudian ditampilkan kembali ke textbox “txtOut”. Selain itu warna huruf pada txtOut akan berubahdari yang awalnya (default) berwarna hitam menjadi berwarna merah. Selain warna merah (Red) masih banyak warna yang lainnya.

Itulah beberapa sintak program yang bisa digunakan jika ingin mengakses data dari sebuah textbox. Untuk contoh program lengkapnya bisa dilihat dibawah ini :

#pragma once

namespace c {

       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
       {
       public:
              Form1(void)
              {
                     InitializeComponent();
                     //
                     //TODO: Add the constructor code here
                     //
                     rb2String->Checked=true;//membuat rb2String menjadi pilihan default
              }

       protected:
              /// <summary>
              /// Clean up any resources being used.
              /// </summary>
              ~Form1()
              {
                     if (components)
                     {
                           delete components;
                     }
              }
       private: System::Windows::Forms::TextBox^  txtIn;
       private: System::Windows::Forms::TextBox^  txtOut;
       protected:
       protected:
       private: System::Windows::Forms::Button^  btnProses;
       private: System::Windows::Forms::Label^  label1;
       private: System::Windows::Forms::RadioButton^  rb2String;
       private: System::Windows::Forms::RadioButton^  rb2Double;
       private: System::Windows::Forms::RadioButton^  rb2Int;
       private: System::Windows::Forms::RadioButton^  rb2Char;
       private: System::Windows::Forms::RadioButton^  rbRound;
       private: System::Windows::Forms::RadioButton^  rbLength;
       private: System::Windows::Forms::RadioButton^  rbWrite;
       private: System::Windows::Forms::RadioButton^  rbForecolor;


       private:
              /// <summary>
              /// Required designer variable.
              /// </summary>
              System::ComponentModel::Container ^components;

#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->txtIn = (gcnew System::Windows::Forms::TextBox());
                     this->txtOut = (gcnew System::Windows::Forms::TextBox());
                     this->btnProses = (gcnew System::Windows::Forms::Button());
                     this->label1 = (gcnew System::Windows::Forms::Label());
                     this->rb2String = (gcnew System::Windows::Forms::RadioButton());
                     this->rb2Double = (gcnew System::Windows::Forms::RadioButton());
                     this->rb2Int = (gcnew System::Windows::Forms::RadioButton());
                     this->rb2Char = (gcnew System::Windows::Forms::RadioButton());
                     this->rbRound = (gcnew System::Windows::Forms::RadioButton());
                     this->rbLength = (gcnew System::Windows::Forms::RadioButton());
                     this->rbWrite = (gcnew System::Windows::Forms::RadioButton());
                     this->rbForecolor = (gcnew System::Windows::Forms::RadioButton());
                     this->SuspendLayout();
                     //
                     // txtIn
                     //
                     this->txtIn->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                           static_cast<System::Byte>(0)));
                     this->txtIn->Location = System::Drawing::Point(12, 12);
                     this->txtIn->Name = L"txtIn";
                     this->txtIn->Size = System::Drawing::Size(405, 26);
                     this->txtIn->TabIndex = 0;
                     //
                     // txtOut
                     //
                     this->txtOut->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                           static_cast<System::Byte>(0)));
                     this->txtOut->ForeColor = System::Drawing::SystemColors::Menu;
                     this->txtOut->Location = System::Drawing::Point(12, 177);
                     this->txtOut->Name = L"txtOut";
                     this->txtOut->Size = System::Drawing::Size(405, 26);
                     this->txtOut->TabIndex = 1;
                     //
                     // btnProses
                     //
                     this->btnProses->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                           static_cast<System::Byte>(0)));
                     this->btnProses->Location = System::Drawing::Point(12, 108);
                     this->btnProses->Name = L"btnProses";
                     this->btnProses->Size = System::Drawing::Size(405, 38);
                     this->btnProses->TabIndex = 2;
                     this->btnProses->Text = L"Proses";
                     this->btnProses->UseVisualStyleBackColor = true;
                     this->btnProses->Click += gcnew System::EventHandler(this, &Form1::btnProses_Click);
                     //
                     // label1
                     //
                     this->label1->AutoSize = true;
                     this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                           static_cast<System::Byte>(0)));
                     this->label1->Location = System::Drawing::Point(172, 149);
                     this->label1->Name = L"label1";
                     this->label1->Size = System::Drawing::Size(78, 25);
                     this->label1->TabIndex = 3;
                     this->label1->Text = L"Hasil : ";
                     //
                     // rb2String
                     //
                     this->rb2String->AutoSize = true;
                     this->rb2String->Location = System::Drawing::Point(22, 44);
                     this->rb2String->Name = L"rb2String";
                     this->rb2String->Size = System::Drawing::Size(64, 17);
                     this->rb2String->TabIndex = 4;
                     this->rb2String->TabStop = true;
                     this->rb2String->Text = L"to String";
                     this->rb2String->UseVisualStyleBackColor = true;
                     //
                     // rb2Double
                     //
                     this->rb2Double->AutoSize = true;
                     this->rb2Double->Location = System::Drawing::Point(22, 72);
                     this->rb2Double->Name = L"rb2Double";
                     this->rb2Double->Size = System::Drawing::Size(71, 17);
                     this->rb2Double->TabIndex = 5;
                     this->rb2Double->TabStop = true;
                     this->rb2Double->Text = L"to Double";
                     this->rb2Double->UseVisualStyleBackColor = true;
                     //
                     // rb2Int
                     //
                     this->rb2Int->AutoSize = true;
                     this->rb2Int->Location = System::Drawing::Point(97, 44);
                     this->rb2Int->Name = L"rb2Int";
                     this->rb2Int->Size = System::Drawing::Size(49, 17);
                     this->rb2Int->TabIndex = 6;
                     this->rb2Int->TabStop = true;
                     this->rb2Int->Text = L"to Int";
                     this->rb2Int->UseVisualStyleBackColor = true;
                     //
                     // rb2Char
                     //
                     this->rb2Char->AutoSize = true;
                     this->rb2Char->Location = System::Drawing::Point(97, 72);
                     this->rb2Char->Name = L"rb2Char";
                     this->rb2Char->Size = System::Drawing::Size(85, 17);
                     this->rb2Char->TabIndex = 7;
                     this->rb2Char->TabStop = true;
                     this->rb2Char->Text = L"to char Array";
                     this->rb2Char->UseVisualStyleBackColor = true;
                     //
                     // rbRound
                     //
                     this->rbRound->AutoSize = true;
                     this->rbRound->Location = System::Drawing::Point(194, 44);
                     this->rbRound->Name = L"rbRound";
                     this->rbRound->Size = System::Drawing::Size(81, 17);
                     this->rbRound->TabIndex = 8;
                     this->rbRound->TabStop = true;
                     this->rbRound->Text = L"Pembulatan";
                     this->rbRound->UseVisualStyleBackColor = true;
                     //
                     // rbLength
                     //
                     this->rbLength->AutoSize = true;
                     this->rbLength->Location = System::Drawing::Point(194, 72);
                     this->rbLength->Name = L"rbLength";
                     this->rbLength->Size = System::Drawing::Size(88, 17);
                     this->rbLength->TabIndex = 9;
                     this->rbLength->TabStop = true;
                     this->rbLength->Text = L"Panjang data";
                     this->rbLength->UseVisualStyleBackColor = true;
                     //
                     // rbWrite
                     //
                     this->rbWrite->AutoSize = true;
                     this->rbWrite->Location = System::Drawing::Point(306, 44);
                     this->rbWrite->Name = L"rbWrite";
                     this->rbWrite->Size = System::Drawing::Size(72, 17);
                     this->rbWrite->TabIndex = 10;
                     this->rbWrite->TabStop = true;
                     this->rbWrite->Text = L"copy data";
                     this->rbWrite->UseVisualStyleBackColor = true;
                     //
                     // rbForecolor
                     //
                     this->rbForecolor->AutoSize = true;
                     this->rbForecolor->Location = System::Drawing::Point(306, 72);
                     this->rbForecolor->Name = L"rbForecolor";
                     this->rbForecolor->Size = System::Drawing::Size(111, 17);
                     this->rbForecolor->TabIndex = 11;
                     this->rbForecolor->TabStop = true;
                     this->rbForecolor->Text = L"rubah warna huruf";
                     this->rbForecolor->UseVisualStyleBackColor = true;
                     //
                     // Form1
                     //
                     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                     this->ClientSize = System::Drawing::Size(429, 213);
                     this->Controls->Add(this->rbForecolor);
                     this->Controls->Add(this->rbWrite);
                     this->Controls->Add(this->rbLength);
                     this->Controls->Add(this->rbRound);
                     this->Controls->Add(this->rb2Char);
                     this->Controls->Add(this->rb2Int);
                     this->Controls->Add(this->rb2Double);
                     this->Controls->Add(this->rb2String);
                     this->Controls->Add(this->label1);
                     this->Controls->Add(this->btnProses);
                     this->Controls->Add(this->txtOut);
                     this->Controls->Add(this->txtIn);
                     this->Name = L"Form1";
                     this->Text = L"Olah data textBox - Rifqi";
                     this->ResumeLayout(false);
                     this->PerformLayout();

              }
#pragma endregion

private: System::Void btnProses_Click(System::Object^  sender, System::EventArgs^  e) {
                      if(txtIn->Text!=""){
                            if(rb2String->Checked){
//memindahkan data dari txtIn ke string a
                                   String ^ a= gcnew String(txtIn->Text);
//menampilkan isi data string a ke txtOut
                                   txtOut->Text=a;
                            }
                            else if(rb2Double->Checked){
                                   double x;
                                   try{
//konversi data text ke double
                                         x = System::Double::Parse(txtIn->Text);
//menambahkan nilai variabel x dengan 1.45
                                         x+=1.45;     
//menampilkan isi data x ke txtOut
                                         txtOut->Text=x.ToString();
                                   }
                                   catch(...){
//jika ada error
                                         txtOut->Text = "Tidak bisa dikonversi, ada kesalahan format input";      
                                   }
                             }
                            else if(rb2Int->Checked){
                                   int y;
                                   try{
//konversi data text ke tipe Integer 32 bit
                                         y = System::Int32::Parse(txtIn->Text);
                                         y+=3;//menambahkan nilai variabel y dengan 3
//menampilkan isi data y ke txtOut
                                         txtOut->Text=y.ToString();
                                   }
                                   catch(...){
//jika ada error
                                         txtOut->Text = "Tidak bisa dikonversi, ada kesalahan format input";      
                                   }
                            }
                            else if(rb2Char->Checked){
//ambil data data txtIn kemudian dimasukkan ke string str
                                   String ^ str= gcnew String(txtIn->Text);
//mengubah dari string ke char
                                   array<Char>^ c = str->ToCharArray();
                     //untuk menampilkan sebuah karakter di array c indeks 0 ke textBox
                                   String ^ stra = gcnew String(c[0].ToString());
//menampilkan data string stra ke txtOut
                                   txtOut->Text = stra;
                           }
                            else if(rbRound->Checked){
                                   double z;
                                   try{
//konversi data text ke double
                                         z = System::Double::Parse(txtIn->Text);
//pembulatan 1 angka dibelakang koma    
                                         z=Math::Round(z,1); 
//menampilkan isi data z ke txtOut
                                         txtOut->Text=z.ToString();       
                                   }
                                   catch(...){
//jika ada error
                                         txtOut->Text = "Tidak bisa dikonversi, ada kesalahan format input";      
                                   }
                            }
                            else if(rbLength->Checked){
                                   String ^ ab= gcnew String(txtIn->Text);
                                   int k;
             
//mengukur panjang string/mengukur banyaknya karakter dalam string ab
                                   k=ab->Length;      
//menampilkan data string stra ke txtOut
                                   txtOut->Text = k.ToString();    
                            }
                            else if(rbWrite->Checked){
//menyalin data yang ada di txtIn ke txtOut
                                   txtOut->Text=txtIn->Text;       
                            }
                            else if(rbForecolor->Checked){
//menyalin data yang ada di txtIn ke txtOut
                                   txtOut->Text=txtIn->Text;
//mengubah warna huruf txtOut menjadi warna merah                                         txtOut->ForeColor=System::Drawing::Color::Red; 
                            }
               }
       }
};
}



Desain tampilan aplikasi :


0 komentar:

Posting Komentar