Case Statement Using String ..?

Saturday, March 29, 2008

Have you ever wish that delphi case statement can use with string, and you find out that it was can't and it only received ordinal ...?

well with a little trick now you can use case with string. Using some function below, now u can use string with case statement.

function StringToCaseSelect(Selector : string;
CaseList: array of string): Integer;
var cnt: integer;
begin
Result:=-1;
for cnt:=0 to Length(CaseList)-1 do
begin
if CompareText(Selector, CaseList[cnt]) = 0 then
begin
Result:=cnt;
Break;
end;
end;
end;


Usage:
case StringToCaseSelect('Delphi', ['About','Borland','Delphi']) of
0:ShowMessage('You''ve picked About') ;
1:ShowMessage('You''ve picked Borland') ;
2:ShowMessage('You''ve picked Delphi') ;
end;

Taken From Delphi.About.com

Diposting oleh admin di 8:02 AM 0 komentar  

Drag And Drop On DBGrid

Wednesday, January 9, 2008

On Recently, i received project that required DBGrid having ability drag and drop to other object, On delphi.about.com they have samples that can make our DBGrid has the ability to drag and drop but the drag and drop it self is not perfect since every left click on the field will execute the begindrag() function, so the problem is how delphi know when to drag n drop and when is only left click.
According to Mr Google, i stumble on one article on delphi 3000.com which was made by one of borland development team, and it suite just perfect for my need. so here is the code or you guys can take a look your self from the link.

An example of drag and drop between DBGrids - by Borland Developer Support Staff

Technical Information Database

TI1562D.txt - An example of drag and drop between DBGrids

Category :General Programming
Platform :All Windows
Product :All32Bit,

Description:
Title: An example of drag and drop between DBGrids

This sample component and sample project demonstrates an easy way
of enabling drag and drop of an arbitrary field in one data aware
grid onto an arbitrary field in another data aware grid.

1. Launch Delphi x.xx (the code will work in 1 and 2 as well with some
minor changes).

2. Do a File|New|Unit. Take the MyDBGrid unit (below) and paste it
in the newly created unit. Do a File|Save As. Save the unit as
MyDBGrid.pas.

3. Do a Component|Install Component. Switch to the Info New Package
tab. Put MyDBGrid.pas in the Unit file name box. Call the package
MyPackage.dpk. Hit Yes when Delphi tells you that the package
will be built and installed. Hit OK when Delphi tells you that
VCLxx.DPL is needed. The package will now be rebuilt and installed.
You will now find the TMyDBGrid component on your Samples tab on
your component palette. Close the package editor and save the
package.

4. Do a File|New Application. Right click on the form (Form1) and
select View As Text. Take the GridU1 form source (below) and paste
it in Form1. Right click on the form and select View As Form. This
may take a few moments since it's opening up the tables for you.
Take the GridU1 unit (below) and paste it in the unit (Unit1).

5. Do a File|Save Project As. Save the unit as GridU1.pas. Save the
project as GridProj.dpr.

6. Now, run the project and enjoy the dragging and dropping of fields
inbetween or with the two grids.
-----------------
The MyDBGrid unit
-----------------


unit MyDBGrid;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids;

type
TMyDBGrid = class(TDBGrid)
private
{ Private declarations }
FOnMouseDown: TMouseEvent;
protected
{ Protected declarations }
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
published
{ Published declarations }
property Row;
property OnMouseDown read FOnMouseDown write FOnMouseDown;
end;

procedure Register;

implementation

procedure TMyDBGrid.MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Assigned(FOnMouseDown) then
FOnMouseDown(Self, Button, Shift, X, Y);
inherited MouseDown(Button, Shift, X, Y);
end;

procedure Register;
begin
RegisterComponents('Samples', [TMyDBGrid]);
end;

end.

---------------
The GridU1 unit
---------------

unit GridU1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Db, DBTables, Grids, DBGrids, MyDBGrid, StdCtrls;

type
TForm1 = class(TForm)
MyDBGrid1: TMyDBGrid;
Table1: TTable;
DataSource1: TDataSource;
Table2: TTable;
DataSource2: TDataSource;
MyDBGrid2: TMyDBGrid;
procedure MyDBGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure MyDBGrid1DragOver(Sender, Source: TObject;
X, Y: Integer; State: TDragState; var Accept: Boolean);
procedure MyDBGrid1DragDrop(Sender, Source: TObject;
X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
SGC : TGridCoord;

procedure TForm1.MyDBGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
DG : TMyDBGrid;
begin
DG := Sender as TMyDBGrid;
SGC := DG.MouseCoord(X,Y);
if (SGC.X > 0) and (SGC.Y > 0) then
(Sender as TMyDBGrid).BeginDrag(False);
end;

procedure TForm1.MyDBGrid1DragOver(Sender, Source: TObject;
X, Y: Integer; State: TDragState; var Accept: Boolean);
var
GC : TGridCoord;
begin
GC := (Sender as TMyDBGrid).MouseCoord(X,Y);
Accept := Source is TMyDBGrid and (GC.X > 0) and (GC.Y > 0);
end;

procedure TForm1.MyDBGrid1DragDrop(Sender, Source: TObject;
X, Y: Integer);
var
DG : TMyDBGrid;
GC : TGridCoord;
CurRow : Integer;
begin
DG := Sender as TMyDBGrid;
GC := DG.MouseCoord(X,Y);
with DG.DataSource.DataSet do begin
with (Source as TMyDBGrid).DataSource.DataSet do
Caption := 'You dragged "'+Fields[SGC.X-1].AsString+'"';
DisableControls;
CurRow := DG.Row;
MoveBy(GC.Y-CurRow);
Caption := Caption+' to "'+Fields[GC.X-1].AsString+'"';
MoveBy(CurRow-GC.Y);
EnableControls;
end;
end;

end.

---------------
The GridU1 form
---------------

object Form1: TForm1
Left = 200
Top = 108
Width = 544
Height = 437
Caption = 'Form1'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
PixelsPerInch = 96
TextHeight = 13
object MyDBGrid1: TMyDBGrid
Left = 8
Top = 8
Width = 521
Height = 193
DataSource = DataSource1
Row = 1
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
OnDragDrop = MyDBGrid1DragDrop
OnDragOver = MyDBGrid1DragOver
OnMouseDown = MyDBGrid1MouseDown
end
object MyDBGrid2: TMyDBGrid
Left = 7
Top = 208
Width = 521
Height = 193
DataSource = DataSource2
Row = 1
TabOrder = 1
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
OnDragDrop = MyDBGrid1DragDrop
OnDragOver = MyDBGrid1DragOver
OnMouseDown = MyDBGrid1MouseDown
end
object Table1: TTable
Active = True
DatabaseName = 'DBDEMOS'
TableName = 'ORDERS'
Left = 104
Top = 48
end
object DataSource1: TDataSource
DataSet = Table1
Left = 136
Top = 48
end
object Table2: TTable
Active = True
DatabaseName = 'DBDEMOS'
TableName = 'CUSTOMER'
Left = 104
Top = 240
end
object DataSource2: TDataSource
DataSet = Table2
Left = 136
Top = 240
end
end

Diposting oleh admin di 9:39 AM 0 komentar  

Happy New Year

Friday, January 4, 2008

Hi Everybody ,

Year 2007 already past, 2008 is already coming. May in this new year we all will have new improvement on ourself, so can be useful for everybody.

So let me say HAPPY NEW YEAR 2008 :)

Diposting oleh admin di 8:49 AM 1 komentar  

Report Engine

Wednesday, December 19, 2007

A while ago. I got this assignment from my boss for making one report, the content it self was simple and not hard, but i need to get data that was save scattered from several different table
and the process to get it was very very annoying and frustrated.

GUI(Graphical User Interface)

At My place of work, many Delphi programmer here like to display the report on the form ..and to display it they simply make it by them self they put a lot of effort to it,By using TStringGrid,TTreeView,TListView.I was one lazy bastard :P so i think hard on how to save my finger become shorter by typing those line of syntax just to display that report to the form ..in the end i used "Fast Report" it was one of the best Engine Report i have encounter,imagine how much time we can saved just by using this report engine, after we must think how to build the data alignment,we must also think how to build and designed the GUI. As deadline coming on our tail ...

Shorten the story the report was well done ..and boss was happy with my job :$ hahaha...
For those of you that has accustomed using report engine i bet u guys must familiar with Fast Report, it was straight to the point job, and the fr3 file can be hidden in a DFM file so it will not make any trouble on implementing your program, the report it self can be exported to several file like PDF,XLS even with out Adobe Acrobat or Excel it self installed cool right ? :P

So Friends .., If you guys got experience and want to share about report engine please be my guess to put your comment ..

Diposting oleh admin di 4:04 PM 0 komentar  

First Time Posting And Setting

Finally , after thinking for some times...i finally have the gut to make my own web things...even it was only a blog.. :D so here we are ...

Diposting oleh admin di 3:53 PM 0 komentar