Dbgird focus column lazarus / 16-Databases ---------------------------------------------------------------------- eldonfsr · 2012-12-16 03:19 Hello friend just one week ago i download the new version of lazarus 1.0.4, i started to do some test working well, but today i tried with database sqlite3 conection well but later when i running a project with dbgrid and try to focus on cell dont respond always go to next colum, some body know if some bug on this version. here is my code, i hope some body can help me. after finish type on column 1 on event onColexit i have to check if record exist or no and send message but the focus no return to first colum always go to next. this is the envent. procedure TFormtickes.DBGDTickColExit(Sender: TObject; Column : Tcolumn; aCol, aRow :Integer ); begin   if( DbgDTick.SelectedIndex = 0) and  (DM.SQLQTemp.State = dsInsert) then    begin       if Not (DM.SQLQTemp.FieldByName('cve_articulo').AsString = '') and (DM.SQLQTemp.FieldByName('nomart').AsString = '') then         if( DM.SQLQProd.Locate('NumArticulo',DM.SQLQTemp.FieldByName(' cve_articulo').AsString,[]) = true) then          begin           DM.SQLQTemp.FieldByName('Numtick').AsInteger:= StrtoInt(DBETickNum.Text);           DM.SQLQTemp.FieldByName('cve_articulo').AsString:=DM.SQLQPro d.FieldByName('NumArticulo').AsString;           DM.SQLQTemp.FieldByName('Nomart').AsString:=DM.SQLQProd.Fiel dByName('descripcion').AsString;           DM.SQLQTemp.FieldByName('Precio').AsInteger :=DM.SQLQProd.FieldByName('preciovta').AsInteger;          end       else       begin          ShowMessage('Articulo no Encontrado');          DbgDTick.SelectedIndex:=0;   //        DBGDTick.SelectedField:=DBGDTick.Columns[0].Field;           DBGDtick.SelectedColumn.Index:=0;          DBGDTick.SetFocus;  //        DBGDTick.SelectedField:=Column.Field.FieldName;       end;    end;    if( Acol=1) and not (DM.SQLQTemp.State = dsInsert) then    begin       if Not (DM.SQLQTemp.FieldByName('cve_articulo').AsString = '') and (DM.SQLQTemp.FieldByName('nomart').AsString = '') then         if( DM.SQLQCli.Locate('codigo',DM.SQLQTemp.FieldByName('cve_ar ticulo').AsString,[loCaseInsensitive,loPartialKey]) = true) then           DM.SQLQTemp.Insert;           DM.SQLQTemp.FieldByName('Numtick').AsInteger:= StrtoInt(DBETickNum.Text);           DM.SQLQTemp.FieldByName('cve_articulo').AsString:=DM.SQLQPro d.FieldByName('NumArticulo').AsString;           DM.SQLQTemp.FieldByName('Nomart').AsString:=DM.SQLQProd.Fiel dByName('descripcion').AsString;           DM.SQLQTemp.FieldByName('Precio').AsInteger :=DM.SQLQProd.FieldByName('preciovta').AsInteger;    end; end;                  Code: [Select]unit formticks; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,   DbCtrls, EditBtn, ExtCtrls, DBGrids, DB, LCLType,strutils; type   { TFormtickes }   TDBGridExt = class(TDBGrid);   TFormtickes = class(TForm)     DateEFecha: TDateEdit;     DBETickNum: TDBEdit;     DBENombre: TDBEdit;     DBEDir: TDBEdit;     DBECol: TDBEdit;     DBEMun: TDBEdit;     DBGDTick: TDBGrid;     DBGCLI: TDBGrid;     DBgArts: TDBGrid;     EBCli: TEditButton;     GroupBox1: TGroupBox;     Label1: TLabel;     Label2: TLabel;     Label5: TLabel;     procedure DBgArtsDblClick(Sender: TObject);     procedure DBgArtsKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState       );     procedure DBgArtsKeyPress(Sender: TObject; var Key: char);     procedure DBGCLIColEnter(Sender: TObject);     procedure DBGCLIDblClick(Sender: TObject);     procedure DBGCLIKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);     procedure DBGCLIKeyPress(Sender: TObject; var Key: char);     procedure DBGDTickColExit(Sender: TObject;Column : Tcolumn; aCol, aRow :Integer);     procedure DBGDTickEditButtonClick(Sender: TObject);     procedure DBGDTickEditingDone(Sender: TObject);     procedure DBGDTickKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState       );     procedure DBGDTickKeyPress(Sender: TObject; var Key: char;Column: TColumn; aRow : Integer);       procedure DBGDTickSelectEditor(Sender: TObject; Column: TColumn; aRow : Integer;       var Editor: TWinControl);     procedure EBCliButtonClick(Sender: TObject);     procedure EBCliEditingDone(Sender: TObject);     procedure EBCliExit(Sender: TObject);     procedure FormShow(Sender: TObject);     procedure TicketsSelectEditor(Sender: TObject; aCol, aRow: Integer;       var Editor: TWinControl);   private     { private declarations }     searchArt:String;     searchcli:string;     procedure PosicionarEdit(AGrid: TDBGrid);   public     { public declarations }   end; var   Formtickes: TFormtickes; implementation uses DataMod, formctrvtas,formMain; {$R *.lfm} { TFormtickes } procedure TFormtickes.FormShow(Sender: TObject); Var Sqltext: String; begin   DBGCli.Hide;   if DM.SQLQCia.Active=false then DM.SQLQCia.Active:=true;   if DM.SQLQCia.FieldByName('cliactual').AsString = '' then   begin     DM.SQLQCli.Active:=true;     DM.SQLQProd.Active:=true;     DM.SQLQDetTick.Active:=true;     DM.SQLQTickes.FieldByName('Ticket_Folio').AsInteger:= DM.SQLQCia.FieldByName('tickescons').AsInteger+1;   end;   DM.SQL3Conc.ExecuteDirect('CREATE TEMP TABLE TempTickets'+       '(NumTick NUMERIC(6), cve_articulo varchar(30) ,NOMART varchar(200), CANTIDAD numeric(9,2), Precio Numeric(8,2),Importe Numeric(8,2),STATUS varchar(1) ,RENGLON numeric(2) ,NUMREG numeric(6) )' );      DM.SQLQTemp.SQL.Text:='select * from TempTickets';   Sqltext := DM.SQLQProd.SQL.GetText;   if DM.SQLQDetTick.RecordCount > 0 then   begin      while not DM.SQLQDetTick.EOF do       begin       DM.SQLQTemp.SQL.Text := 'insert into TempTickets (NumTick,Cve_Articulo, NomArt , Cantidad ,Precio,Status , Renglon, NumRec ) values (:IDTick,:CvEArt, :DescArt,:Cant , :Prec ,:Imp ,:Estado, :Reng, :Recno);';       DM.SQLQTemp.Params.ParamByName('IDTICK').AsInteger := DM.SQLQDetTick.FieldByName('Ticket_Folio').AsInteger;       DM.SQLQTemp.Params.ParamByName('CVEART').AsString := DM.SQLQDetTick.FieldByName('cve_articulo').AsString;       DM.SQLQTemp.Params.ParamByName('CANT').AsInteger := DM.SQLQDetTick.FieldByName('cantidad').AsInteger;       DM.SQLQTemp.Params.ParamByName('Estado').AsString := DM.SQLQDetTick.FieldByName('Status').AsString;       DM.SQLQTemp.Params.ParamByName('Recno').AsInteger := DM.SQLQDetTick.FieldByName('RowId').AsInteger;       DM.SQLQTemp.Params.ParamByName('Prec').AsInteger := DM.SQLQDetTick.FieldByName('Precio').AsInteger;       DM.SQLQTemp.Params.ParamByName('Imp').AsFloat := (DM.SQLQDetTick.FieldByName('Precio').AsInteger *  DM.SQLQDetTick.FieldByName('Cantidad').AsInteger);       DM.SQLQProd.SQL.Text:='select * from productos where numarticulo = '''+DM.SQLQDetTick.FieldByName('cve_articulo').AsString +'';       DM.SQLQProd.ExecSQL;       if DM.SQLQProd.RecordCount > 0 then       begin         DM.SQLQTemp.Params.ParamByName('DESCART').AsString := DM.SQLQProd.FieldByName('descripcion').AsString;       end;       DM.SQLQTemp.ExecSQL;       DM.SQLQDetTick.Next;       end;   end;   DM.SQLQTemp.Active:=true;   DM.Sqlqprod.Active:=false;   DM.SQLQProd.SQL.Text:=Sqltext;   DM.SQLQProd.Active :=true;   DbeNombre.Enabled:=false;   DbeDir.Enabled:=false;   DbeCol.Enabled:=false;   DbeMun.Enabled:=false;   FormTickes.Top := FormaMain.Top +120;   FormTickes.Left := FormaMain.Left +60; end; procedure TFormtickes.EBCliButtonClick(Sender: TObject); var P:TPoint; begin   //if not assigned(formaselcli) then   //   application.CreateForm(Tformaselcli, formaselcli);   //   P.X :=(EBcli.Top  +  formTickes.Top )-(EBCLi.Height+4);   //   P.Y:= (EBcli.Left + EBcli.Width)-(EBCLi.Height+90) ;   //   P:= ClientToScreen(P);   //   formaselcli.Left:=P.Y ;   //   formaselcli.Top:=P.X;   //   formaselcli.show;   DbGCli.Left:=80;   DbGCli.Top:=70;   DbgCli.Visible:=true;   DbgCli.SetFocus; end; procedure TFormtickes.EBCliEditingDone(Sender: TObject); begin     DbGDTick.SetFocus; end; procedure TFormtickes.EBCliExit(Sender: TObject); begin end; procedure TFormtickes.DBGCLIKeyPress(Sender: TObject; var Key: char); begin   if( Key = #13 )then    begin      EBCli.Text:= DM.SQLQCli.FieldByName('codigo').AsString;      EBCli.SetFocus;      DbGCli.Hide;    end;   if (Key in ['a'..'z','0'..'9']) then;   begin       searchart:= searchart+key;       with DM.SQLQCli do          Locate('nombre',searchcli, [loCaseInsensitive,         loPartialKey]);   end;   if (Key=#8 ) then    begin       searchart := midstr(searchcli,0,length(searchcli)-2);       with DM.SQLQCli do          Locate('nombre',searchcli, [loCaseInsensitive,         loPartialKey]);    end; end; procedure TFormtickes.DBGDTickColExit(Sender: TObject; Column : Tcolumn; aCol, aRow :Integer ); begin   if( DbgDTick.SelectedIndex = 0) and  (DM.SQLQTemp.State = dsInsert) then    begin       if Not (DM.SQLQTemp.FieldByName('cve_articulo').AsString = '') and (DM.SQLQTemp.FieldByName('nomart').AsString = '') then         if( DM.SQLQProd.Locate('NumArticulo',DM.SQLQTemp.FieldByName(' cve_articulo').AsString,[]) = true) then          begin           DM.SQLQTemp.FieldByName('Numtick').AsInteger:= StrtoInt(DBETickNum.Text);           DM.SQLQTemp.FieldByName('cve_articulo').AsString:=DM.SQLQPro d.FieldByName('NumArticulo').AsString;           DM.SQLQTemp.FieldByName('Nomart').AsString:=DM.SQLQProd.Fiel dByName('descripcion').AsString;           DM.SQLQTemp.FieldByName('Precio').AsInteger :=DM.SQLQProd.FieldByName('preciovta').AsInteger;          end       else       begin          ShowMessage('Articulo no Encontrado');          DbgDTick.SelectedIndex:=0;   //        DBGDTick.SelectedField:=DBGDTick.Columns[0].Field;           DBGDtick.SelectedColumn.Index:=0;          DBGDTick.SetFocus;  //        DBGDTick.SelectedField:=Column.Field.FieldName;       end;    end;    if( Acol=1) and not (DM.SQLQTemp.State = dsInsert) then    begin       if Not (DM.SQLQTemp.FieldByName('cve_articulo').AsString = '') and (DM.SQLQTemp.FieldByName('nomart').AsString = '') then         if( DM.SQLQCli.Locate('codigo',DM.SQLQTemp.FieldByName('cve_ar ticulo').AsString,[loCaseInsensitive,loPartialKey]) = true) then           DM.SQLQTemp.Insert;           DM.SQLQTemp.FieldByName('Numtick').AsInteger:= StrtoInt(DBETickNum.Text);           DM.SQLQTemp.FieldByName('cve_articulo').AsString:=DM.SQLQPro d.FieldByName('NumArticulo').AsString;           DM.SQLQTemp.FieldByName('Nomart').AsString:=DM.SQLQProd.Fiel dByName('descripcion').AsString;           DM.SQLQTemp.FieldByName('Precio').AsInteger :=DM.SQLQProd.FieldByName('preciovta').AsInteger;    end; end; procedure TFormtickes.DBGDTickEditButtonClick(Sender: TObject); begin     DBGArts.Top:= DBGDTick.top+(DBGDTick.DefaultRowHeight * 2)+ 4;     DbGArts.Left:= DbGDTick.Left+12;     DBGArts.Width:=380;     DBGArts.Height:=140;     DbGArts.visible:=true;     DBGArts.SetFocus; end; procedure TFormtickes.DBGDTickEditingDone(Sender: TObject); begin end; procedure TFormtickes.DBGDTickKeyDown(Sender: TObject; var Key: Word;   Shift: TShiftState); begin   if key = VK_INSERT then    begin      DM.SQLQTemp.Insert;    end; end; procedure TFormtickes.DBGDTickKeyPress(Sender: TObject; var Key: char;Column: TColumn; aRow : Integer); begin  if (Column.DesignIndex=0) or ( Arow =0) then   begin       showmessage('Aqui paso');   end; end;  procedure TFormtickes.DBGDTickSelectEditor(Sender: TObject; Column: TColumn; aRow: Integer;   var Editor: TWinControl); begin //  showmessage(Column.FieldName); //  if (DM.SQLQTemp.FieldByName('cve_articulo').AsString ='' ) then DM.SQLQTemp.Insert; //  if (Column.FieldName='cve_articulo') then //   begin //   if (DM.SQLQTemp.State = dsInsert) or (DM.SQLQTemp.State = dsEdit )then //    begin //     PosicionarEdit(DbGDTick) ; //     Editor:= EBArticulo; //     EBArticulo.Visible:=true; //     EBArticulo.SetFocus; //    end; //   end; end; procedure TFormtickes.DBGCLIColEnter(Sender: TObject); begin     EBCli.Text:= DM.SQLQCli.FieldByName('codigo').AsString;     EBCli.SetFocus;     DbGCli.Hide; end; procedure TFormtickes.DBgArtsDblClick(Sender: TObject); begin   if (DM.SQLQTemp.FieldByName('cve_articulo').AsString  = '' ) then    begin       DM.SQLQTemp.Insert;       DM.SQLQTemp.FieldByName('Numtick').AsInteger:= StrtoInt(DBETickNum.Text);       DM.SQLQTemp.FieldByName('cve_articulo').AsString:=DM.SQLQProd.Fi eldByName('NumArticulo').AsString;       DM.SQLQTemp.FieldByName('Nomart').AsString:=DM.SQLQProd.FieldByN ame('descripcion').AsString;       DM.SQLQTemp.FieldByName('Precio').AsInteger :=DM.SQLQProd.FieldByName('preciovta').AsInteger; //      DM.SQLQTemp.SQL.Text := 'insert into TempTickets (NumTick,Cve_Articulo, NomArt ) values (:IDTick,:CvEArt, :DescArt);'; //      DM.SQLQTemp.Params.ParamByName('IDTICK').AsInteger := DM.SQLQDetTick.FieldByName('Ticket_Folio').AsInteger; //      DM.SQLQTemp.Params.ParamByName('CVEART').AsString := DM.SQLQProd.FieldByName('NumArticulo').AsString; //      DM.SQLQTemp.Params.ParamByName('DescArt').AsString :=DM.SQLQProd.FieldByName('descripcion').AsString; //      DM.SQLQTemp.ExecSQL;       DBGArts.Hide;       DBGDTick.SetFocus;    end   else   begin   if not (DM.SQLQTemp.FieldbyName('cve_articulo').AsString = '' ) then    begin     DM.SQLQTemp.Edit;     DM.SQLQTemp.FieldByName('cve_articulo').AsString:=DM.SQLQProd.Fiel dByName('NumArticulo').AsString;     DM.SQLQTemp.FieldByName('Nomart').AsString:=DM.SQLQProd.FieldByNam e('descripcion').AsString;     DM.SQLQTemp.FieldByName('Precio').AsInteger :=DM.SQLQProd.FieldByName('preciovta').AsInteger;     DBGArts.Hide;     DBGDTick.SetFocus;    end;   end; end; procedure TFormtickes.DBgArtsKeyDown(Sender: TObject; var Key: Word;   Shift: TShiftState); begin   if( Key = 13 )then   if (DM.SQLQTemp.FieldByName('cve_articulo').AsString  = '' ) then    begin       DM.SQLQTemp.Insert;       DM.SQLQTemp.FieldByName('Numtick').AsInteger:= StrtoInt(DBETickNum.Text);       DM.SQLQTemp.FieldByName('cve_articulo').AsString:=DM.SQLQProd.Fi eldByName('NumArticulo').AsString;       DM.SQLQTemp.FieldByName('Nomart').AsString:=DM.SQLQProd.FieldByN ame('descripcion').AsString;       DM.SQLQTemp.FieldByName('Precio').AsInteger :=DM.SQLQProd.FieldByName('preciovta').AsInteger;       DBGArts.Hide;       DBGDTick.SetFocus;    end   else   if not (DM.SQLQTemp.FieldbyName('cve_articulo').AsString = '' ) then    begin     DM.SQLQTemp.Edit;     DM.SQLQTemp.FieldByName('cve_articulo').AsString:=DM.SQLQProd.Fiel dByName('NumArticulo').AsString;     DM.SQLQTemp.FieldByName('Nomart').AsString:=DM.SQLQProd.FieldByNam e('descripcion').AsString;     DM.SQLQTemp.FieldByName('Precio').AsInteger :=DM.SQLQProd.FieldByName('preciovta').AsInteger;     DBGArts.Hide;     DBGDTick.SetFocus;    end; end; procedure TFormtickes.DBgArtsKeyPress(Sender: TObject; var Key: char); begin   if (Key in ['a'..'z','0'..'9']) then;   begin       searchart:= searchart+key;       with DM.SQLQProd do          Locate('descripcion',searchart, [loCaseInsensitive,         loPartialKey]);   end;   if (Key=#8 ) then    begin       searchart := midstr(searchart,0,length(searchart)-2);       with DM.SQLQProd do          Locate('descripcion',searchart, [loCaseInsensitive,         loPartialKey]);    end; end; procedure TFormtickes.DBGCLIDblClick(Sender: TObject); begin    EBCli.Text:= DM.SQLQCli.FieldByName('codigo').AsString;    EBCli.SetFocus;    DbGCli.Hide; end; procedure TFormtickes.DBGCLIKeyDown(Sender: TObject; var Key: Word;   Shift: TShiftState); begin   if (key = 13) then   begin    EBCli.Text:= DM.SQLQCli.FieldByName('codigo').AsString;    EBCli.SetFocus;    DbGCli.Hide;   end; end; procedure TFormTickes.PosicionarEdit(AGrid: TDBGrid); var   i,L: Integer;   P: TPoint; begin   with TDBGridExt(AGrid) do   begin     L:= ColWidths[0];     for i:= 1 to Col-1 do L:= L + ColWidths[i];     P.X:= L + 4;     P.Y:= RowHeights[0] + Row * DefaultRowHeight + 4;  //   EBArticulo.BoundsRect:= CellRect(Col, Row);     DBGArts.Top:= DBGDTick.top+(RowHeights[0] + Row * DefaultRowHeight + 4);     DbGArts.Left:= DbGDTick.Left+L;   end; end;  procedure TFormtickes.TicketsSelectEditor(Sender: TObject; aCol, aRow: Integer;   var Editor: TWinControl); begin   if (aCol=0) and (aRow >0) then    begin //      dpCal.BoundsRect := sgTrans.CellRect(aCol,aRow); //      sgTrans.Cells[sgTrans.Col, sgTrans.Row] :=DateToStr(dpCal.TheDate) ; //      Editor := dpCal;    end; end; end. ---------------------------------------------------------------------- jesusr · 2012-12-17 15:29 I'm very surprised to confirm that this works. Lazarus should do something so it alerts the programmer about this wrong doing. Well, let me explain, OnColExit for the DbGrid is just a notification, TNotifyEvent, that occurs when you are selecting another column, when this event is triggered, selection has not yet changed and DbGrid.SelectedColumn is still the old one. TNotifyEvent has this signature: Code: [Select]procedure(Sender: TObject) of object;according to your code your OnColExit has this signature: Code: [Select]DBGDTickColExit(Sender: TObject; Column : Tcolumn; aCol, aRow :Integer );if you try to do this in code: Code: [Select]DBGTick.OnColExit := @DBGDTickColExitThe compiler will complain that event signatures do not match (the exact error message will be: TheFile.pas(Line,Col) Error: Incompatible types: got "" expected "" ) But...., as you probably are using this project from an older version, you are not doing this in code, the OnColExit event is being set up from the form lfm file. In your case when the OnColExit event is triggered, you are getting totally invalid, bogus, Column and aCol/aRow values and using them will produce unexpected behaviour. From the compiler side, everything looks normal, but it is not, and Lazarus should warn that this is wrong. So to fix the problem you will have to delete the invalid events and recode your routines so they use DbGrid.SelectedColumn or friends. ---------------------------------------------------------------------- eldonfsr · 2012-12-18 04:08 all Right Jesus, as you say this was a project i left for some time in lazarus 9.29, but i saw the new version of lazarus and i tried to compile and start to work on it, but some things worked before now i have problems, i made change as you say and all is working better, thanks a lot for you help. thanks. thanks.