当我使用c++生成器时,如何在输入完成后自动跳转到下一个"TEdit"

  • 本文关键字:quot TEdit 下一个 c++ c++ c++builder
  • 更新时间 :
  • 英文 :


我想实现一个许可证功能。我使用了五个TEdit控件,以及如何在输入完成后自动跳转到下一个TEdit

试试这样的东西:

class TMyForm : public TForm
{
__published:
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
TEdit *Edit4;
TEdit *Edit5;
void __fastcall EditChange(TObject *Sender);
private:
TEdit *Edits[5];
int __fastcall IndexOf(TEdit *Edit);
public:
__fastcall TMyForm(TComponent *Owner);
}

__fastcall TMyForm::TMyForm(TComponent *Owner)
: TForm(Owner)
{
Edits[0] = Edit1;
Edits[1] = Edit2;
Edits[2] = Edit3;
Edits[3] = Edit4;
Edits[4] = Edit5;
}
int __fastcall TMyForm::IndexOf(TEdit *Edit)
{
for(int i = 0; i < 5; ++i)
{
if (Edits[i] == Edit)
return i;
}
return -1;
}
// assign this single OnChange event handler to the 5 TEdit controls...
void __fastcall TMyForm::EditChange(TObject *Sender)
{
TEdit *ThisEdit = static_cast<TEdit*>(Sender);
if (ThisEdit->GetTextLen() >= ThisEdit->MaxLength)
{
int idx = IndexOf(ThisEdit);
if (idx != -1)
{
if (idx < 4)
Edits[idx+1]->SetFocus();
else
OkButton->SetFocus(); // or whatever you want to do after the last input...
}   
}
}

或者:

__fastcall TMyForm::TMyForm(TComponent *Owner)
: TForm(Owner)
{
Edit1->Tag = reinterpret_cast<NativeInt>(static_cast<TControl*>(Edit2));
Edit2->Tag = reinterpret_cast<NativeInt>(static_cast<TControl*>(Edit3));
Edit3->Tag = reinterpret_cast<NativeInt>(static_cast<TControl*>(Edit4));
Edit4->Tag = reinterpret_cast<NativeInt>(static_cast<TControl*>(Edit5));
Edit5->Tag = 0; // or whatever control you want focused after the last input...
}
// assign this single OnChange event handler to the 5 TEdit controls...
void __fastcall TMyForm::EditChange(TObject *Sender)
{
TEdit *ThisEdit = static_cast<TEdit*>(Sender);
if (ThisEdit->GetTextLen() >= ThisEdit->MaxLength)
{
TControl *NextControl = reinterpret_cast<TControl*>(ThisEdit->Tag);
if (NextControl)
NextControl->SetFocus();
else
// whatever you want to do after the last input...
}
}

或者:

// configure the tab stops on the TEdit (and other controls) as desired,
// and assign this single OnChange event handler to the 5 TEdit controls...
void __fastcall TMyForm::EditChange(TObject *Sender)
{
TEdit *ThisEdit = static_cast<TEdit*>(Sender);
if (ThisEdit->GetTextLen() >= ThisEdit->MaxLength)
SelectNext(ThisEdit, true, true);
}

最新更新