在DelphiXE中如何使用TComboBox作為單元格編輯器-創(chuàng)新互聯(lián)

在Delphi XE中如何使用TComboBox作為單元格編輯器,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)2013年開(kāi)創(chuàng)至今,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元常州做網(wǎng)站,已為上家服務(wù),為常州各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

需要進(jìn)行以下幾步:

  1. 創(chuàng)建StringGrid,在OnSelectCell事件中顯示ComboBox覆蓋單元格作為編輯器

  2. 創(chuàng)建ComboBox,將其Parent設(shè)置為StringGrid,并將StringGrid的行高設(shè)置為ComboBox的高度

  3. 處理ComboBox的OnChange事件,修改StringGrid單元格的值

  4. 處理ComboBox的OnExit事件,隱藏ComboBox

  5. 創(chuàng)建新單元,定義同名類(lèi)TStringGrid繼承Vcl.Grids.TStringGrid并重寫(xiě)其WMCommand方法

  6. 在使用StringGrid的單元頭部引用新單元,必須放在Vcl.Grids之后


以下是示例代碼:

單元1:

點(diǎn)擊(此處)折疊或打開(kāi)

  1. unit Unit1;

  2. interface

  3. uses

  4.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,

  5.   Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.Grids,

  6.   //必須將重定義的TStringGrid單元引用放置在Vcl.Grids之后

  7.   Unit2;

  8. type

  9.   TForm1 = class(TForm)

  10.     StringGrid1: TStringGrid;

  11.     procedure FormCreate(Sender: TObject);

  12.     procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;

  13.       var CanSelect: Boolean);

  14.   private

  15.     FComboBox: TComboBox;

  16.     procedure OnComboBoxChange(Sender: TObject);

  17.     procedure OnComboBoxExit(Sender: TObject);

  18.     { Private declarations }

  19.   public

  20.     { Public declarations }

  21.   end;

  22. var

  23.   Form1: TForm1;

  24. implementation

  25. {$R *.dfm}

  26. procedure TForm1.FormCreate(Sender: TObject);

  27. begin

  28.   //創(chuàng)建ComboBox,也可以直接拖拽到Form

  29.   //此處只需要設(shè)置Parent := StringGrid1

  30.   FComboBox := TComboBox.Create(StringGrid1);

  31.   FComboBox.Parent := StringGrid1;

  32.   FComboBox.Items.Add('Item1');

  33.   FComboBox.Items.Add('Item2');

  34.   FComboBox.OnChange := OnComboBoxChange;

  35.   FComboBox.OnExit := OnComboBoxExit;

  36.   FComboBox.Visible := False;

  37.   //ComboBox高度是固定不能改變的

  38.   //因此設(shè)置StringGrid1的行高與ComboBox高度一致

  39.   StringGrid1.DefaultRowHeight := FComboBox.Height;

  40. end;

  41. procedure TForm1.OnComboBoxChange(Sender: TObject);

  42. begin

  43.   StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row] := FComboBox.Text;

  44. end;

  45. procedure TForm1.OnComboBoxExit(Sender: TObject);

  46. begin

  47.   FComboBox.Visible := False;

  48. end;

  49. procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;

  50.   var CanSelect: Boolean);

  51. var

  52.   ARect: TRect;

  53. begin

  54.   //示例代碼僅在第二列中使用ComboBox作為編輯器

  55.   if CanSelect and (ACol = 1) then

  56.   begin

  57.     FComboBox.ItemIndex := FComboBox.Items.IndexOf

  58.       (StringGrid1.Cells[ACol, ARow]);

  59.     //使ComboBox顯示并覆蓋住選中單元格

  60.     ARect := StringGrid1.CellRect(ACol, ARow);

  61.     FComboBox.Left := ARect.Left;

  62.     FComboBox.Top := ARect.Top;

  63.     FComboBox.Width := ARect.Right - ARect.Left;

  64.     FComboBox.Visible := True;

  65.     FComboBox.SetFocus;

  66.   end;

  67. end;

  68. end.


單元2:

點(diǎn)擊(此處)折疊或打開(kāi)

  1. unit Unit2;

  2. interface

  3. uses

  4.   Vcl.Grids, Winapi.Windows, Winapi.Messages, Vcl.Controls;

  5. type

  6.   TStringGrid = class(Vcl.Grids.TStringGrid)

  7.   private

  8.     procedure WMCommand(var AMessage: TWMCommand); message WM_COMMAND;

  9.   end;

  10. implementation

  11. { TStringGrid }

  12. procedure TStringGrid.WMCommand(var AMessage: TWMCommand);

  13. begin

  14.   //如果當(dāng)前是StringGrid內(nèi)置編輯框,調(diào)用父類(lèi)方法

  15.   //否則向控件發(fā)送CN_COMMAND事件

  16.   if (InplaceEditor <> nil) and (AMessage.Ctl = InplaceEditor.Handle) then

  17.     inherited

  18.   else if AMessage.Ctl <> 0 then

  19.   begin

  20.     AMessage.Result := SendMessage(AMessage.Ctl, CN_COMMAND,

  21.       TMessage(AMessage).WParam, TMessage(AMessage).LParam);

  22.   end;

  23. end;

  24. end.


說(shuō)明:

  1. TStringGrid只支持內(nèi)置的輸入框做為單元格編輯器,所以只好放置一個(gè)ComboBox并覆蓋住要編輯的單元格

  2. TStringGrid祖先類(lèi)TCustomGrid在WMCommand方法中限制了只處理InplaceEditor,所以需要重寫(xiě)這個(gè)方法

也可以繼承TStringGrid而不是使用同名類(lèi),再全部動(dòng)態(tài)創(chuàng)建,但是太麻煩而且基本沒(méi)什么區(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://muchs.cn/article48/heghp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、軟件開(kāi)發(fā)、網(wǎng)站收錄全網(wǎng)營(yíng)銷(xiāo)推廣、移動(dòng)網(wǎng)站建設(shè)、App開(kāi)發(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作