Combobox in stringgrid

Hi,

I tested the solution of http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20369496.html and I found the following runtime problem:

1. Click for example the second cell in the second column. Appears the combobox. Select a value in the combobox by clicking. The cell takes over the selected value.
2. Click again on this cell, and appears again in the combobox the earlier selected value. Click somewhere in the table on a cell, the cell get the focus. Then click back on the combox and select an other value. After clicking this value appears in the cell which got the focus.

I'd like to avoid this mistake. How would be possible to prevent this?

Thanks for every comment

wbr Janos
LVL 10
kacorretiredAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mokuleCommented:
Hi,
Modify StringGrid1SelectCell to look like below.
I hope this helps.

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var R: TRect;
begin
  if ((ACol = 1) and (aRow <> 0)) then
  begin
    {Tamaqo y posicisn del ComboBox para llenar la celdilla}
    R := StringGrid1.CellRect(ACol, ARow);
    R.Left := R.Left + StringGrid1.Left;
    R.Right := R.Right + StringGrid1.Left;
    R.Top := R.Top + StringGrid1.Top;
    R.Bottom := R.Bottom + StringGrid1.Top;

    {Mostrar el CombBox}
    with cbInplaceComboBox do
    begin
      Left := R.Left + 1;
      Top := R.Top + 1;
      Width := (R.Right + 1) - R.Left;
      Height := (R.Bottom + 1) - R.Top;

      ItemIndex := Items.IndexOf(StringGrid1.Cells[ACol, ARow]);
      Visible := True;
      SetFocus;
    end;
  end
  else cbInplaceComboBox.Visible := False;
  CanSelect := True;
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mokuleCommented:
Btw
I would suggest to change the first line in form create procedure like below.
It looks better at least for me.

  StringGrid1.DefaultRowHeight := cbInplaceComboBox.Height-1;
Mike LittlewoodEngineerCommented:
To be honest his example looks a little overcomplicated for this task.
This is all I used and it works fine. Im only showing combobox on columns 2 and 3.

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var
  R: TRect;
begin
  if ACol in [2,3] then
  begin
    R := TStringGrid(Sender).CellRect(ACol, ARow);

    R.Left := R.Left + StringGrid1.Left;
    R.Right := R.Right + StringGrid1.Left;
    R.Top := R.Top + StringGrid1.Top;
    R.Bottom := R.Bottom + StringGrid1.Top;

    with ComboBox1 do
    begin
      ItemIndex := Items.IndexOf(StringGrid1.Cells[ACol, ARow]);

      Width := R.Right - R.Left;
      Left  := R.Left + 1;
      Top   := R.Top + 1;
      Visible := True;
    end
  end
  else
    ComboBox1.Visible := False;
end;

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

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
  if ComboBox1.ItemIndex <> -1 then
  begin
    StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row] := ComboBox1.Text;
    StringGrid1.SetFocus;
  end;
end;

kacorretiredAuthor Commented:
thanks guys for your quick comments, I try to test them but I have to be away for 2 days
wbr
Janos
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.