Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TStringGrid ans TComboBoxes

Posted on 2002-04-29
2
Medium Priority
?
181 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

782 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