根据应用程序的状态,表单上的按键有时可能有不同的配方。见下面的示例:在父表单和子控件之间分配按键
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