0% found this document useful (0 votes)
12 views44 pages

Components

The document provides an introduction to VCL (Visual Component Library) components used in C++ Builder for developing Windows applications. It categorizes components into standard, additional, Win32, system, data controls, and dialogs, and includes practical examples of using various components such as labels, edit boxes, checkboxes, radio buttons, list boxes, and combo boxes. Each example demonstrates how to create and manipulate these components programmatically.

Uploaded by

asite249
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views44 pages

Components

The document provides an introduction to VCL (Visual Component Library) components used in C++ Builder for developing Windows applications. It categorizes components into standard, additional, Win32, system, data controls, and dialogs, and includes practical examples of using various components such as labels, edit boxes, checkboxes, radio buttons, list boxes, and combo boxes. Each example demonstrates how to create and manipulate these components programmatically.

Uploaded by

asite249
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRACTICAL LESSON: WORKING WITH

COMPONENTS IN C++ BUILDER


PART 1: INTRODUCTION TO VCL COMPONENTS
What are VCL Components?

VCL (Visual Component Library) is a framework of visual and non-visual components for
building Windows applications in C++ Builder. Components are reusable objects that provide
specific functionality.

Component Categories:

1.​ Standard Components: Button, Label, Edit, Memo, CheckBox, RadioButton


2.​ Additional Components: BitBtn, SpeedButton, MaskEdit, StringGrid
3.​ Win32 Components: TreeView, ListView, ProgressBar, TrackBar
4.​ System Components: Timer, MediaPlayer, FileListBox
5.​ Data Controls: DBGrid, DBEdit, DBNavigator
6.​ Dialogs: OpenDialog, SaveDialog, ColorDialog, FontDialog

PART 2: STANDARD COMPONENTS


Example 1: Label and Edit Components

cpp

//---------------------------------------------------------------------------

// Unit1.h

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <[Link]>

#include <[Link]>

#include <[Link]>
#include <[Link]>

//---------------------------------------------------------------------------

class TForm1 : public TForm

__published:​ // IDE-managed Components

TLabel *Label1;

TLabel *Label2;

TLabel *lblOutput;

TEdit *edtFirstName;

TEdit *edtLastName;

TButton *btnDisplay;

TButton *btnClear;

void __fastcall btnDisplayClick(TObject *Sender);

void __fastcall btnClearClick(TObject *Sender);

void __fastcall FormCreate(TObject *Sender);

void __fastcall edtFirstNameChange(TObject *Sender);

private:​ // User declarations

public:​ ​ // User declarations

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif
cpp

//---------------------------------------------------------------------------

// [Link]

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

// Setting component properties programmatically

Form1->Caption = "Label and Edit Example";

Form1->Width = 500;

Form1->Height = 300;

// Label properties
Label1->Caption = "First Name:";

Label1->Font->Size = 10;

Label1->Font->Style = TFontStyles() << fsBold;

Label2->Caption = "Last Name:";

Label2->Font->Size = 10;

Label2->Font->Style = TFontStyles() << fsBold;

lblOutput->Caption = "";

lblOutput->Font->Size = 12;

lblOutput->Font->Color = clBlue;

// Edit properties

edtFirstName->Text = "";

edtFirstName->MaxLength = 50;

edtFirstName->CharCase = ecUpperCase; // Auto uppercase

edtLastName->Text = "";

edtLastName->MaxLength = 50;

// Button properties

btnDisplay->Caption = "Display Full Name";

btnDisplay->Width = 150;

btnClear->Caption = "Clear";

btnClear->Width = 100;
}

//---------------------------------------------------------------------------

void __fastcall TForm1::btnDisplayClick(TObject *Sender)

String firstName = edtFirstName->[Link]();

String lastName = edtLastName->[Link]();

if([Link]() || [Link]()) {

ShowMessage("Please enter both first and last name!");

return;

String fullName = firstName + " " + lastName;

lblOutput->Caption = "Full Name: " + fullName;

//---------------------------------------------------------------------------

void __fastcall TForm1::btnClearClick(TObject *Sender)

edtFirstName->Clear();

edtLastName->Clear();

lblOutput->Caption = "";

edtFirstName->SetFocus();

//---------------------------------------------------------------------------
void __fastcall TForm1::edtFirstNameChange(TObject *Sender)

// Real-time character count

Form1->Caption = "Characters: " + IntToStr(edtFirstName->[Link]());

//---------------------------------------------------------------------------

Example 2: Memo Component

cpp

//---------------------------------------------------------------------------

// Unit1.h

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <[Link]>

#include <[Link]>

#include <[Link]>

#include <[Link]>

//---------------------------------------------------------------------------

class TForm1 : public TForm

__published:​ // IDE-managed Components

TMemo *Memo1;

TButton *btnAddLine;

TButton *btnClear;
TButton *btnCount;

TButton *btnSave;

TEdit *edtInput;

TLabel *Label1;

TLabel *lblInfo;

void __fastcall FormCreate(TObject *Sender);

void __fastcall btnAddLineClick(TObject *Sender);

void __fastcall btnClearClick(TObject *Sender);

void __fastcall btnCountClick(TObject *Sender);

void __fastcall btnSaveClick(TObject *Sender);

private:​ // User declarations

public:​ ​ // User declarations

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

cpp

//---------------------------------------------------------------------------

// [Link]

#include <vcl.h>

#pragma hdrstop
#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

Form1->Caption = "Memo Component Example";

// Memo properties

Memo1->Lines->Clear();

Memo1->ScrollBars = ssVertical;

Memo1->WordWrap = true;

Memo1->Font->Name = "Courier New";

Memo1->Font->Size = 10;

// Add initial text

Memo1->Lines->Add("Welcome to Memo Component!");

Memo1->Lines->Add("This is line 2");


Memo1->Lines->Add("You can add multiple lines here");

Label1->Caption = "Enter text:";

lblInfo->Caption = "";

btnAddLine->Caption = "Add Line";

btnClear->Caption = "Clear All";

btnCount->Caption = "Count Lines/Words";

btnSave->Caption = "Save to File";

//---------------------------------------------------------------------------

void __fastcall TForm1::btnAddLineClick(TObject *Sender)

if(!edtInput->[Link]()) {

Memo1->Lines->Add(edtInput->Text);

edtInput->Clear();

edtInput->SetFocus();

//---------------------------------------------------------------------------

void __fastcall TForm1::btnClearClick(TObject *Sender)

if(MessageDlg("Clear all text?", mtConfirmation,

TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)


{

Memo1->Clear();

lblInfo->Caption = "";

//---------------------------------------------------------------------------

void __fastcall TForm1::btnCountClick(TObject *Sender)

int lineCount = Memo1->Lines->Count;

// Count words

String allText = Memo1->Text;

int wordCount = 0;

bool inWord = false;

for(int i = 1; i <= [Link](); i++) {

if(allText[i] == ' ' || allText[i] == '\n' || allText[i] == '\r') {

inWord = false;

else if(!inWord) {

wordCount++;

inWord = true;

}
lblInfo->Caption = "Lines: " + IntToStr(lineCount) +

", Words: " + IntToStr(wordCount);

//---------------------------------------------------------------------------

void __fastcall TForm1::btnSaveClick(TObject *Sender)

try {

Memo1->Lines->SaveToFile("[Link]");

ShowMessage("Text saved to [Link] successfully!");

catch(Exception &e) {

ShowMessage("Error saving file: " + [Link]);

//---------------------------------------------------------------------------

Example 3: CheckBox and RadioButton Components

cpp

//---------------------------------------------------------------------------

// Unit1.h

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <[Link]>

#include <[Link]>
#include <[Link]>

#include <[Link]>

//---------------------------------------------------------------------------

class TForm1 : public TForm

__published:​ // IDE-managed Components

TCheckBox *chkBold;

TCheckBox *chkItalic;

TCheckBox *chkUnderline;

TRadioButton *rbRed;

TRadioButton *rbGreen;

TRadioButton *rbBlue;

TLabel *lblSample;

TButton *btnApply;

TGroupBox *GroupBox1;

TGroupBox *GroupBox2;

void __fastcall FormCreate(TObject *Sender);

void __fastcall btnApplyClick(TObject *Sender);

void __fastcall chkBoldClick(TObject *Sender);

void __fastcall rbRedClick(TObject *Sender);

private:​ // User declarations

void applyFormatting();

public:​ ​ // User declarations


__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

cpp

//---------------------------------------------------------------------------

// [Link]

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{
Form1->Caption = "CheckBox and RadioButton Example";

// GroupBox captions

GroupBox1->Caption = "Text Style";

GroupBox2->Caption = "Text Color";

// CheckBox captions

chkBold->Caption = "Bold";

chkItalic->Caption = "Italic";

chkUnderline->Caption = "Underline";

// RadioButton captions

rbRed->Caption = "Red";

rbGreen->Caption = "Green";

rbBlue->Caption = "Blue";

// Set default selection

rbRed->Checked = true;

// Sample label

lblSample->Caption = "Sample Text";

lblSample->Font->Size = 16;

btnApply->Caption = "Apply Formatting";

//---------------------------------------------------------------------------
void TForm1::applyFormatting()

// Apply font styles

TFontStyles styles;

if(chkBold->Checked) {

styles = styles << fsBold;

if(chkItalic->Checked) {

styles = styles << fsItalic;

if(chkUnderline->Checked) {

styles = styles << fsUnderline;

lblSample->Font->Style = styles;

// Apply color

if(rbRed->Checked) {

lblSample->Font->Color = clRed;

else if(rbGreen->Checked) {

lblSample->Font->Color = clGreen;

else if(rbBlue->Checked) {
lblSample->Font->Color = clBlue;

//---------------------------------------------------------------------------

void __fastcall TForm1::btnApplyClick(TObject *Sender)

applyFormatting();

//---------------------------------------------------------------------------

void __fastcall TForm1::chkBoldClick(TObject *Sender)

// Apply formatting in real-time

applyFormatting();

//---------------------------------------------------------------------------

void __fastcall TForm1::rbRedClick(TObject *Sender)

// Apply formatting in real-time

applyFormatting();

//---------------------------------------------------------------------------

Example 4: ListBox and ComboBox Components


cpp

//---------------------------------------------------------------------------

// Unit1.h

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <[Link]>

#include <[Link]>

#include <[Link]>

#include <[Link]>

//---------------------------------------------------------------------------

class TForm1 : public TForm

__published:​ // IDE-managed Components

TListBox *lstItems;

TComboBox *cmbCategories;

TEdit *edtItem;

TButton *btnAdd;

TButton *btnRemove;

TButton *btnClear;

TButton *btnMoveUp;

TButton *btnMoveDown;

TLabel *Label1;

TLabel *Label2;

TLabel *lblSelected;
void __fastcall FormCreate(TObject *Sender);

void __fastcall btnAddClick(TObject *Sender);

void __fastcall btnRemoveClick(TObject *Sender);

void __fastcall btnClearClick(TObject *Sender);

void __fastcall btnMoveUpClick(TObject *Sender);

void __fastcall btnMoveDownClick(TObject *Sender);

void __fastcall lstItemsClick(TObject *Sender);

void __fastcall cmbCategoriesChange(TObject *Sender);

private:​ // User declarations

public:​ ​ // User declarations

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

cpp

//---------------------------------------------------------------------------

// [Link]

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

Form1->Caption = "ListBox and ComboBox Example";

Label1->Caption = "Enter Item:";

Label2->Caption = "Category:";

// Setup ComboBox

cmbCategories->Style = csDropDownList; // Dropdown only, no edit

cmbCategories->Items->Clear();

cmbCategories->Items->Add("Fruits");

cmbCategories->Items->Add("Vegetables");

cmbCategories->Items->Add("Dairy");

cmbCategories->Items->Add("Beverages");

cmbCategories->ItemIndex = 0; // Select first item

// Setup ListBox
lstItems->Clear();

// Button captions

btnAdd->Caption = "Add Item";

btnRemove->Caption = "Remove Selected";

btnClear->Caption = "Clear All";

btnMoveUp->Caption = "Move Up";

btnMoveDown->Caption = "Move Down";

lblSelected->Caption = "No item selected";

//---------------------------------------------------------------------------

void __fastcall TForm1::btnAddClick(TObject *Sender)

if(edtItem->[Link]()) {

ShowMessage("Please enter an item name!");

return;

String category = cmbCategories->Text;

String item = "[" + category + "] " + edtItem->Text;

lstItems->Items->Add(item);

edtItem->Clear();

edtItem->SetFocus();
}

//---------------------------------------------------------------------------

void __fastcall TForm1::btnRemoveClick(TObject *Sender)

int index = lstItems->ItemIndex;

if(index == -1) {

ShowMessage("Please select an item to remove!");

return;

lstItems->Items->Delete(index);

lblSelected->Caption = "No item selected";

//---------------------------------------------------------------------------

void __fastcall TForm1::btnClearClick(TObject *Sender)

if(MessageDlg("Clear all items?", mtConfirmation,

TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)

lstItems->Clear();

lblSelected->Caption = "No item selected";

}
//---------------------------------------------------------------------------

void __fastcall TForm1::btnMoveUpClick(TObject *Sender)

int index = lstItems->ItemIndex;

if(index <= 0) {

return; // Already at top or nothing selected

String temp = lstItems->Items->Strings[index];

lstItems->Items->Strings[index] = lstItems->Items->Strings[index - 1];

lstItems->Items->Strings[index - 1] = temp;

lstItems->ItemIndex = index - 1;

//---------------------------------------------------------------------------

void __fastcall TForm1::btnMoveDownClick(TObject *Sender)

int index = lstItems->ItemIndex;

if(index == -1 || index >= lstItems->Items->Count - 1) {

return; // Already at bottom or nothing selected

String temp = lstItems->Items->Strings[index];


lstItems->Items->Strings[index] = lstItems->Items->Strings[index + 1];

lstItems->Items->Strings[index + 1] = temp;

lstItems->ItemIndex = index + 1;

//---------------------------------------------------------------------------

void __fastcall TForm1::lstItemsClick(TObject *Sender)

int index = lstItems->ItemIndex;

if(index != -1) {

lblSelected->Caption = "Selected: " + lstItems->Items->Strings[index];

//---------------------------------------------------------------------------

void __fastcall TForm1::cmbCategoriesChange(TObject *Sender)

Form1->Caption = "Category: " + cmbCategories->Text;

//---------------------------------------------------------------------------

PART 3: ADVANCED COMPONENTS


Example 5: StringGrid Component

cpp

//---------------------------------------------------------------------------
// Unit1.h

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <[Link]>

#include <[Link]>

#include <[Link]>

#include <[Link]>

#include <[Link]>

//---------------------------------------------------------------------------

class TForm1 : public TForm

__published:​ // IDE-managed Components

TStringGrid *sgProducts;

TEdit *edtProduct;

TEdit *edtPrice;

TEdit *edtQuantity;

TButton *btnAdd;

TButton *btnDelete;

TButton *btnCalculate;

TLabel *Label1;

TLabel *Label2;

TLabel *Label3;

TLabel *lblTotal;

void __fastcall FormCreate(TObject *Sender);


void __fastcall btnAddClick(TObject *Sender);

void __fastcall btnDeleteClick(TObject *Sender);

void __fastcall btnCalculateClick(TObject *Sender);

void __fastcall sgProductsClick(TObject *Sender);

void __fastcall sgProductsDrawCell(TObject *Sender, int ACol, int ARow,

TRect &Rect, TGridDrawState State);

private:​ // User declarations

void setupGrid();

void clearFields();

double calculateTotal();

public:​ ​ // User declarations

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

cpp

//---------------------------------------------------------------------------

// [Link]

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

//---------------------------------------------------------------------------

void TForm1::setupGrid()

// Grid setup

sgProducts->ColCount = 4;

sgProducts->RowCount = 2; // Header + 1 data row

sgProducts->FixedRows = 1;

sgProducts->FixedCols = 0;

// Column headers

sgProducts->Cells[0][0] = "Product Name";

sgProducts->Cells[1][0] = "Price";

sgProducts->Cells[2][0] = "Quantity";

sgProducts->Cells[3][0] = "Total";

// Column widths
sgProducts->ColWidths[0] = 150;

sgProducts->ColWidths[1] = 80;

sgProducts->ColWidths[2] = 80;

sgProducts->ColWidths[3] = 100;

// Grid options

sgProducts->Options = sgProducts->Options << goRowSelect;

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

Form1->Caption = "StringGrid Product Manager";

Label1->Caption = "Product:";

Label2->Caption = "Price:";

Label3->Caption = "Quantity:";

btnAdd->Caption = "Add Product";

btnDelete->Caption = "Delete Selected";

btnCalculate->Caption = "Calculate Total";

lblTotal->Caption = "Total: $0.00";

lblTotal->Font->Size = 12;

lblTotal->Font->Style = TFontStyles() << fsBold;


setupGrid();

//---------------------------------------------------------------------------

void TForm1::clearFields()

edtProduct->Clear();

edtPrice->Clear();

edtQuantity->Clear();

edtProduct->SetFocus();

//---------------------------------------------------------------------------

void __fastcall TForm1::btnAddClick(TObject *Sender)

// Validation

if(edtProduct->[Link]()) {

ShowMessage("Please enter product name!");

return;

double price, quantity;

try {

price = StrToFloat(edtPrice->Text);

quantity = StrToFloat(edtQuantity->Text);

}
catch(...) {

ShowMessage("Please enter valid numbers for price and quantity!");

return;

// Add new row

int newRow = sgProducts->RowCount;

sgProducts->RowCount = newRow + 1;

// Fill cells

sgProducts->Cells[0][newRow] = edtProduct->Text;

sgProducts->Cells[1][newRow] = FormatFloat("0.00", price);

sgProducts->Cells[2][newRow] = IntToStr((int)quantity);

sgProducts->Cells[3][newRow] = FormatFloat("0.00", price * quantity);

clearFields();

//---------------------------------------------------------------------------

void __fastcall TForm1::btnDeleteClick(TObject *Sender)

int currentRow = sgProducts->Row;

if(currentRow <= 0) {

ShowMessage("Please select a row to delete!");

return;
}

// Shift rows up

for(int i = currentRow; i < sgProducts->RowCount - 1; i++) {

for(int j = 0; j < sgProducts->ColCount; j++) {

sgProducts->Cells[j][i] = sgProducts->Cells[j][i + 1];

sgProducts->RowCount = sgProducts->RowCount - 1;

//---------------------------------------------------------------------------

double TForm1::calculateTotal()

double total = 0;

for(int i = 1; i < sgProducts->RowCount; i++) {

try {

double rowTotal = StrToFloat(sgProducts->Cells[3][i]);

total += rowTotal;

catch(...) {

// Skip invalid rows

}
return total;

//---------------------------------------------------------------------------

void __fastcall TForm1::btnCalculateClick(TObject *Sender)

double total = calculateTotal();

lblTotal->Caption = "Total: $" + FormatFloat("0.00", total);

//---------------------------------------------------------------------------

void __fastcall TForm1::sgProductsClick(TObject *Sender)

int row = sgProducts->Row;

if(row > 0) {

edtProduct->Text = sgProducts->Cells[0][row];

edtPrice->Text = sgProducts->Cells[1][row];

edtQuantity->Text = sgProducts->Cells[2][row];

//---------------------------------------------------------------------------

void __fastcall TForm1::sgProductsDrawCell(TObject *Sender, int ACol,

int ARow, TRect &Rect,


TGridDrawState State)

// Custom cell drawing for header

if(ARow == 0) {

sgProducts->Canvas->Brush->Color = clNavy;

sgProducts->Canvas->Font->Color = clWhite;

sgProducts->Canvas->Font->Style = TFontStyles() << fsBold;

sgProducts->Canvas->FillRect(Rect);

sgProducts->Canvas->TextOut([Link] + 2, [Link] + 2,

sgProducts->Cells[ACol][ARow]);

//---------------------------------------------------------------------------

Example 6: Timer Component

cpp

//---------------------------------------------------------------------------

// Unit1.h

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <[Link]>

#include <[Link]>

#include <[Link]>

#include <[Link]>

#include <[Link]>
//---------------------------------------------------------------------------

class TForm1 : public TForm

__published:​ // IDE-managed Components

TTimer *Timer1;

TLabel *lblClock;

TLabel *lblCounter;

TButton *btnStart;

TButton *btnStop;

TButton *btnReset;

TProgressBar *ProgressBar1;

void __fastcall FormCreate(TObject *Sender);

void __fastcall Timer1Timer(TObject *Sender);

void __fastcall btnStartClick(TObject *Sender);

void __fastcall btnStopClick(TObject *Sender);

void __fastcall btnResetClick(TObject *Sender);

private:​ // User declarations

int counter;

public:​ ​ // User declarations

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;


//---------------------------------------------------------------------------

#endif

cpp

//---------------------------------------------------------------------------

// [Link]

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

Form1->Caption = "Timer Component Example";

// Timer setup

Timer1->Interval = 1000; // 1 second


Timer1->Enabled = false;

// Labels

lblClock->Font->Size = 16;

lblClock->Font->Style = TFontStyles() << fsBold;

lblClock->Caption = TimeToStr(Time());

lblCounter->Font->Size = 20;

lblCounter->Font->Style = TFontStyles() << fsBold;

lblCounter->Font->Color = clBlue;

// Buttons

btnStart->Caption = "Start";

btnStop->Caption = "Stop";

btnReset->Caption = "Reset";

// ProgressBar

ProgressBar1->Min = 0;

ProgressBar1->Max = 60;

ProgressBar1->Position = 0;

counter = 0;

lblCounter->Caption = IntToStr(counter);

//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)

// Update clock

lblClock->Caption = TimeToStr(Time());

// Update counter

counter++;

lblCounter->Caption = IntToStr(counter);

// Update progress bar

if(counter <= 60) {

ProgressBar1->Position = counter;

else {

ProgressBar1->Position = counter % 60;

// Change color based on counter

if(counter % 2 == 0) {

lblCounter->Font->Color = clBlue;

else {

lblCounter->Font->Color = clRed;

//---------------------------------------------------------------------------
void __fastcall TForm1::btnStartClick(TObject *Sender)

Timer1->Enabled = true;

btnStart->Enabled = false;

btnStop->Enabled = true;

//---------------------------------------------------------------------------

void __fastcall TForm1::btnStopClick(TObject *Sender)

Timer1->Enabled = false;

btnStart->Enabled = true;

btnStop->Enabled = false;

//---------------------------------------------------------------------------

void __fastcall TForm1::btnResetClick(TObject *Sender)

Timer1->Enabled = false;

counter = 0;

lblCounter->Caption = IntToStr(counter);

ProgressBar1->Position = 0;

btnStart->Enabled = true;

btnStop->Enabled = false;

}
//---------------------------------------------------------------------------

PART 4: DIALOG COMPONENTS


Example 7: File Dialogs

cpp

//---------------------------------------------------------------------------

// Unit1.h

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <[Link]>

#include <[Link]>

#include <[Link]>

#include <[Link]>

#include <[Link]>

//---------------------------------------------------------------------------

class TForm1 : public TForm

__published:​ // IDE-managed Components

TMemo *Memo1;

TButton *btnOpen;

TButton *btnSave;

TButton *btnColor;

TButton *btnFont;

TOpenDialog *OpenDialog1;

TSaveDialog *SaveDialog1;
TColorDialog *ColorDialog1;

TFontDialog *FontDialog1;

TLabel *lblFileName;

void __fastcall FormCreate(TObject *Sender);

void __fastcall btnOpenClick(TObject *Sender);

void __fastcall btnSaveClick(TObject *Sender);

void __fastcall btnColorClick(TObject *Sender);

void __fastcall btnFontClick(TObject *Sender);

private:​ // User declarations

String currentFileName;

public:​ ​ // User declarations

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

cpp

//---------------------------------------------------------------------------

// [Link]

#include <vcl.h>

#pragma hdrstop
#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

Form1->Caption = "Dialog Components Example";

// OpenDialog setup

OpenDialog1->Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";

OpenDialog1->DefaultExt = "txt";

// SaveDialog setup

SaveDialog1->Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";

SaveDialog1->DefaultExt = "txt";

// Buttons

btnOpen->Caption = "Open File";


btnSave->Caption = "Save File";

btnColor->Caption = "Change Color";

btnFont->Caption = "Change Font";

lblFileName->Caption = "No file loaded";

currentFileName = "";

//---------------------------------------------------------------------------

void __fastcall TForm1::btnOpenClick(TObject *Sender)

if(OpenDialog1->Execute()) {

try {

Memo1->Lines->LoadFromFile(OpenDialog1->FileName);

currentFileName = OpenDialog1->FileName;

lblFileName->Caption = "File: " + currentFileName;

ShowMessage("File opened successfully!");

catch(Exception &e) {

ShowMessage("Error opening file: " + [Link]);

//---------------------------------------------------------------------------

void __fastcall TForm1::btnSaveClick(TObject *Sender)


{

if([Link]()) {

if(SaveDialog1->Execute()) {

currentFileName = SaveDialog1->FileName;

else {

return;

try {

Memo1->Lines->SaveToFile(currentFileName);

lblFileName->Caption = "File: " + currentFileName;

ShowMessage("File saved successfully!");

catch(Exception &e) {

ShowMessage("Error saving file: " + [Link]);

//---------------------------------------------------------------------------

void __fastcall TForm1::btnColorClick(TObject *Sender)

ColorDialog1->Color = Memo1->Color;

if(ColorDialog1->Execute()) {
Memo1->Color = ColorDialog1->Color;

//---------------------------------------------------------------------------

void __fastcall TForm1::btnFontClick(TObject *Sender)

FontDialog1->Font = Memo1->Font;

if(FontDialog1->Execute()) {

Memo1->Font = FontDialog1->Font;

//---------------------------------------------------------------------------

PART 5: PRACTICE EXERCISES


Exercise 1: Image Viewer

Create an application using TImage component to:

●​ Load and display images


●​ Zoom in/out
●​ Rotate image
●​ Save modified image

Exercise 2: Music Player Interface

Create a music player UI using:

●​ TrackBar for volume control


●​ ProgressBar for playback progress
●​ Buttons for play/pause/stop
●​ ListBox for playlist
Exercise 3: Contact Manager

Build a contact manager with:

●​ StringGrid to display contacts


●​ Edit fields for name, phone, email
●​ Search functionality with ComboBox
●​ Save/Load from file using dialogs

Exercise 4: Calculator with History

Create a calculator with:

●​ Button components for digits and operations


●​ ListBox to show calculation history
●​ Memory functions (M+, M-, MR, MC)
●​ Clear and backspace functionality

Exercise 5: Countdown Timer

Build a countdown timer using:

●​ Timer component
●​ SpinEdit or TrackBar for time setting
●​ ProgressBar for visual countdown
●​ Alert message when time's up

You might also like