procedure TForm1.Timer1Timer(Sender: TObject);
var
DateTime : TDateTime;
Timestr,Timestr2 : string;
Datestr : string;
begin
DateTime := Time; // store the current date and time
Timestr2 := TimeToStr(DateTime); // convert the time into a string
Caption := Timestr2; // display the time on the form's caption
{Note This could have been done with the following line of code:
Caption := TimeToStr(Time); }
Timestr := formatDateTime('hh:mm', StrToDateTime(Timestr2));//format the time }
Label1.caption := DateToStr(Date);
Label2.caption := Timestr;
Form1.Caption := Caption; //puts time on Form Bar
// Label2.caption := TimeToStr(Time); //alternative method
end;
Category: DELPHI
Send GMail from Delphi Easily
1-first login to : https://www.google.com/settings/security/lesssecureapps and turn on access for secure apps
2-Create on form : button, IdSSLIOHandlerSocketOpenSSL1, IdMessage1, IdSMTP1
3-Code
uses
idglobal;
procedure SendEmail(const Recipients: string; const Subject: string; const Body: string);
var
SMTP: TIdSMTP;
Email: TIdMessage;
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
eFrom: string;
begin
eFrom := 'from@gmail.com';
SMTP := TIdSMTP.Create(nil);
Email := TIdMessage.Create(nil);
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
SSLHandler.MaxLineAction := maException;
SSLHandler.SSLOptions.Method := sslvTLSv1;
SSLHandler.SSLOptions.Mode := sslmUnassigned;
SSLHandler.SSLOptions.VerifyMode := [];
SSLHandler.SSLOptions.VerifyDepth := 0;
SSLHandler.Host := 'smtp.gmail.com';
SSLHandler.Port := 587;
SMTP.IOHandler := SSLHandler;
SMTP.Host := 'smtp.gmail.com';
SMTP.Port := 587;
SMTP.Username := eFrom;
SMTP.Password := 'xxxxxx';
SMTP.UseTLS := utUseExplicitTLS;
Email.From.Address := eFrom;
Email.Recipients.EmailAddresses := Recipients;
Email.Subject := Subject;
Email.Body.Text := Body;
SMTP.Connect; //always raise 'Host not found'
SMTP.Send(Email);
SMTP.Disconnect;
finally
SMTP.Free;
Email.Free;
SSLHandler.Free;
end;
end;
;------------------------------------------------------------------------
procedure TestSendMail(Sender: TObject);
begin
SendEmail('to@yahoo.com', 'Subject', 'Body');
end;
Delphi Get SQL SERVER INSTANCE NAMES
1er tane listbox, adodataset ve buton
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient, ActiveX,DB, ComObj, AdoInt,oleDB,
ADODB, Menus, cxLookAndFeelPainters, StdCtrls, cxButtons, cxControls,
cxContainer, cxEdit, cxTextEdit, cxMemo;
type
TForm1 = class(TForm)
ADODataSet1: TADODataSet;
cxButton1: TcxButton;
cboServers: TListBox;
procedure cxButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure ListAvailableSQLServers(Names : TStrings);
var
RSCon: ADORecordsetConstruction;
Rowset: IRowset;
SourcesRowset: ISourcesRowset;
SourcesRecordset: _Recordset;
SourcesName, SourcesType: TField;
function PtCreateADOObject(const ClassID: TGUID): IUnknown;
var
Status: HResult;
FPUControlWord: Word;
begin
asm
FNSTCW FPUControlWord
end;
Status := CoCreateInstance(
CLASS_Recordset,
nil,
CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER,
IUnknown,
Result);
asm
FNCLEX
FLDCW FPUControlWord
end;
OleCheck(Status);
end;
begin
SourcesRecordset := PtCreateADOObject(CLASS_Recordset) as _Recordset;
RSCon := SourcesRecordset as ADORecordsetConstruction;
SourcesRowset := CreateComObject(ProgIDToClassID('SQLOLEDB Enumerator')) as ISourcesRowset;
OleCheck(SourcesRowset.GetSourcesRowset(nil, IRowset, 0, nil, IUnknown(Rowset)));
RSCon.Rowset := RowSet;
with TADODataSet.Create(nil) do
try
Recordset := SourcesRecordset;
SourcesName := FieldByName('SOURCES_NAME'); { do not localize }
SourcesType := FieldByName('SOURCES_TYPE'); { do not localize }
Names.BeginUpdate;
try
while not EOF do
begin
if (SourcesType.AsInteger = DBSOURCETYPE_DATASOURCE) and (SourcesName.AsString '') then
Names.Add(SourcesName.AsString);
Next;
end;
finally
Names.EndUpdate;
end;
finally
Free;
end;
End;
procedure TForm1.cxButton1Click(Sender: TObject);
begin
Screen.Cursor := crSQLWait;
cboServers.Items.Clear;
try
ListAvailableSQLServers(cboServers.Items);
finally
Screen.Cursor := crDefault;
end;
if cboServers.Items.Count>0 Then
cboServers.Itemindex:=0;
end;
end.
DELPHI PROGRAM HATALARINI MSGBOXDA GOSTER
except
on E : Exception do
ShowMessage(E.Message);
end;
delphide belirli kelime aralarını silmek icin function
belirli seylerin arasını sil pos StdCtrls uses olacak
ve kullanırken ShowMessage(ExtractBetweenTags(cxmemo1.Text,”,”));
Function ExtractBetweenTags(Const Value,TagI,TagF:string):string;
var
i,f : integer;
begin
i:=Pos(TagI,Value);
f:=Pos(TagF,Value);
if (i>0) and (f>i) then
Result:=Copy(Value,i+length(TagI),f-i-length(TagF)+1);
end;
DELPHIDE DBF VERITABANI DOSYASIYLA ISLEM YAPMA
ADOCONNECTION ADODATASET VE DATASOURCE EKLE
DATASOURCA TIKLA VE DATASET AYARINI ADODATASET1 YAP
ADODATASET1IN CONNECTIONINIDA ADOCONNECTION1 YAP
BUTTON VE GRID EKLE
KODLAR BU :
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
cxEdit, DB, cxDBData, Menus, cxLookAndFeelPainters, StdCtrls, cxButtons,
cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
cxGridTableView, cxGridDBTableView, cxGrid, ADODB, Grids, DBGrids;
type
TForm1 = class(TForm)
ADODataSet1: TADODataSet;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
cxGrid1DBTableView1: TcxGridDBTableView;
cxGrid1Level1: TcxGridLevel;
cxGrid1: TcxGrid;
cxButton1: TcxButton;
procedure cxButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.cxButton1Click(Sender: TObject);
var
dbf_folder : string;
begin
dbf_folder:='C:\Users\JJJ\Downloads\Compressed';//set your dbf folder location here
ADOConnection1.LoginPrompt:=false;
ADOConnection1.ConnectionString:=Format('Provider=Microsoft.JET.OLEDB.4.0;Data Source=%s;Extended Properties=dBase IV;',[dbf_folder]);
try
ADOConnection1.Connected:=True;
ADODataSet1.CommandText:='Select * from SSS.dbf'; //make your SQL query using the name of the dbf file
ADODataSet1.Open;
while not ADODataSet1.eof do
begin
//do your stuff here
//ADODataSet1.FieldByName('').AsString
ADODataSet1.Next;
end;
except
on E : Exception do
ShowMessage(E.Message);
end;
cxGrid1DBTableView1.BeginUpdate;
cxGrid1DBTableView1.ClearItems;
cxGrid1DBTableView1.DataController.CreateAllItems;
cxGrid1DBTableView1.EndUpdate;
cxGrid1DBTableView1.Columns[0].Summary.FooterKind:=skCount;
cxGrid1DBTableView1.ApplyBestFit();
end;
end.
delphi cxgrid autofitcolumn , get footer value
//cxGrid1DBTableView1.ApplyBestFit(); //ShowMessage((cxGrid1DBTableView1.DataController.Summary.FooterSummarytexts[5])); //toplam nokta //ShowMessage((cxGrid1DBTableView1.DataController.Summary.FooterSummarytexts[6])); //satış adet //ShowMessage((cxGrid1DBTableView1.DataController.Summary.FooterSummarytexts[7])); //satış tl
Delphide surukle birak excel dosyasini ve cxgride al sheetleri kodlari
unit Unit1;
interface
(*
Code for the article:
Accessing and managing MS Excel sheets with Delphi
http://delphi.about.com/library/weekly/aa090903a.htm
How to retrieve, display and edit Microsoft Excel spreadsheets
with ADO (dbGO) and Delphi. This step-by-step article describes
how to connect to Excel, retrieve sheet data, and enable editing
of data (using the DBGrid). You'll also find a list of most common
errors (and how to deal with them) that might pop up in the process.
*)
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, DB, ADODB, Buttons,
ComCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
cxEdit, cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, cxPC;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
cxPageControl1: TcxPageControl;
cxTabSheet1: TcxTabSheet;
cxTabSheet2: TcxTabSheet;
cxGrid1: TcxGrid;
cxGrid1DBTableView1: TcxGridDBTableView;
cxGrid1Level1: TcxGridLevel;
DBNavigator1: TDBNavigator;
Edit3: TEdit;
ListBox1: TListBox;
ADOConnection1: TADOConnection;
ADOConnection2: TADOConnection;
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
l1: TLabel;
l2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
BitBtn1: TBitBtn;
ComboBox1: TComboBox;
Edit4: TEdit;
Panel2: TPanel;
Button1: TButton;
Button2: TButton;
StatusBar1: TStatusBar;
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure ConnectToExcel;
procedure FetchData;
procedure GetFieldInfo;
procedure DisplayException(Sender:TObject; E: Exception);
public
procedure WMDROPFILES(var msg : TWMDropFiles) ;
message WM_DROPFILES;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses typinfo,shellapi;
{ TForm1 }
procedure TForm1.ConnectToExcel;
var strConn : widestring;
i:integer;
begin
i := Pos('xlsx',edit1.Text);
if i>1 then strConn:='Provider=Microsoft.ACE.OLEDB.12.0;' +
'Data Source=' + Edit1.Text + ';' +
'Extended Properties="Excel 12.0;HDR=YES";'
else
//xlsx
//Provider=Microsoft.ACE.OLEDB.12.0;
//Data Source=c:\myFolder\myExcel2007file.xlsx;
//Extended Properties="Excel 12.0;HDR=YES";
strConn:='Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + Edit1.Text + ';' +
'Extended Properties=Excel 8.0;';
AdoConnection1.Connected:=False;
AdoConnection1.ConnectionString:=strConn;
try
AdoConnection1.Open;
AdoConnection1.GetTableNames(ComboBox1.Items,True);
except
ShowMessage('Unable to connect to Excel, make sure the workbook ' + Edit1.Text + ' exist!');
raise;
end;
end;(*ConnectToExcel*)
procedure TForm1.FetchData;
begin
StatusBar1.SimpleText:='';
if not AdoConnection1.Connected then ConnectToExcel;
AdoQuery1.Close;
if combobox1.Text ='' then combobox1.ItemIndex:=0;
ShowMessage(Edit2.Text+l1.Caption+ComboBox1.Text+l2.Caption);
AdoQuery1.SQL.Text:=Edit2.Text+l1.Caption+ComboBox1.Text+l2.Caption;
try
AdoQuery1.Open;
except
ShowMessage('Unable to read data from Excel, make sure the query ' + Edit1.Text + ' is meaningful!');
raise;
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
FetchData;
GetFieldInfo;
cxGrid1DBTableView1.BeginUpdate;
cxGrid1DBTableView1.ClearItems;
cxGrid1DBTableView1.DataController.CreateAllItems;
cxGrid1DBTableView1.EndUpdate;
cxGrid1DBTableView1.Columns[0].Summary.FooterKind:=skCount;
cxGrid1DBTableView1.ApplyBestFit();
//grdCevapDBTableView2.BeginUpdate;
//grdCevapDBTableView2.ClearItems;
//fillGridView(grdCevapDBTableView2,command);
//grdCevapDBTableView2.DataController.CreateAllItems;
//grdCevapDBTableView2.EndUpdate;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles( Handle, True ) ;
AdoConnection1.LoginPrompt:=False;
AdoQuery1.Connection:=AdoConnection1;
DataSource1.DataSet:=AdoQuery1;
DBNavigator1.DataSource:=DataSource1;
Application.OnException:= DisplayException;
end;
procedure TForm1.DisplayException(Sender: TObject; E: Exception);
begin
StatusBar1.SimpleText:=E.Message;
edit3.Text:=E.Message;
end;
procedure TForm1.GetFieldInfo;
var
i : integer;
ft : TFieldType;
sft : string;
fname : string;
begin
ListBox1.Clear;
for i := 0 to AdoQuery1.Fields.Count - 1 do
begin
ft := AdoQuery1.Fields[i].DataType;
sft := GetEnumName(TypeInfo(TFieldType), Integer(ft));
fname:= AdoQuery1.Fields[i].FieldName;
ListBox1.Items.Add(Format('%d) NAME: %s TYPE: %s',[1+i, fname, sft]));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var sAppend : string;
begin
sAppend:='INSERT INTO [Sheet2$] IN "' + Edit1.Text + '" "Excel 8.0;" SELECT AuthorEmail, Title, Description FROM Articles';
AdoQuery2.SQL.Text:=sAppend;
AdoQuery2.ExecSQL;
end;
procedure TForm1.Button1Click(Sender: TObject);
var sCopy : string;
begin
sCopy := 'SELECT * INTO ["Excel 8.0;Database=' + Edit1.Text + '"].[SheetAuthors] FROM Authors';
AdoQuery2.SQL.Text:=sCopy;
AdoQuery2.ExecSQL;
end;
procedure TForm1.WMDROPFILES(var msg: TWMDropFiles) ;
const
MAXFILENAME = 255;
var
cnt, fileCount : integer;
fileName : array [0..MAXFILENAME] of char;
begin
// how many files dropped?
fileCount := DragQueryFile(msg.Drop, $FFFFFFFF, fileName, MAXFILENAME) ;
// query for file names
for cnt := 0 to -1 + fileCount do
begin
DragQueryFile(msg.Drop, cnt, fileName, MAXFILENAME) ;
//do something with the file(s)
Edit1.Text:=fileName;
end;
//release memory
DragFinish(msg.Drop) ;
end;
end.
Tablo isimlerini listboxa almak delphi
procedure TForm1.cxButton1Click(Sender: TObject);
var
tablolar:Tstrings;
begin
tablolar := TStringList.Create;
try
ADOConnection1.GetTableNames(tablolar);
listbox1.Items.Assign(tablolar);
finally
tablolar.Free
end
end;
Cxgrid filter by code
procedure TForm1.Button3Click(Sender: TObject);
var
AItemList: TcxFilterCriteriaItemList;
begin
drIlaclistesiDBTableView1.DataController.Filter.BeginUpdate;
try
drIlaclistesiDBTableView1.DataController.Filter.Root.Clear;
AItemList := drIlaclistesiDBTableView1.DataController.Filter.Root.AddItemList(fboAnd);
AItemList.AddItem(drIlaclistesiDBTableView1ILACADI, foLike, '*%', '*%');
AItemList.AddItem(drIlaclistesiDBTableView1MIKTARI, foGreater, '0', '0');
finally
drIlaclistesiDBTableView1.DataController.Filter.EndUpdate;
drIlaclistesiDBTableView1.DataController.Filter.Active := true;
end;
end;