根據應用程序的狀態,表單上的按鍵有時可能有不同的配方。見下面的示例:在父表單和子控件之間分配按鍵
unit Unit1;
interface
uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ComCtrls,
Buttons;
type
TForm1 = class(TForm)
private
ListView1: TListView;
ButtonOK: TBitBtn;
ButtonCancel: TBitBtn;
procedure ButtonClick(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TForm1.Create(AOwner: TComponent);
begin
inherited CreateNew(AOwner);
ClientWidth := 300;
ClientHeight := 240;
ListView1 := TListView.Create(Self);
ListView1.Name := 'ListView1';
ListView1.Parent := Self;
ListView1.Height := 200;
ListView1.Align := alTop;
ListView1.AddItem('aaaaa', nil);
ListView1.AddItem('bbbbb', nil);
ListView1.AddItem('ccccc', nil);
ButtonOK := TBitBtn.Create(Self);
ButtonOK.Parent := Self;
ButtonOK.Left := 8;
ButtonOK.Top := 208;
ButtonOK.Kind := bkOK;
ButtonOK.OnClick := ButtonClick;
ButtonCancel := TBitBtn.Create(Self);
ButtonCancel.Parent := Self;
ButtonCancel.Left := 90;
ButtonCancel.Top := 208;
ButtonCancel.Kind := bkCancel;
ButtonCancel.OnClick := ButtonClick;
end;
procedure TForm1.ButtonClick(Sender: TObject);
begin
ShowMessage((Sender as TBitBtn).Caption);
Application.Terminate;
end;
end.
(要運行此,創建一個標準的VCL應用程序,並與上述替換Unit1.pas的內容)
如果一個啓動應用並按下回車或Esc,適當的按鈕被「點擊」。但是,當開始編輯列表視圖(通過點擊一個項目的一個半時間)輸入和Esc應該接受或取消他們不需要的編輯 - 他們仍然「單擊」按鈕。
類似的情況存在,如果有快捷鍵的操作F2或含有cxGrid,默認情況下使用這些快捷鍵來啓動編輯模式或下拉組合框中編輯表單上F4。
您是否知道如何繼續使用TButton.Default/Cancel和actions的舒適度,而而不是不得不重新實現我使用的所有組件的密鑰處理?
它應該也可以做同樣的事情,而不用做一個派生類,就像這裏:http://stackoverflow.com/questions/2363456/how-do-i-catch-a-vk-tab-key-in -my-tedit-control-and-not-let-it-lose-the-focus – kludg 2010-03-09 19:31:30
而不是創建自己的TmyEdit或類似的東西,只需使用類似於類型TEdit = class(StdCtrls.TEdit){... } end;'添加任何你想要的東西,包括按鍵的新消息處理。你的表單上的任何地方,你有一個標準的TEdit現在將使用你的新擴展版本。或者,將您的新類型聲明添加到單元以從多個表單使用它。在包含原始控件的所有單元(添加它到最後是最簡單的解決方案)之後,您將必須確保將您的單元添加到uses子句中。 – 2011-09-14 17:15:30