Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Docking combo box in stringgrid cell

Status
Not open for further replies.

vincer

Programmer
Mar 12, 2002
45
GB
Hello ,
is there an easy way of associating a combobox with a certain column in a string grid?
Thanks,
Vince
 
This is what works for me. Just drop a ComboBox on the same form as the StringGrid for each column you need a ComboBox for.


procedure TfrmMain.sgCellsClick(Sender: TObject);
var
nTop, nLeft, nWidth, nHeight : integer;
begin

if ( sgCells.Col = 0 ) then
begin
with cbCol0 do
begin
PositionCB( sgCells, nTop, nLeft, nHeight, nWidth );
Top := nTop;
Left := nLeft;
Width := nWidth;
Height := nHeight;

ItemIndex := Items.IndexOf( sgCells.Cells[ sgCells.Col, sgCells.Row ] );
Visible := True;
ActiveControl := cbCol0;
end;
end
else if ( sgCells.Col = 1 ) then
with cbCol1 do
begin
PositionCB( sgCells, nTop, nLeft, nHeight, nWidth );
Top := nTop;
Left := nLeft;
Width := nWidth;
Height := nHeight;

ItemIndex := Items.IndexOf( sgCells.Cells[ sgCells.Col, sgCells.Row ] );
Visible := True;
ActiveControl := cbCol1;
end;
end
.
.
.

procedure TfrmMain.PositionCB( const sgGrid : TStringGrid; var iTop, iLeft, iHeight, iWidth : integer );
var
i, iStartRow, iStartCol : integer;
begin
with sgGrid do
begin
iStartRow := TopRow;
iStartCol := LeftCol;
iTop := Top;
iLeft := Left;

{ Compute Row Height }
for i := iStartRow to Row do
iTop := iTop + RowHeights[ i ] + GridLineWidth;

{ Compute Col Width }
for i := iStartCol to Col - 1 do
iLeft := iLeft + ColWidths[ i ] + GridLineWidth;
if ( FixedCols > 0 ) then
for i := 0 to FixedCols - 1 do
iLeft := iLeft + ColWidths[ i ] + GridLineWidth;

iTop := iTop + GridLineWidth + 1;
iLeft := iLeft + GridLineWidth + 1;

iHeight := RowHeights[ Row ] + ( GridLineWidth * ( Row - iStartRow ) );
iWidth := ColWidths[ Col ];
end;
end;


I hope this helps.
 
Thanks for that,
it doesn't quite work but it's better than I had.
Basically the activecontrol statement causes it to generate
"cannot focus a disabled or invisible widget"
so I took it out.

If I click on the scroll bar the highlighted cell is out of sync with the combobox position

I shall attempt to fix it.
 
The program I pulled this from is being run daily without the error you described. You may want to check the properties of the ComboBox.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top