如何在StringGrid使用ComboBox输入内容

mmxer 2000-08-19 01:08:00
末学后进向各位大虾请教,我要在Stringgrid中使用combobox,请教如何实现。
...全文
373 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouzq 2000-08-21
  • 打赏
  • 举报
回复
关注
lotto 2000-08-21
  • 打赏
  • 举报
回复
procedure TForm1.FormCreate(Sender: TObject);
begin
combobox1.visible := false;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,ARow: Integer; var CanSelect: Boolean);
var
R: TRect;
org: TPoint;
begin
With Sender As TStringgrid Do
If (ACol = 2) and (ARow >= FixedRows) Then //在第二列显示一个ComboBox
Begin
perform( WM_CANCELMODE, 0, 0 );
R := CellRect( Acol, Arow );
org:= Self.ScreenToClient( ClientToScreen( R.topleft ));
With combobox1 do
begin
setbounds( org.X, org.Y, r.right-r.left, height );
itemindex := Items.IndexOf( Cells[ acol, arow ] );
Show;
BringTofront;
SetFocus;
DroppedDown := true;
end;
End;
end;

procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
with sender as TCombobox do
begin
hide;
if itemindex >= 0 then
with stringgrid1 do
cells[ col, row ] := items[itemindex];
end;
End;
Michaelyfj 2000-08-19
  • 打赏
  • 举报
回复
PoolD的方法虽然能达到你的目的,但并不是较好的办法,我提供参考一下:
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
nLeft, nTop : Integer;
R : TRect;
begin
R := StringGrid1.CellRect(ACol, ARow);
nLeft := stringGrid1.Left;
nTop := stringGrid1.top;
R.Top := R.Top + nTop;
R.Left := R.Left + nLeft;
// InflateRect(R, nLeft, nTop);
ComboBox1.BringToFront;
comboBox1.SetBounds(r.left, r.Top, R.Right-R.Left, r.Bottom-r.Top);
comboBox1.Visible := True;
ComboBox1.SetFocus;
end;
PoolD 2000-08-19
  • 打赏
  • 举报
回复
在form上放一个combobox和一个stringgrid,将combobox的visible设为true

procedure tform1.stringgrid1mousedown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
GridX, GridY: integer;
CellPos: TRect;
begin
stringgrid1.MouseToCell(X, Y, GridX, GridY);
CellPos := stringgrid1.CellRect(Col, Row);
combobox1.Left := stringgrid1.Left + CellPos.Left + 2;
combobox1.Top := stringgrid1.Top + CellPos.Top + ABS((CellPos.Bottom - CellPos.Top - combobox1.Height + 3) div 2);
combobox1.Width := CellPos.Right - CellPos.Left + 1;
combobox1.visible := true;
combobox1.setfocus;
end;

在combobox的exit事件中将combobox隐藏,将stringgrid setfocus。
基本的显示就是这样,其他还有一些控制,自己试一下就行了。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧