如何在Delphi 的StringGrid 控件的 cell 中添加进别的控件

Richzy 2001-07-31 02:45:04
有哪位大虾知道如何在Delphi 的StringGrid 控件的 cell 中添加进别的控件,如:ComboBox ?请速回复,小弟在这边感激不尽!
...全文
394 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Richzy 2002-01-02
  • 打赏
  • 举报
回复
经过我多方查找,终找到一个比较好的例子,和大家一同分享:
=========================================================
StringGrid.dpr:
program StringGrid;

uses
Forms,
frmMain in 'frmMain.pas' {Form1};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
===================================================================
StringGrid.dpr
program StringGrid;

uses
Forms,
frmMain in 'frmMain.pas' {Form1};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
=========================================================================
frmMain.dfm
object Form1: TForm1
Left = 189
Top = 108
Width = 430
Height = 390
Caption = 'Example: TStringGrid advanced using'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 8
Top = 8
Width = 405
Height = 201
ColCount = 2
DefaultColWidth = 200
DefaultRowHeight = 21
RowCount = 9
TabOrder = 0
OnDrawCell = StringGrid1DrawCell
OnSelectCell = StringGrid1SelectCell
RowHeights = (
21
21
21
21
21
21
21
21
21)
end
object cbInplaceComboBox: TComboBox
Left = 216
Top = 32
Width = 145
Height = 21
Style = csDropDownList
ItemHeight = 13
TabOrder = 1
OnChange = cbInplaceComboBoxChange
Items.Strings = (
'1 value '
'2 value '
'3 value '
'4 value'
'5 value'
'6 value'
'7 value '
'8 value'
'9 value')
end
object Button1: TButton
Left = 320
Top = 333
Width = 91
Height = 25
Caption = '&Close'
Default = True
TabOrder = 2
OnClick = Button1Click
end
object MemoDescription: TMemo
Left = 7
Top = 216
Width = 406
Height = 105
Font.Charset = DEFAULT_CHARSET
Font.Color = clNavy
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
Lines.Strings = (
'In this sample I shows how you can:'
''
'1. change the inplace editor for second column '
' from standard inplace editor to user TComboBox component'
''
' view the next procedures:'
' - TfrmMain.FormCreate'
' - TfrmMain.CMDialogKey'
' - TfrmMain.cbInplaceComboBoxChange'
' - TfrmMain.StringGrid1SelectCell'
''
'2. draw the cell values with different fonts'
''
' view the next procedure:'
' - TfrmMain.StringGrid1DrawCell'
' '
'3. change alignment for cells'
' '
' view the next procedure:'
' - TfrmMain.StringGrid1DrawCell')
ParentFont = False
ScrollBars = ssVertical
TabOrder = 3
end
end
========================================================================
frmMain.pas
unit frmMain;

interface

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

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
cbInplaceComboBox: TComboBox;
Button1: TButton;
MemoDescription: TMemo;
procedure FormCreate(Sender: TObject);
procedure cbInplaceComboBoxChange(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure CMDialogKey(var msg: TCMDialogKey); message CM_DIALOGKEY;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
StringGrid1.DefaultRowHeight:=cbInplaceComboBox.Height;
cbInplaceComboBox.Visible:=False;
StringGrid1.Cells[0,0]:='Row No';
StringGrid1.Cells[1,0]:='Values(from combobox)';
for i:=1 to StringGrid1.RowCount-1 do
StringGrid1.Cells[0,i]:=IntToStr(i);
end;

procedure TForm1.CMDialogKey(var msg:TCMDialogKey);
begin
if (ActiveControl = cbInplaceComboBox) then
begin
if (msg.CharCode = VK_TAB) then
begin
//set focus back to the grid and pass the tab key to it
cbInplaceComboBox.SetFocus;
cbInplaceComboBox.Perform(WM_KEYDOWN, msg.CharCode, msg.KeyData);
// swallow this message
msg.result := 1;
Exit;
end;
end;

inherited;
end;

procedure TForm1.cbInplaceComboBoxChange(Sender: TObject);
var
intRow:integer;
begin
inherited;

{Get the ComboBox selection and place in the grid}
with cbInplaceComboBox do
begin
intRow := StringGrid1.Row;
if (StringGrid1.Col = 2) then
StringGrid1.Cells[2, intRow] := Items[ItemIndex]
else
StringGrid1.Cells[StringGrid1.Col, intRow] := Items[ItemIndex];
Visible := False;
end;
StringGrid1.SetFocus;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
R:TRect;
begin
if ((ACol = 1) and (ARow <> 0)) then
begin
{Size and position the combo box to fit the cell}
R := StringGrid1.CellRect(ACol, ARow);
R.Left := R.Left + StringGrid1.Left;
R.Right := R.Right + StringGrid1.Left;
R.Top := R.Top + StringGrid1.Top;
R.Bottom := R.Bottom + StringGrid1.Top;

{Show the combobox}
with cbInplaceComboBox do
begin
Left := R.Left + 1;
Top := R.Top + 1;
Width := (R.Right + 1) - R.Left;
Height := (R.Bottom + 1) - R.Top;
ItemIndex := Items.IndexOf(StringGrid1.Cells[ACol, ARow]);
Visible := True;
SetFocus;
end;
end;
CanSelect := True;

end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
const
AlignFlags: array [TAlignment] of Integer =
(DT_LEFT or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
DT_RIGHT or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
DT_CENTER or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX);
var s: string;
begin
inherited;

with Rect do
begin
Left := Left + 2;
Top := Top + 2;
Right := Right - 5
end;

s:= StringGrid1.Cells[ACol, ARow];

if (ARow = 0) and (ACol < StringGrid1.ColCount) then
begin
StringGrid1.Canvas.Font.Style := StringGrid1.Canvas.Font.Style + [fsBold];
StringGrid1.Canvas.Brush.Color := StringGrid1.FixedColor;
StringGrid1.Canvas.FillRect(Rect);

DrawText(StringGrid1.Canvas.Handle,
PChar(s), Length(s),
Rect, AlignFlags[taCenter]);
end
else
if (ACol = 0) and (ARow > 0) and (ARow < StringGrid1.RowCount) then
begin
StringGrid1.Canvas.FillRect(Rect);
DrawText(StringGrid1.Canvas.Handle,
PChar(s), Length(s),
Rect, AlignFlags[taRightJustify]);
end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;

end.

Richzy 2001-08-02
  • 打赏
  • 举报
回复
多谢各位的关注,我需要的是在StringGrid的cell中加如ComboBox而不是在DBGrid中加入。
具体效果是当cell不得到焦点是和普通的cell一样,当得到焦点是在cell中用ComboBox替换cell中的Edit,失去焦点后用ComboBox选中的值填入cell中。
victorchen_2000 2001-08-02
  • 打赏
  • 举报
回复
对grid 的cell的编辑其实是有个TEdit 控件在cell上,你可以看看\src\vcl下的grid控件的原码,看一下就明白了。
zzutlink 2001-08-02
  • 打赏
  • 举报
回复
在CELL中放控件?我放过,很丑的,别干这种事情了,换别的办法吧!
重载ondrawcell事件往里面加一个stringlist吧
trainbox 2001-08-02
  • 打赏
  • 举报
回复
在窗体放一个combox,当你要显示他时,就把它移动到你要显示的位置,再把visible设为true就行了。不需要显示时,就把visible设为false就行了
Tompage 2001-08-02
  • 打赏
  • 举报
回复
我曾经从StringGrid继承过一个子类来处理这种问题, 继承后将cell中内置的编辑框类替换掉,当时我是把DBGrid类中的那个子类拷过来用的,反正是挺麻烦的!!
ExitWindows 2001-08-01
  • 打赏
  • 举报
回复
up
商雁 2001-07-31
  • 打赏
  • 举报
回复
找一个第三方grid控件就行了。很多免费的控件都有这个功能。
ExitWindows 2001-07-31
  • 打赏
  • 举报
回复
up
willsound 2001-07-31
  • 打赏
  • 举报
回复
书写dbdrid的drawdatacell事件 使你想加入的控件代替DBEDIT
然后书写oncolexit事件,便之在未获焦点时不显示此控件
hicaci 2001-07-31
  • 打赏
  • 举报
回复
gz
delphi_user 2001-07-31
  • 打赏
  • 举报
回复
关注
willsound 2001-07-31
  • 打赏
  • 举报
回复
书写dbdrid的drawdatacell事件
prometheusphinx 2001-07-31
  • 打赏
  • 举报
回复
请问你需要怎样一种效果,能否说得具体一点呢?

5,928

社区成员

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

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