In short I want with the code below was created a package that I created the two components in the component pallet and that the popup menu of the combobox is set to Courier exactly as the value selected in DBSimpleComboBox
unit DBSimpleComponents;
{$mode objfpc}{$H+}
{
* Libreria realizzata da Sammarco Francesco, questa libreria contiene dei componenti che permettono
* di gestire con più facilita' i dati di una query
* Errori constatati: in Linux la combobox non riesce a rendere i componenti del menu a discesa in formato courier
* questo fa in modo che i valori non sono allineati come dovrebbero essere.
* Per contattare l'autore scrivere a francesco.sammarco@gmail.com
}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, DbCtrls, sqldb, StdCtrls, strutils;
const MaxCampi = 50; //numero massimo di campi in un oggetto
function StandardizzaStringaInLunghezza(stringa: string; Lunghezza: integer; DaDove: integer): string;
type
{ TDBSimpleComboBox }
{
Oggetto realizzato da Sammarco Francesco che può contenere più campi di una query all'interno di una combobox
e che restituisce il valore del campo scelto come chiave
}
TDBSimpleComboBox = class(TComboBox)
private
{ Private declarations }
MiaQuery: TSQLQuery;
MiaKey: string;
IndiceValori: integer;
MieiValori: array of string;
IndiceCampi: integer;
MieiCampi: array[0..MaxCampi] of string;
LenCampi: array[0..MaxCampi] of integer;
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner : TComponent); override;
Destructor Distruggi();
procedure SetQuery(Query: TSQLQuery);
procedure SetKey(Key: string);
procedure SetField(Field: string; Lung: integer);
procedure Requery();
function getValueKey(): string;
published
{ Published declarations }
end;
{ TDBSimpleListBox }
{
Oggetto realizzato da Sammarco Francesco che può contenere più campi di una query all'interno di una ListBox
e che restituisce il valore del campo scelto come chiave
}
TDBSimpleListBox = class(TListBox)
private
{ Private declarations }
MiaQuery: TSQLQuery;
MiaKey: string;
IndiceValori: integer;
MieiValori: array of string;
IndiceCampi: integer;
MieiCampi: array[0..MaxCampi] of string;
LenCampi: array[0..MaxCampi] of integer;
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner : TComponent); override;
Destructor Distruggi();
procedure SetQuery(Query: TSQLQuery);
procedure SetKey(Key: string);
procedure SetField(Field: string; Lung: integer);
procedure Requery();
function getValueKey(): string;
function getValueKeyOfIndex(index: integer): string;
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Data Controls',[TDBSimpleComboBox]);
RegisterComponents('Data Controls',[TDBSimpleListBox]);
end;
//funzione che mi standardizza in lunghezza la stringa passata
{ Se DaDove è uguale a 0 allineo la stringa da sinistra altrimenti da destra }
function StandardizzaStringaInLunghezza(stringa: string; Lunghezza: integer; DaDove: integer): string;
var
ret: string;
i: integer;
begin
ret:='';
if length(stringa)>Lunghezza then
begin
ret:=AnsiMidStr(Stringa,1,Lunghezza-1) + '.';
end
else
begin
for i:=1 to (Lunghezza-length(stringa)) do
begin
ret:=ret + ' ' ;
end;
if (DaDove=0) then
ret:=ret + stringa
else
ret:=stringa + ret;
end;
StandardizzaStringaInLunghezza:=ret;
end;
{ TDBSimpleComboBox }
//creo l'oggetto
constructor TDBSimpleComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
IndiceValori:=0;
IndiceCampi:=0;
end;
//Distruggo l'oggetto
destructor TDBSimpleComboBox.Distruggi();
begin
MiaQuery.Free;
end;
//gli dico qualè la query da usare
procedure TDBSimpleComboBox.SetQuery(Query: TSQLQuery);
begin
MiaQuery:=Query;
end;
//questa procedura stabilisce quale campo è considerato come campo chiave
procedure TDBSimpleComboBox.SetKey(Key: string);
begin
MiaKey:=Key;
end;
//Questa procedura permette di stabilire quali sono i campi da visualizzare nella combo box
procedure TDBSimpleComboBox.SetField(Field: string; Lung: integer);
begin
MieiCampi[IndiceCampi]:=Field;
if Lung>0 then
LenCampi[IndiceCampi]:=(-Lung);
IndiceCampi:=IndiceCampi+1;
end;
//Questa procedura permette di stampare nella combobox i dati anche a più colonne
procedure TDBSimpleComboBox.Requery();
var
i: integer;
j: integer;
Campo: string;
lunghezza: integer;
QuantiRecord: integer;
begin
if (length(MiaQuery.SQL.Text)>0) then
begin
Clear;
Font.Name:='courier';
MiaQuery.Open;
//-------------------------
//inizializzo il vettore che contiene la lunghezza massima del campo
for i:=0 to IndiceCampi do
begin
if LenCampi>=0 then
LenCampi:=0;
end;
//-----------------------
// mi recupero le lunghezze dei dati
//e mi recupero il numero di record della query
//non uso la MiaQuery.RecordCount perchè non mi ritorna il valore corretto
if not MiaQuery.EOF then
begin
MiaQuery.First;
QuantiRecord:=0;
while not MiaQuery.EOF do
begin
i:=0;
repeat
lunghezza:=length(Trim(MiaQuery.FieldByName(MieiCampi).Text));
if ((lunghezza>LenCampi)AND(LenCampi>=0)) then
LenCampi:=lunghezza;
i:=i+1;
until (i>=IndiceCampi);
QuantiRecord:=QuantiRecord+1;
MiaQuery.Next;
end;
MiaQuery.First;
end;
//-----------------------
SetLength(MieiValori, QuantiRecord);
//-----------------------
// mi recupero i dati
if not MiaQuery.EOF then
begin
MiaQuery.First;
j:=0;
while not MiaQuery.EOF do
begin
i:=0;
Campo:='';
repeat
If i=0 then
Campo:=Campo + StandardizzaStringaInLunghezza(Trim(MiaQuery.FieldByName(MieiCampi).Text),Abs(LenCampi),1)
else
{non uso il carattere ^I perchè in Windows mi stampa anche un carattere brutto che non voglio, così uso 3 spazi }
Campo:=Campo + ' ' + StandardizzaStringaInLunghezza(Trim(MiaQuery.FieldByName(MieiCampi).Text),Abs(LenCampi),1);
i:=i+1;
until (i>=IndiceCampi);
MieiValori[j]:=MiaQuery.FieldByName(MiaKey).Text;
Items.Add(Trim(Campo));
j:=j+1;
MiaQuery.Next;
end;
end;
MiaQuery.Close;
end;
end;
//Questa funzione permette di restituire il valore della campo considerato chiave
function TDBSimpleComboBox.getValueKey(): string;
begin
if (ItemIndex>=0) then
getValueKey:=MieiValori[ItemIndex];
end;
//------------------------------------------------------------------------------------------------------------
//da qua in poi ci sono solo i dati dell'oggetto memo
//funzione che mi standardizza in lunghezza la stringa passata
{ TDBSimpleMemo }
//creo l'oggetto
constructor TDBSimpleListBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
IndiceValori:=0;
IndiceCampi:=0;
end;
//Distruggo l'oggetto
destructor TDBSimpleListBox.Distruggi();
begin
MiaQuery.Free;
end;
//gli dico qualè la query da usare
procedure TDBSimpleListBox.SetQuery(Query: TSQLQuery);
begin
MiaQuery:=Query;
end;
//questa procedura stabilisce quale campo è considerato come campo chiave
procedure TDBSimpleListBox.SetKey(Key: string);
begin
MiaKey:=Key;
end;
//Questa procedura permette di stabilire quali sono i campi da visualizzare nella combo box
procedure TDBSimpleListBox.SetField(Field: string; Lung: integer);
begin
MieiCampi[IndiceCampi]:=Field;
if Lung>0 then
LenCampi[IndiceCampi]:=(-Lung);
IndiceCampi:=IndiceCampi+1;
end;
//Questa procedura permette di stampare nella combobox i dati anche a più colonne
procedure TDBSimpleListBox.Requery();
var
i: integer;
j: integer;
Campo: string;
lunghezza: integer;
QuantiRecord: integer;
begin
if (length(MiaQuery.SQL.Text)>0) then
begin
Clear;
Font.Name:='courier';
MiaQuery.Open;
//-------------------------
//inizializzo il vettore che contiene la lunghezza massima del campo
for i:=0 to IndiceCampi do
begin
if LenCampi>=0 then
LenCampi:=0;
end;
//-----------------------
// mi recupero le lunghezze dei dati
//e mi recupero il numero di record della query
//non uso la MiaQuery.RecordCount perchè non mi ritorna il valore corretto
if not MiaQuery.EOF then
begin
MiaQuery.First;
QuantiRecord:=0;
while not MiaQuery.EOF do
begin
i:=0;
repeat
lunghezza:=length(Trim(MiaQuery.FieldByName(MieiCampi).Text));
if ((lunghezza>LenCampi)AND(LenCampi>=0)) then
LenCampi:=lunghezza;
i:=i+1;
until (i>=IndiceCampi);
QuantiRecord:=QuantiRecord+1;
MiaQuery.Next;
end;
MiaQuery.First;
end;
//-----------------------
SetLength(MieiValori, QuantiRecord);
//-----------------------
// mi recupero i dati
if not MiaQuery.EOF then
begin
MiaQuery.First;
j:=0;
while not MiaQuery.EOF do
begin
i:=0;
Campo:='';
repeat
If i=0 then
Campo:=Campo + StandardizzaStringaInLunghezza(Trim(MiaQuery.FieldByName(MieiCampi).Text),Abs(LenCampi),1)
else
{non uso il carattere ^I perchè in Windows mi stampa anche un carattere brutto che non voglio, così uso 3 spazi }
Campo:=Campo + ' ' + StandardizzaStringaInLunghezza(Trim(MiaQuery.FieldByName(MieiCampi).Text),Abs(LenCampi),1);
i:=i+1;
until (i>=IndiceCampi);
MieiValori[j]:=MiaQuery.FieldByName(MiaKey).Text;
Items.Add(Trim(Campo));
j:=j+1;
MiaQuery.Next;
end;
end;
MiaQuery.Close;
end;
end;
//Questa funzione permette di restituire il valore della campo considerato chiave
function TDBSimpleListBox.getValueKey(): string;
begin
if (ItemIndex>=0) then
getValueKey:=MieiValori[ItemIndex];
end;
//Questa funzione permette di restituire il valore della campo considerato chiave
function TDBSimpleListBox.getValueKeyOfIndex(index: integer): string;
begin
if (Index>=0) then
getValueKeyOfIndex:=MieiValori[Index];
end;
end.