Re: StringGrid next row

Posted by Aulia Setiawan on September 02, 2008

In Reply to Re: StringGrid next row posted by Kelly Sanders P16701 on August 30, 2008

: : I have a problem with StringGrid. How to move the pointer from row to row, when I pressed ENTER BUTTON
: : Best Regards
: :
: : Aulia Setiawan

: The Delphi StringGrid goes to the next row, when you press KEY DOWN. So you have to replace an ENTER key with a KEY DOWN key, when a key is pressed and the stringgrid has focus.
: So you have to set up an event handler for KeyPress, check if the key pressed was ENTER, if yes: simulate an "arrow down" key press.
: If this answer doesn't help you, reply me in the DelphiLand forum and I'll post some more detailed stuff :)

Dear Ms Kelly Sanders, Could post some more deatail about "The Delphi StringGrid goes to the next row by press ENTER". I inform you that on the stringgrid I put a TextEdit and ComboBox Object. My Code like this:

procedure TForm1.FormActivate(Sender: TObject);
begin
  with grdScr do begin
    SetFocus;
    ColWidths[0] := 15;
    Cols[0].Add('#');
    ColWidths[1] := 150;
    Cols[1].Add('               Kode');
    ColWidths[2] := 247;
    Cols[2].Add('               Nama Barang');
    ColWidths[3] := 38;
    Cols[3].Add('  Qty');
    ColWidths[4] := 60;
    Cols[4].Add('  Satuan');
    ColWidths[5] := 80;
    Cols[5].Add('     Harga');
    ColWidths[6] := 70;
    Cols[6].Add('    Disc%');
    ColWidths[7] := 95;
    Cols[7].Add('      Jumlah');
  end;
end;

procedure TForm1.grdScrKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
  if (Key = VK_ESCAPE) then Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  grdScr.DefaultRowHeight := ComboBox1.Height;
end;

procedure TForm1.grdScrDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
  R:TRect;
begin
  if (ACol = 1)and
     (ARow = grdScr.Row) then
    with Edit1 do begin
      SetFocus;
      BringToFront;
      CopyRect(R, Rect);
      R.TopLeft := Form1.ScreenToClient(grdScr.ClientToScreen(R.TopLeft));
      R.BottomRight := Form1.ScreenToClient(grdScr.ClientToScreen(R.BottomRight));
      SetBounds(R.Left,R.Top,R.Right - R.Left,R.Bottom - R.Top);
    end;
  if (ACol = 2) and
     (ARow = grdScr.Row) then
    with ComboBox1 do begin
      BringToFront;
      CopyRect(R,Rect);
      R.TopLeft := Form1.ScreenToClient(grdScr.ClientToScreen(R.TopLeft));
      R.BottomRight := Form1.ScreenToClient(grdScr.ClientToScreen(R.BottomRight));
      SetBounds(R.Left,R.Top,R.Right - R.Left,R.Bottom - R.Top);
    end;
end;


procedure TForm1.grdScrTopLeftChanged(Sender: TObject);
var
  R:TRect;
begin
  with grdScr do
    CopyRect(R,CellRect(Col,Row));
  with Edit1 do begin
    Visible := false;
    R.TopLeft := Form1.ScreenToClient(grdScr.ClientToScreen(R.TopLeft));
    R.BottomRight := Form1.ScreenToClient(grdScr.ClientToScreen(R.BottomRight));
    SetBounds(R.Left,R.Top,R.Right - R.Left,R.Bottom - R.Top);
  end;
  with ComboBox1 do begin
    Visible := false;
    R.TopLeft := Form1.ScreenToClient(grdScr.ClientToScreen(R.TopLeft));
    R.BottomRight := Form1.ScreenToClient(grdScr.ClientToScreen(R.BottomRight));
    SetBounds(R.Left,R.Top,R.Right - R.Left,R.Bottom - R.Top);
  end;
  with grdScr do
    if (TopRow <> Row) then begin
      Edit1.Show;
      ComboBox1.Show;
    end;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  with grdScr do Cells[2,Row] := ComboBox1.Text;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  with grdScr do Cells[1,Row] := Edit1.Text;
end;

procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if(Key = VK_ESCAPE)then Close;
  if(Key = VK_RETURN)then
    begin
      // ?
    end;
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  myRect: TGridRect;
  R:TRect;
begin
  if (Key = VK_ESCAPE) then Close;
  if (Key = VK_RETURN) then
    begin
      // ?
    end;
end;

Related articles

       

Follow Ups