StringGrid - autouzupełnianie wpisów

jozkan

Chcemy aby tabela była edytowalna więc StringGrid.Options goEditing ustawiamy na True. Podczas wypełniania komórek tabeli procedura AutoUzupelnij przeszuka wskazany zakres komórek i uzupełni nasz wpis. Efekt podobny do zastosowanego w Excelu

SG - StringGrid w którym wprowadzamy dane
SG_Source - StringGrid z którego bierzemy dane do porównania z tym co wpisujemy (być może SG=SG_Source)
zakres_source - zakres komórek z których bierzemy dane do porównania z tym co wpisujemy

procedure SetStringGridSelText(SG:TWinControl;SelStart,SelLength:Integer);
var
  Wnd:Integer;
begin
  Wnd:=GetWindow(SG.Handle,GW_CHILD);
  SendMessage(Wnd,EM_SETSEL,SelStart,SelStart+SelLength);
  SG.SetFocus;
end;

//w OnKeyUp StringGrida
procedure AutoUzupelnij(SG,SG_Source:TStringGrid;zakres_source:TRect;Key:Word);
var
   tekst_pisany,tekst_source:string;
   aCol,aRow:Integer;
   traf:Boolean;
begin
   if (Key=Vk_Back)or(Key=Vk_Delete)or(Key=Vk_Up)or(Key=Vk_Down)
      or(Key=Vk_Left)or(Key=Vk_Right)or(Key=Vk_Home)or(Key=Vk_End)
      or(Key=Vk_Shift)or(Key=Vk_Control)or(Key=Vk_Return)or(Key=Vk_Menu)
   then Exit;
   traf:=False;
   tekst_pisany:=SG.Cells[SG.Col,SG.Row];
   for aCol:=zakres_source.Left to zakres_source.Right do
   begin
      if traf then Break;
      traf:=False;
      for aRow:=zakres_source.Top to zakres_source.Bottom do
      begin
         if  (SG=SG_Source)and(aCol=SG.Col)and(aRow=SG.Row) then
         tekst_source:='' else
         tekst_source:=SG_Source.Cells[aCol,aRow];
         if tekst_source<>'' then
         begin
            if Length(tekst_source)&gt;=Length(tekst_pisany) then
            begin
               if Pos(tekst_pisany,tekst_source)=1 then
               begin
                  Delete(tekst_source,1,Length(tekst_pisany));
                  SG.Cells[SG.Col,SG.Row]:=tekst_pisany+tekst_source;
                  SetStringGridSelText(SG,Length(tekst_pisany),Length(tekst_source));
                  traf:=True;
                  Break;
               end;
            end;
         end;
      end;
   end;
end;
FAQ

1 komentarz