[Latest][6]

Delphi
DIY

Login

Enter your username and password to enter your Blogger Dasboard


Featured Post

Power resistor DIY

Power resistor DIY This is one simple way to create power resistor at home. I needed 0,33 ohm resistor with some larger wattage so I de...

Recent Articles

Powered By Blogger

Friday, December 2, 2016

Delphi tutorial - Use external fonts

kobyx     1:36 PM     0
In this example I will show you how to use external fonts in your application, without installing font.
Following video shows details about creating this example, and below this video you can find source code for showed example.



unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm2 = class(TForm)
    Label1: TLabel;
    ListBox1: TListBox;
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    UpDown1: TUpDown;
    FontDialog1: TFontDialog;
    procedure Edit2Change(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
   pthx:WideString;
  end;

var
  Form2: TForm2;
  Strl:TStringList;

implementation
//this is procedure to get files in folder with filter just to search for ttf ( font)files
//and to put them in a stringlist
procedure ListFileDir(Path: string; FileList: TStrings);
var
  SR: TSearchRec;
begin
  if FindFirst(Path + '*.ttf', faAnyFile, SR) = 0 then   begin
    repeat
      if (SR.Attr <> faDirectory) then  begin
        FileList.Add(SR.Name);
      end;
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
end;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
if FontDialog1.Execute then
begin
Label1.Font:=FontDialog1.Font;
end;
end;

procedure TForm2.Edit1Change(Sender: TObject);
begin
Label1.Font.Size:=StrToInt(Edit1.Text);
end;

procedure TForm2.Edit2Change(Sender: TObject);
begin
Label1.Caption:=Edit2.Text;
end;

procedure TForm2.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var i:Integer;
begin
CanClose:=False;
Screen.Cursor:=crHourGlass;
//we need to remove fonts from memory
for I := 0 to Strl.Count-1 do
begin
 RemoveFontResource(PChar(pthx+Strl[i]));
 SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
end;
Strl.Free;
Screen.Cursor:=crDefault;
CanClose:=True;
end;

procedure TForm2.FormCreate(Sender: TObject);
var i:Integer;
begin
pthx:=ExtractFilePath(Application.ExeName);
Strl:=TStringList.Create;
Strl.Duplicates:=dupIgnore;

//get font files
ListFileDir(pthx,Strl);

for I := 0 to Strl.Count-1 do
begin
 AddFontResource(PChar(pthx+Strl[i]));
 SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
end;

ListBox1.Items.AddStrings(Strl);

end;

procedure TForm2.ListBox1Click(Sender: TObject);
begin
if ListBox1.SelCount<>0 then
begin
//this is a way to remove extension from file name , we put empty string
Label1.Font.Name:=ChangeFileExt(ListBox1.Items[ListBox1.ItemIndex],'');
end;
end;

procedure TForm2.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
Edit1.Text:=IntToStr(UpDown1.Position);
end;

end.

Monday, November 14, 2016

Delphi tutorial connect SQLite with ZEOS library

kobyx     12:16 PM     0

For this app to create you will need "sqlite3.dll" file which you can download from : https://sqlite.org/
or you can download file that I used in this example together with exe file:
https://drive.google.com/open?id=0B_njK7HczCjDcjRrTlNNM0g4R2M

Just save it in the same folder where your exe file will be, and link to it in you application like I explained in video tutorial.


The third thing you need is installed ZeosDBO components for Delphi, from ZeosLib Development Group, and the lates source code you can find at http://www.sourceforge.net/projects/zeoslib
/*****************************************************************************/
Basically that are tools that you need.
We start with creating new project , adding necesary components on the form and writting code to achieve what we want.
In the video you can see all these steps, and in the code for showed example you can see details.

unit MainForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, ZAbstractRODataset, ZAbstractDataset,
ZDataset, ZAbstractConnection, ZConnection;

type
TfrmMain = class(TForm)
ZConn: TZConnection;
ZQuery1: TZQuery;
ZQuery2: TZQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
ComboBox1: TComboBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit1: TEdit;
Label4: TLabel;
Edit2: TEdit;
Button3: TButton;
Label5: TLabel;
Edit3: TEdit;
Label6: TLabel;
Edit4: TEdit;
Button4: TButton;
Button5: TButton;
Label7: TLabel;
Label8: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure DBGrid1CellClick(Column: TColumn);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}


procedure TfrmMain.Button1Click(Sender: TObject);
begin
ZConn.Protocol:='sqlite-3';
ZConn.LibraryLocation:=ExtractFilePath(Application.ExeName)+'sqlite3.dll';
if not FileExists(ZConn.LibraryLocation) then Exit;
ZConn.Database:=ExtractFilePath(Application.ExeName)+'testdb.s3db';
//if not FileExists(ZConn.Database) then Exit;
ZConn.Connect;
Label2.Caption:='testdb.s3db';
ComboBox1.Items.Clear;
ZConn.GetTableNames('',ComboBox1.Items);
ComboBox1.ItemIndex:=0;
ComboBox1.OnChange(Self);
end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin
ZQuery1.Close;
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('CREATE TABLE if not exists testtbl(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,'
+'name VARCHAR(255),surname VARCHAR(255), UNIQUE(id))') ;
ZQuery1.ExecSQL;

ComboBox1.Items.Clear;
ZConn.GetTableNames('',ComboBox1.Items);
ComboBox1.ItemIndex:=0;
ComboBox1.OnChange(Self);
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
if Edit1.Text<>'' then
begin
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('Insert into testtbl(name,surname) values('+QuotedStr(Edit1.Text)+','+QuotedStr(Edit2.Text)+')');
ZQuery1.ExecSQL;
ComboBox1.OnChange(Self);
end;
end;

procedure TfrmMain.Button4Click(Sender: TObject);
begin
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('Update testtbl set name='+QuotedStr(Edit3.Text)+',surname='+QuotedStr(Edit4.Text)
+' where id='+QuotedStr(Label8.Caption));
ZQuery1.ExecSQL;
ComboBox1.OnChange(Self);
end;

procedure TfrmMain.Button5Click(Sender: TObject);
begin
ZQuery2.Delete;
end;

procedure TfrmMain.ComboBox1Change(Sender: TObject);
var i :Integer;
begin
ZQuery2.Close;
ZQuery2.SQL.Clear;
ZQuery2.SQL.Add('Select * from '+ComboBox1.Text);
ZQuery2.Open;

for I := 0 to DBGrid1.Columns.Count-1 do
begin
DBGrid1.Columns[i].Width:=100;
end;
end;

procedure TfrmMain.DBGrid1CellClick(Column: TColumn);
begin
Edit3.Text:=ZQuery2.FieldByName('name').AsString;
Edit4.Text:=ZQuery2.FieldByName('surname').AsString;
label8.Caption:=ZQuery2.FieldByName('id').AsString;
end;

end.


Thursday, October 20, 2016

Delphi tutorial – connect MySQL with ZEOS library

kobyx     11:11 AM     0

 

Delphi tutorial – connect MySQL with ZEOS library



After you manage to create this application, you will actually have small MySQL manager.
To achieve this you will have to have access to MySQL server.
In this tutorial I am using portable version of server called „USBWebserver“ which can be downloaded from : www.usbwebserver.net

Here you can see default port to Apache and MySQL server, and if you click on the button PHPMyadmin,
PHPMyAdmin will be opened in default web browser where you will see that default user for accessing PHPMyAdmin is „root“ and default password is „usbw“.



In this example I used these data to access MySQL.
You also have option online with some free MySQL servers, just google something like „free mysql server „ or something like that.
Just pay attention on username , password and port number.

/*****************************************************************************/
Second thing you need to have is „libmysql.dll“ file which is a part of the MySQL Connector, you can find one on official MySQL web page, or to download the one that I used in this example :
https://drive.google.com/open?id=0B_njK7HczCjDMGRVMGhGbUk1OHc
Just save it in the same folder where your exe file will be, and link to it in you application like I explained in video tutorial (ZConnection1.LibraryLocation:=ExtractFilePath(Application.ExeName)+'libmysql.dll';)
/*****************************************************************************/
The third thing you need is installed ZeosDBO components for Delphi, from ZeosLib Development Group, and the lates source code you can find at http://www.sourceforge.net/projects/zeoslib
/*****************************************************************************/
Basically that are tools that you need.
We start with creating new project , adding necesary components on the form and writting code to achieve what we want.
In the video you can see all these steps, and in the code for showed example you can see details.

Code:

unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ZAbstractRODataset, ZAbstractDataset, ZDataset,
  ZAbstractConnection, ZConnection, ComCtrls, StdCtrls, Grids, DBGrids;

type
  TfrmMain = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    txtHost: TEdit;
    txtPort: TEdit;
    txtUser: TEdit;
    Label4: TLabel;
    txtPass: TEdit;
    Button1: TButton;
    Button2: TButton;
    StatusBar1: TStatusBar;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    Label5: TLabel;
    txtDBName: TEdit;
    Button3: TButton;
    cboDatabases: TComboBox;
    cmdDelDB: TButton;
    cmdConnectDB: TButton;
    GroupBox4: TGroupBox;
    cboTables: TComboBox;
    cmdDelTable: TButton;
    cmdOpenTable: TButton;
    Label6: TLabel;
    Button4: TButton;
    txtNewTable: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    txtName: TEdit;
    txtSurname: TEdit;
    Button5: TButton;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ZConnection1AfterConnect(Sender: TObject);
    procedure ZConnection1AfterDisconnect(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure cmdDelDBClick(Sender: TObject);
    procedure cmdConnectDBClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cmdOpenTableClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure cmdDelTableClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.Button1Click(Sender: TObject);
var i:Integer;
begin
with ZConnection1 do begin
  Disconnect;
  Protocol:='mysql';
  LibraryLocation:=ExtractFilePath(Application.ExeName)+'libmysql.dll';
  HostName:=txtHost.Text;
  Port:=StrToInt(txtPort.Text);
  User:=txtUser.Text;
  Password:=txtPass.Text;
  Connect;
end;
cboDatabases.Items.Clear;
ZConnection1.GetCatalogNames(cboDatabases.Items);
cboDatabases.ItemIndex:=0;
end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin
ZConnection1.Disconnect;
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
ZQuery1.Close;
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('CREATE DATABASE '+txtDBName.Text+' DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
ZQuery1.ExecSQL;
Button1.Click;
cboDatabases.Items.Clear;
ZConnection1.GetCatalogNames(cboDatabases.Items);
cboDatabases.ItemIndex:=0;
end;

procedure TfrmMain.Button4Click(Sender: TObject);
begin
ZQuery1.Close;
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('CREATE TABLE IF NOT EXISTS '+txtNewTable.Text
+'(id MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, Name VARCHAR(50), Surname VARCHAR(50),'
+'PRIMARY KEY(id) )DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
ZQuery1.ExecSQL;
cmdConnectDB.Click;
end;

procedure TfrmMain.Button5Click(Sender: TObject);
var nmx,surnmx :string;
begin
nmx:=txtName.Text;
surnmx:=txtSurname.Text;
ZQuery1.Close;
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('INSERT INTO '+cboTables.Text+'(Name,Surname)'
+' VALUES ("'+nmx+'", "'+surnmx+'")');
ZQuery1.ExecSQL;
cmdOpenTable.Click;
end;

procedure TfrmMain.cmdConnectDBClick(Sender: TObject);
begin
ZConnection1.Disconnect;
ZConnection1.Database:=cboDatabases.Text;
ZConnection1.Connect;

cboTables.Items.Clear;
ZConnection1.GetTableNames('',cboTables.Items);
cboTables.ItemIndex:=0;
end;


procedure TfrmMain.cmdDelDBClick(Sender: TObject);
begin
if cboDatabases.Text<>'' then   begin
ZConnection1.Database:='';
ZQuery1.Close;
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('Drop database '+cboDatabases.Text);
ZQuery1.ExecSQL;
end;
    Button1.Click;
cboDatabases.Items.Clear;
ZConnection1.GetCatalogNames(cboDatabases.items);
cboDatabases.ItemIndex:=0;
end;

procedure TfrmMain.cmdDelTableClick(Sender: TObject);
begin
if cboTables.Text<>'' then begin
  ZQuery1.Close;
  ZQuery1.SQL.Clear;
  ZQuery1.SQL.Add('Drop table '+cboTables.Text);
  ZQuery1.ExecSQL;
end;
cmdConnectDB.Click;
end;

procedure TfrmMain.cmdOpenTableClick(Sender: TObject);
var i:Integer;
begin
 if cboTables.Text<>'' then begin
 ZQuery1.Close;
 ZQuery1.SQL.Clear;
 ZQuery1.SQL.Add('Select * from '+cboTables.Text);
 ZQuery1.Active:=True;
 end;
 for I := 0 to DBGrid1.Columns.Count-1 do
   begin
     DBGrid1.Columns[i].Width:=100;
   end;
end;


procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ZConnection1.Disconnect;
end;

procedure TfrmMain.ZConnection1AfterConnect(Sender: TObject);
begin
StatusBar1.Panels[0].Text:='Connected';
end;

procedure TfrmMain.ZConnection1AfterDisconnect(Sender: TObject);
begin
StatusBar1.Panels[0].Text:='Disconnected';
end;

end.

//end code


















Sunday, September 25, 2016

Tutorial by Kobyx Kitchen bar

kobyx     4:27 AM     0
Tutorial by Kobyx   Kitchen bar

Tutorial on creating kitchen bar by yourself.








Saturday, September 24, 2016

Delphi tutorial MS Access database with KADao

kobyx     4:04 AM     0
Delphi tutorial MS Access database with KADao

This is Delphi Tutorial about creating and manipulating MS Access database
with KADAo component ( http://www.kadao.dir.bg/ ) ,
and as usual you can find here source code for this app,
and also exe files at address : https://drive.google.com/open?id=0B_njK7HczCjDWkdtbTVseExyYW8


Here is the video :



Source code for this app example :

unit MainForm

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, KDaoTable, KDaoDataBase, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
lbTblName: TLabel;
Label2: TLabel;
ComboBox1: TComboBox;
Edit1: TEdit;
Label3: TLabel;
Label4: TLabel;
Edit2: TEdit;
Label5: TLabel;
Edit3: TEdit;
Button5: TButton;
Button6: TButton;
Database1: TKADaoDatabase;
Table1: TKADaoTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button7: TButton;
lbDbName: TLabel;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
SaveDialog1.InitialDir:=ExtractFilePath(Application.ExeName) ;
if SaveDialog1.Execute() then
begin

Database1.Close;
Database1.CreateAccessDatabase(SaveDialog1.FileName+'.mdb');
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
TM:TKADaoTableManager;
begin
if Database1.Connected=False then Exit;
try
Database1.Connected:=true;
TM:=TKADaoTableManager.Create(Database1);
TM.TableName:=InputBox('Insert table name','Table name','Table 1');
TM.FieldDefs.Add('Field 1',ftInteger,0,False);
TM.FieldDefs.Add('Field 2',ftString,100,False);
TM.FieldDefs.Add('Field 3',ftDate,0,False);
TM.IndexDefs.Add('Field 1','Field 1',[ixPrimary,ixUnique]);
TM.IndexDefs.Add('Field 2','Field 2',[]);
TM.CreateTable;
lbTblName.Caption:=TM.TableName;
TM.Free;

Database1.Connected:=False;
Database1.Open;
ComboBox1.Items.Clear;
ComboBox1.Items.AddStrings(Database1.TableNames);
ComboBox1.ItemIndex:=0;


except
ShowMessage('Error creating table');
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if OpenDialog1.Execute() then
begin
OpenDialog1.InitialDir:=ExtractFilePath(Application.ExeName);
Database1.Close;
Database1.Database:=OpenDialog1.FileName;
Database1.Open;
lbDbName.Caption:=Database1.Database;
ComboBox1.Items.Clear;
ComboBox1.Items.AddStrings(Database1.TableNames);
ComboBox1.ItemIndex:=0;
end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
if (Database1.Connected=True) and ( ComboBox1.Text<>'') then
begin
Table1.Close;
Table1.TableName:=ComboBox1.Text;
Table1.Open;

DBGrid1.Columns[0].Width:=100;
DBGrid1.Columns[1].Width:=200;
DBGrid1.Columns[2].Width:=100;

end;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
if (Edit1.Text<>'') then begin

Table1.Insert;
Table1.Append;
Table1.FieldByName('Field 1').AsInteger:=StrToInt(Edit1.Text);
Table1.FieldByName('Field 2').AsString:=Edit2.Text;
Table1.FieldByName('Field 3').AsDateTime:=StrToDate(Edit3.Text);
Table1.Post;

end;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
Table1.Edit;
Table1.UpdateRecord;
Table1.Post;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
Table1.Delete;
end;

end.

Monday, June 27, 2016

Delphi tutorial Client server application

kobyx     2:30 PM     0
 Delphi tutorial   Client server application

This is Delphi Tutorial about client-server application, and as usual you can find here source code for this app, and also exe files at address :
https://drive.google.com/open?id=0B_njK7HczCjDekg0WG1hamUyWTA

Here is video tutorial :




and here is source code :
Source code for client app :
unit MainForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;

type
TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; GroupBox1: TGroupBox; CheckBox1: TCheckBox; Label3: TLabel; Label4: TLabel; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Button1: TButton; Button2: TButton; Memo1: TMemo; ClientSocket1: TClientSocket; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private { Private declarations }
public { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.Active:=False;
ClientSocket1.Host:=Edit1.Text;
ClientSocket1.Port:=StrToInt(Edit2.Text)
;
ClientSocket1.Active:=True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ClientSocket1.Socket.SendText(Edit5.Text);
Memo1.Lines.Add('Me : '+edit5.Text);
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
CheckBox1.Checked:=Socket.Connected;
Edit3.Text:=Socket.LocalAddress;
Edit4.Text:=Socket.LocalHost;
Memo1.Lines.Clear;

end;

procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
CheckBox1.Checked:=Socket.Connected;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(ClientSocket1.Socket.ReceiveText);
end;

end.
Source code for server app :
unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;

type
TForm1 = class(TForm) GroupBox1: TGroupBox; GroupBox2: TGroupBox; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; CheckBox1: TCheckBox; Edit1: TEdit; Button1: TButton; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Label6: TLabel; Button2: TButton; Memo1: TMemo; ServerSocket1: TServerSocket; procedure Button1Click(Sender: TObject); procedure ServerSocket1Accept(Sender: TObject; Socket: TCustomWinSocket); procedure Button2Click(Sender: TObject); procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
private { Private declarations }
public { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
port:Integer;
begin
ServerSocket1.Active:=False;
port:=StrToInt(Edit1.Text);
ServerSocket1.Port:=port;
ServerSocket1.Active:=True;
Edit4.Text:='Listening...';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ServerSocket1.Socket.Connections[0].SendText(Edit6.Text);
Memo1.Lines.Add('Me : '+Edit6.Text);
Edit6.Text:='';
end;

procedure TForm1.ServerSocket1Accept(Sender: TObject; Socket: TCustomWinSocket);
begin
CheckBox1.Checked:=true;
Edit2.Text:=Socket.RemoteAddress;
Edit3.Text:=Socket.RemoteHost;
Edit5.Text:=Socket.LocalAddress;
Edit4.Text:='Connected';
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Socket.RemoteHost+' : '+Socket.ReceiveText);
end;

Tuesday, May 3, 2016

Delphi Tutorial Timer

kobyx     2:29 PM     0
In this video you can learn how to work with Timer in Delphi.
You can learn how to manipulate with components using timer.

example application:https://drive.google.com/open?id=0B_njK7HczCjDOEpVMER4aFctSDQ

Here is also code used in this video :

unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, pngimage;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Image1: TImage;
    Button1: TButton;
    Label1: TLabel;
    Button2: TButton;
    ProgressBar1: TProgressBar;
    Timer1: TTimer;
    Timer2: TTimer;
    Timer3: TTimer;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Edit1: TEdit;
    UpDown1: TUpDown;
    Edit2: TEdit;
    UpDown2: TUpDown;
    Edit3: TEdit;
    UpDown3: TUpDown;
    Edit4: TEdit;
    UpDown4: TUpDown;
    Edit5: TEdit;
    UpDown5: TUpDown;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Edit3Change(Sender: TObject);
    procedure Edit4Change(Sender: TObject);
    procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
    procedure UpDown2Click(Sender: TObject; Button: TUDBtnType);
    procedure UpDown3Click(Sender: TObject; Button: TUDBtnType);
    procedure UpDown4Click(Sender: TObject; Button: TUDBtnType);
    procedure Button7Click(Sender: TObject);
    procedure Timer3Timer(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Edit5Change(Sender: TObject);
    procedure UpDown5Click(Sender: TObject; Button: TUDBtnType);
  private
    { Private declarations }
  public
   LabInt:Integer;
   ButtonTag: Integer;

   IntxL, IntxR, IntxB, IntxT : Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
if Timer1.Enabled=false then
  Timer1.Enabled:=true
else
  Timer1.Enabled:=False;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if Timer2.Enabled=false then
  Timer2.Enabled:=true
else
  Timer2.Enabled:=False;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ButtonTag:=101;
Timer3.Enabled:=True;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
ButtonTag:=102;
Timer3.Enabled:=True;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
ButtonTag:=103;
Timer3.Enabled:=True;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
ButtonTag:=104;
Timer3.Enabled:=True;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
Timer1.Enabled:=False;
Timer2.Enabled:=False;
Timer3.Enabled:=False;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
IntxL:=StrToInt(Edit1.Text);
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
IntxR:=StrToInt(Edit2.Text);
end;

procedure TForm1.Edit3Change(Sender: TObject);
begin
IntxT:=StrToInt(Edit3.Text);
end;

procedure TForm1.Edit4Change(Sender: TObject);
begin
IntxB:=StrToInt(Edit4.Text);
end;

procedure TForm1.Edit5Change(Sender: TObject);
begin
Timer3.Interval:=StrToInt(Edit5.Text) ;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

LabInt:=0;

IntxL :=10;
IntxR :=10;
IntxB:=10;
IntxT:=10;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
LabInt:=LabInt+10;
Label1.Caption:=IntToStr(LabInt);
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
if ProgressBar1.Position<ProgressBar1.Max then
 ProgressBar1.Position:=ProgressBar1.Position+1
else
  ProgressBar1.Position:=0;
end;

procedure TForm1.Timer3Timer(Sender: TObject);
begin
case ButtonTag of
101:
if Image1.Left>0 then
Image1.Left:=Image1.Left-IntxL
else Button4.Click;
102:
if Image1.Left<Panel2.Width then
Image1.Left:=Image1.Left+IntxR
else
Button3.Click;
103:
if Image1.Top>0 then
Image1.Top:=Image1.Top-IntxT
else
Button6.Click;
104:
if Image1.Top<Panel2.Height then
 Image1.Top:=Image1.Top+IntxB
 else
 Button5.Click;

end;

end;

procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then IntxL:=IntxL+10;
if Button=btPrev then IntxL:=IntxL-10;
Edit1.Text:=IntToStr(IntxL);
end;

procedure TForm1.UpDown2Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then IntxR:=IntxR+10;
if Button=btPrev then IntxR:=IntxR-10;
Edit2.Text:=IntToStr(IntxR);
end;

procedure TForm1.UpDown3Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then IntxT:=IntxT+10;
if Button=btPrev then IntxT:=IntxT-10;
Edit3.Text:=IntToStr(IntxT);
end;

procedure TForm1.UpDown4Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then IntxB:=IntxB+10;
if Button=btPrev then IntxB:=IntxB-10;
Edit4.Text:=IntToStr(IntxB);
end;

procedure TForm1.UpDown5Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then Timer3.Interval:=Timer3.Interval+10;
if Button=btPrev then Timer3.Interval:=Timer3.Interval-10;
Edit5.Text:=IntToStr(Timer3.Interval);
end;

end.

Comments

© 2014 Tutorials. Designed by Bloggertheme9. Powered by Blogger.