Solved

TStringGrid ans TComboBoxes

Posted on 2002-04-29
2
173 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now