Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ComboBox on StringGrid

2,096 views
Skip to first unread message

Mike Sandoval

unread,
May 8, 2002, 12:30:30 PM5/8/02
to
Hello All,

Somehow, I think I should be using DrawGrid rather than StringGrid but I am
not sure how.

I need to create a ComboBox on my grid when certain cells are selected.
Right now I get the rect's of the cell selected and if appropriate I display
the ComboBox over the cell and add the selected item to the grid cell. This
works pretty good, but does not look real professional especially when the
scrolls are used. The grid moves but the ComboBox stays put.

Is there an easy way to create a ComboBox as the StringGrid (DrawGrid) cell?
The ComboBox must be dynamic. only present if cell is selected and other
conditions are met.

Thank you in advance for your support.

--
Mike Sandoval

Ryan Eibling

unread,
May 8, 2002, 2:30:02 PM5/8/02
to
I had exactly the same issue to solve and I did pretty much the same thing
you did. The only "easy" way I know of is to use one of the powerful custom
grid controls out there. The poor man's way doesn't look that bad in my
implementation as long as the DefaultRowHeight of the grid is set to 21 (to
match the combo height) and I solve the scrollbar issue by just hiding the
combo in the OnTopLeftChange event of the grid. Here's all of the code I
used, in case it helps. In case it's not obvious, ProjGrid is the string
grid and GridCombo is the combo box. My code assumes that the combo is
populated and that each cell in the column that uses the combo will already
hold a value that matches an item in the combo.

procedure TForm1.ProjGridSelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
crect: TRect;
begin
if (ACol = 2) then
begin
crect := ProjGrid.CellRect(ACol, ARow);
GridCombo.Top := ProjGrid.Top + crect.Top + 2;
GridCombo.Left := ProjGrid.Left + crect.Left + 2;
GridCombo.Width := crect.Right - crect.Left;
GridCombo.ItemIndex := GridCombo.Items.IndexOf(ProjGrid.Cells[ACol,
ARow]);
GridCombo.Visible := True;
end
else
GridCombo.Visible := False;
end;

procedure TForm1.GridComboExit(Sender: TObject);
begin
GridCombo.Visible := False;
end;

procedure TForm1.GridComboChange(Sender: TObject);
begin
ProjGrid.Cells[ProjGrid.Selection.Left,ProjGrid.Selection.Top] :=
GridCombo.Text;
end;

procedure TForm1.ProjGridTopLeftChanged(Sender: TObject);
begin
GridCombo.Visible := False;
end;

Mike Sandoval

unread,
May 8, 2002, 4:08:31 PM5/8/02
to
Thanks Ryan,

That helped. It looks much nicer now.

Regards,

Mike


Peter Below (TeamB)

unread,
May 9, 2002, 7:15:19 AM5/9/02
to
In article <3cd951ee$1_2@dnews>, Mike Sandoval wrote:
> Somehow, I think I should be using DrawGrid rather than StringGrid but I am
> not sure how.

TDrawgrid has the same problem.



> I need to create a ComboBox on my grid when certain cells are selected.
> Right now I get the rect's of the cell selected and if appropriate I display
> the ComboBox over the cell and add the selected item to the grid cell. This
> works pretty good, but does not look real professional especially when the
> scrolls are used. The grid moves but the ComboBox stays put.

This problem can be solved by setting the comboboxes Parent to the stringgrid.
But it will not work properly in this scenario unless you modify the parent
grid a bit as well. The Delphi grid controls have not been designed to serve
as parents for other controls other then the inplace editor, and so they do
not echo WM_COMMAND messages from child controls back to the control as
CN_COMMAND. That is easily fixed, fortunately.

unit ControlStringgrid;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids;

type
TControlStringgrid = class(TStringgrid)
private
{ Private declarations }
Procedure WMCommand( var msg: TWMCommand ); message WM_COMMAND;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('PBGoodies', [TControlStringgrid]);
end;

{ TControlStringgrid }

procedure TControlStringgrid.WMCommand(var msg: TWMCommand);
begin
If EditorMode and Assigned( Inplaceeditor )
and ( msg.Ctl = InplaceEditor.Handle )
Then
inherited
Else
If msg.Ctl <> 0 Then
msg.result :=
SendMessage( msg.ctl, CN_COMMAND,
TMessage(msg).wparam,
TMessage(msg).lparam );
end;

end.

--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


0 new messages