C++BUILDER中,对TEdit控件用上下左右箭头实现各个TEdit焦点的切换?
1个回答
展开全部
先手动拖入4个Edit。
H里追加:
private: // User declarations
TEdit * mpEditList[4];
FormCreate里追加:
void __fastcall TForm1::FormCreate(TObject *Sender)
{
mpEditList[0] = this->Edit1;
mpEditList[1] = this->Edit2;
mpEditList[2] = this->Edit3;
mpEditList[3] = this->Edit4;
}
4个Edit都选上,F11,OnKeyDown的消息对应同一个函数:
void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
TEdit * edit = (TEdit*)Sender;
long nIndex = -1;
for (long k=0; k<4; k++)
{
if (edit == mpEditList[k])
{
nIndex = k;
break;
}
}
if (nIndex == -1)
return;
long nIndexNew = nIndex;
if (Key == VK_DOWN)
{
if (nIndexNew<4-1)
nIndexNew ++;
}
if (Key == VK_UP)
{
if (nIndexNew>0)
nIndexNew --;
}
mpEditList[nIndexNew]->SetFocus();
}
VK_DOWN 等的定义查看C++builder Help:
Virtual Key Codes
H里追加:
private: // User declarations
TEdit * mpEditList[4];
FormCreate里追加:
void __fastcall TForm1::FormCreate(TObject *Sender)
{
mpEditList[0] = this->Edit1;
mpEditList[1] = this->Edit2;
mpEditList[2] = this->Edit3;
mpEditList[3] = this->Edit4;
}
4个Edit都选上,F11,OnKeyDown的消息对应同一个函数:
void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
TEdit * edit = (TEdit*)Sender;
long nIndex = -1;
for (long k=0; k<4; k++)
{
if (edit == mpEditList[k])
{
nIndex = k;
break;
}
}
if (nIndex == -1)
return;
long nIndexNew = nIndex;
if (Key == VK_DOWN)
{
if (nIndexNew<4-1)
nIndexNew ++;
}
if (Key == VK_UP)
{
if (nIndexNew>0)
nIndexNew --;
}
mpEditList[nIndexNew]->SetFocus();
}
VK_DOWN 等的定义查看C++builder Help:
Virtual Key Codes
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询