This site is temporarily down. Please come back. A listbox in a cell of a stringgrid
DelphiFAQ Home Search:

A listbox in a cell of a stringgrid

 

comments2 comments. Current rating: 5 stars (2 votes). Leave comments and/ or rate it.

Here's another great trick by programmer illusionists. What appears as embedded comboboxes within a stringgrid is actually just a combobox floating above the stringgrid, and it just so happens to be the exact same size as the stringgrid cell underneath it.

Here's the basics:

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

  procedure TfrmMain.StringGrid1DrawCell(Sender: TObject; Col, Row: 
    Integer; Rect: TRect; State: TGridDrawState);
  var
    R: TRect;
  begin
    if (Col >= StringGrid1.FixedCols) and
      (Row >= StringGrid1.FixedRows) and
      (gdFocused in State) then
      with ComboBox1 do
      begin
        BringToFront;
        CopyRect(R, Rect);
        R.TopLeft :=     frmMain.ScreenToClient(
                         StringGrid1.ClientToScreen(R.TopLeft));
        R.BottomRight := frmMain.ScreenToClient(
                         StringGrid1.ClientToScreen(R.BottomRight));
        SetBounds(R.Left, R.Top, R.Right-R.Left, R.Bottom-R.Top);
      end;
  end;

  procedure TfrmMain.StringGrid1TopLeftChanged(Sender: TObject);
  var
    R: TRect;
  begin
    with StringGrid1 do
        CopyRect(R, CellRect(Col, Row));

    with ComboBox1 do
    begin
      Visible := False;
      R.TopLeft :=     frmMain.ScreenToClient(
                       StringGrid1.ClientToScreen(R.TopLeft));
      R.BottomRight := frmMain.ScreenToClient(
                       StringGrid1.ClientToScreen(R.BottomRight));
      SetBounds(R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top);
    end;

    with StringGrid1 do
      if (TopRow <= Row) and (TopRow + VisibleRowCount > Row) then
         ComboBox1.Show;
  end;

  procedure TfrmMain.ComboBox1Change(Sender: TObject);
  begin
    with StringGrid1 do
      Cells[Col, Row] := ComboBox1.Text;
  end;

In essence, the main routine here is the stringgrid's OnDrawCell event handler. Of course, I also set the stringgrid's DefaultRowHeight property to be the same height as the combobox. In addition, the stringgrid's OnTopLeftChanged event handler is used to hide the combobox when the user scrolls out of view. Also, when the user selects an item from the combobox, simply place the text in the current Col/Row.

You can also do a couple other little tricks such as setting the stringgrid's Objects[] property to point to the combobox, as well as possibly setting the combobox's Parent property to point to the stringgrid. However, I've had problems with the Parent approach -- namely, that of dropping down the listbox associated with the combobox.
Content-type: text/html

Comments:

2006-07-23, 04:20:20
anonymous from Bulgaria  
rating
2010-03-23, 08:40:15
anonymous from South Africa  
rating
This is very helpfull Thanx

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: