Solved

TStringGrid ans TComboBoxes

Posted on 2002-04-29
2
178 Views
Last Modified: 2010-04-04
Hey!

Any hint on how I can put a combobox in a specific cell of a StringGrid ?

I know how to create at runtime a combobox which uses the StringGrid as parent but I want it to kind of use one specific cell as parent (when I scroll, for example, the combobox follows the cell)

Any help ?
0
Comment
Question by:qas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
MBo earned 50 total points
ID: 6979295
Assume sg1- stringgrid
       cb1 - combobox (drop it in design time after stringgrid (to avoid bringtofront) and make visible=false;)

procedure TForm1.sg1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var
  R: TRect;
begin
  if ARow =1 then     begin
      R := SG1.CellRect(ACol, ARow);
      cb1.left := R.Left+ SG1.Left+1;
      cb1.top := R.Top + sg1.top+1;
      cb1.Width := sg1.ColWidths[acol];
      cb1.Tag:=ACol+65536*ARow;//remember cell
      cb1.Visible := True;
      cb1.SetFocus;
    end;
  CanSelect := True;
end;

procedure TForm1.cb1Change(Sender: TObject);
begin
SG1.Cells[cb1.Tag mod 65536,cb1.tag div 65536]:=
  cb1.Items[cb1.itemindex];
cb1.visible:=false;
end;

procedure TForm1.cb1Exit(Sender: TObject);
begin
cb1.visible:=false;
end;

procedure TForm1.sg1TopLeftChanged(Sender: TObject);
var
  R: TRect;
begin
      R := SG1.CellRect(cb1.Tag mod 65536,cb1.tag div 65536);
      cb1.left := R.Left+ SG1.Left+1;
end;
0
 
LVL 6

Expert Comment

by:zebada
ID: 6979354
Or if you want to have the combo box visible in the string grid all the time do this:

implementation

// Define the cell that you want the combo box in.
const
  cbCol=2;
  cbRow=3;

procedure TForm1.sgDrawCell(Sender: TObject; ACol, ARow: Integer; r: TRect; State: TGridDrawState);
begin
  with ( Sender as TStringGrid) do
  begin
      if ( (LeftCol<=cbCol) and (cbCol<LeftCol+VisibleColCount) and
           (TopRow<=cbRow) and (cbRow<TopRow+VisibleRowCount) ) then
      begin
        cb.Visible := true;
        if ( (ACol=cbCol) and (ARow=cbRow) ) then
          cb.SetBounds(Left+2+r.Left,Top+2+r.Top,r.Right+2-r.Left,r.Bottom+10-r.Top);
      end
      else
        cb.Visible := false;
  end;
end;

procedure TForm1.cbEnter(Sender: TObject);
var
  r: TGridRect;
begin
  r.Left := cbCol;
  r.Right := cbCol;
  r.Top := cbRow;
  r.Bottom := cbRow;
  sg.Selection := r;
end;

procedure TForm1.cbExit(Sender: TObject);
begin
  sg.SetFocus;
end;

procedure TForm1.sgSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin
  if ( (ACol=cbCol) and (ARow=cbRow) ) then
    cb.SetFocus;
end;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question