Recent

Author Topic: New ComboBox e ListBox Component to DB  (Read 9469 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
New ComboBox e ListBox Component to DB
« on: February 12, 2010, 01:58:07 pm »
Hello, I created two objects, and will publish them soon so that everyone can use it if they need it that is a combobox that allows you to inherit values from a TSQLQuery (allows to display up to 50 fields) and each row is assigned a field object query that can be read by a very simple function. The same thing I did for the ListBox. Now my only problem is that everything works on windows regularly, on ubuntu linux no. This is because in linux ubuntu on the combobox I can not aligned on values because I can not set the dropdown that contains the values of the combo with a style courier. Does anyone know how? And then I explained how to put these items in a package LPK? Thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: New ComboBox e ListBox Component to DB
« Reply #1 on: February 12, 2010, 05:29:58 pm »
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.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: New ComboBox e ListBox Component to DB
« Reply #2 on: February 15, 2010, 08:05:11 am »
No one can help me?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: New ComboBox e ListBox Component to DB
« Reply #3 on: February 15, 2010, 01:42:31 pm »
Ok I managed to get the package with the components, now my problem is to change the font menu popup TDBSimpleComboBox so that is of a courier, so well aligned values to display in TDBSimpleComboBox in case of multiple fields . Why in Ubuntu I tried to play with the newly created object and the caption of TDBSimpleComboBox is formed by courier

TDBSimpleComboBox.Font.Name = 'courier';

while the contents of the pop up menu of TDBSimpleComboBox is in another format, mind on windows if required:

TDBSimpleComboBox.Font.Name = 'courier';

also the content of the popup menu TDBSimpleComboBox format is courier.
If I can do this online I put the package available to everyone!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018