TComboBox的OnKeyPress事件接收了回车键,为什么窗体还能接收到
TComboBox的OnKeyPress事件接收了回车键,为什么窗体还能接收到,窗体能接收到也罢了,为什么TComboBox会在窗体接收到后再收到一次回车键,郁闷
也就是说,一次回车键让TComboBox触发了2次OnKeyPress,窗体的KeyPress还触发了一次,这不合理吧,虽然我开了窗体的KeyPreView
怎么这个问题呢
[解决办法]
应该是只会触发两次
先触发form上的 再触发combobx里的
如果不想触发form上的 那个只有把Form的keypreview设置成false或在Form的press事件里判断
combobox是不是获得焦点如果获得则不做press里的代码
[解决办法]
我自己写了一下,得到如下结果
我开了窗体的KeyPreView 只执行2次
窗体的KeyPress触发了一次 TComboBox触发了1次OnKeyPress
我关闭窗体的KeyPreView 只执行1次
TComboBox触发了1次OnKeyPress
不知道你的为什么会那样
如果只是针对TComboBox操作的话
KeyPreView 最好不要设成true
[解决办法]
Keyboard events are received at several levels:
The application level, with an Application-> OnMessage event.
You will rarely need to intercept keystrokes at the application level, but it is important to know that this first level is available.
The "shortcut-key " level
When you specify a shortcut key, such as those provided as a property of menu items, the keystroke is intercepted before the form sees it.
The form level
The form contains a KeyPreview property that enables you to code "global " keystroke events.
The component level
When you program key-press event handlers at the component level, the component with focus intercepts the keystroke.
[解决办法]
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//PostMessage(Button1-> Handle,WM_LBUTTONDOWN,0,0);
//PostMessage(Button1-> Handle,WM_LBUTTONUP,0,0);
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//ShowMessage( "button1 be clicked ");
}
//---------------------------------------
void __fastcall TForm1::ComboBox1KeyPress(TObject *Sender, char &Key)
{
if(Key == 13)
{
ShowMessage( "combobox enter ");
}
}
//---------------------------------------
void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
{
if(Key == 13)
{
if(!this-> ComboBox1-> Focused())
{
ShowMessage( "Form enter ");
}
}
}
//---------------------------------------
如果combobox获得了焦点 就响应combobox的回车 否则响应
[解决办法]
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key == 13)
{
if(this-> ComboBox1-> Focused())
{
//ComboBox1KeyDown(Sender,Key,Shift);
//ShowMessage( "换页 ");
}
else
{
ShowMessage( "换页 ");
}
}
}
//---------------------------------------
void __fastcall TForm1::ComboBox1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key == 13)
{
ShowMessage( "do combobox event ");
ShowMessage( "换页 ");
}
}
//---------------------------------------
Ring_Pt(已是黄昏独自愁) ( )
说的就是这个意思
当combobox获得焦点,所有的事情都让combobox的Keyprss来做就行了