当我使用c ++ builder时,如何在此输入完成后自动跳到下一个“ TEdit”

时间:2018-07-10 02:07:21

标签: c++ c++builder

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

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

__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);
}

或者:

HashMap<String,Object> amenityMap = new HashMap<>();