在Delphi XE中如何使用TComboBox作為單元格編輯器,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
需要進(jìn)行以下幾步:
創(chuàng)建StringGrid,在OnSelectCell事件中顯示ComboBox覆蓋單元格作為編輯器
創(chuàng)建ComboBox,將其Parent設(shè)置為StringGrid,并將StringGrid的行高設(shè)置為ComboBox的高度
處理ComboBox的OnChange事件,修改StringGrid單元格的值
處理ComboBox的OnExit事件,隱藏ComboBox
創(chuàng)建新單元,定義同名類TStringGrid繼承Vcl.Grids.TStringGrid并重寫其WMCommand方法
在使用StringGrid的單元頭部引用新單元,必須放在Vcl.Grids之后
以下是示例代碼:
單元1:
點(diǎn)擊(此處)折疊或打開
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.Grids,
//必須將重定義的TStringGrid單元引用放置在Vcl.Grids之后
Unit2;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
private
FComboBox: TComboBox;
procedure OnComboBoxChange(Sender: TObject);
procedure OnComboBoxExit(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
//創(chuàng)建ComboBox,也可以直接拖拽到Form
//此處只需要設(shè)置Parent := StringGrid1
FComboBox := TComboBox.Create(StringGrid1);
FComboBox.Parent := StringGrid1;
FComboBox.Items.Add('Item1');
FComboBox.Items.Add('Item2');
FComboBox.OnChange := OnComboBoxChange;
FComboBox.OnExit := OnComboBoxExit;
FComboBox.Visible := False;
//ComboBox高度是固定不能改變的
//因此設(shè)置StringGrid1的行高與ComboBox高度一致
StringGrid1.DefaultRowHeight := FComboBox.Height;
end;
procedure TForm1.OnComboBoxChange(Sender: TObject);
begin
StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row] := FComboBox.Text;
end;
procedure TForm1.OnComboBoxExit(Sender: TObject);
begin
FComboBox.Visible := False;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
var
ARect: TRect;
begin
//示例代碼僅在第二列中使用ComboBox作為編輯器
if CanSelect and (ACol = 1) then
begin
FComboBox.ItemIndex := FComboBox.Items.IndexOf
(StringGrid1.Cells[ACol, ARow]);
//使ComboBox顯示并覆蓋住選中單元格
ARect := StringGrid1.CellRect(ACol, ARow);
FComboBox.Left := ARect.Left;
FComboBox.Top := ARect.Top;
FComboBox.Width := ARect.Right - ARect.Left;
FComboBox.Visible := True;
FComboBox.SetFocus;
end;
end;
end.
單元2:
點(diǎn)擊(此處)折疊或打開
unit Unit2;
interface
uses
Vcl.Grids, Winapi.Windows, Winapi.Messages, Vcl.Controls;
type
TStringGrid = class(Vcl.Grids.TStringGrid)
private
procedure WMCommand(var AMessage: TWMCommand); message WM_COMMAND;
end;
implementation
{ TStringGrid }
procedure TStringGrid.WMCommand(var AMessage: TWMCommand);
begin
//如果當(dāng)前是StringGrid內(nèi)置編輯框,調(diào)用父類方法
//否則向控件發(fā)送CN_COMMAND事件
if (InplaceEditor <> nil) and (AMessage.Ctl = InplaceEditor.Handle) then
inherited
else if AMessage.Ctl <> 0 then
begin
AMessage.Result := SendMessage(AMessage.Ctl, CN_COMMAND,
TMessage(AMessage).WParam, TMessage(AMessage).LParam);
end;
end;
end.
說明:
TStringGrid只支持內(nèi)置的輸入框做為單元格編輯器,所以只好放置一個(gè)ComboBox并覆蓋住要編輯的單元格
TStringGrid祖先類TCustomGrid在WMCommand方法中限制了只處理InplaceEditor,所以需要重寫這個(gè)方法
也可以繼承TStringGrid而不是使用同名類,再全部動(dòng)態(tài)創(chuàng)建,但是太麻煩而且基本沒什么區(qū)別
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)站標(biāo)題:在DelphiXE中如何使用TComboBox作為單元格編輯器-創(chuàng)新互聯(lián)
文章路徑:http://jinyejixie.com/article48/heghp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、軟件開發(fā)、網(wǎng)站收錄、全網(wǎng)營銷推廣、移動(dòng)網(wǎng)站建設(shè)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容