C++ Builder XE4, 10.2 Tokyo > 외부 소스 > TEdit에 대한 입력 제한 {숫자 | 알파벳 | -_}

운영 환경
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)

htps : // m / 7 ~ f9 / ms / fd0441c7f061 ~ 360
을 기반으로 외부 소스로했다.

UtilTEditStringLimit



UtilTEditStringLimit.h
//---------------------------------------------------------------------------

#ifndef UtilTEditStringLimitH
#define UtilTEditStringLimitH

//---------------------------------------------------------------------------

/*
フォーム上のTEditに関して文字列の入力制限を加える。
*/

class CUtilTEditStringLimit {
private:

public:
    static bool __fastcall EditKeyPress_profilename(TObject *Sender, System::WideChar &Key);
};

#endif

UtilTEditStringLimit.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "UtilTEditStringLimit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

/*
v0.1 2017/11/07
    - EditKeyPress_profilename()追加
*/

#include <IdGlobal.hpp>

static const char kCode_BackSpace = 0x08;
static const char kCode_minus = '-';
static const char kCode_underline = '_';

/*static*/bool __fastcall CUtilTEditStringLimit::EditKeyPress_profilename(TObject *Sender, System::WideChar &Key)
{
    if ( Key == kCode_BackSpace ||
        Key == kCode_minus ||
        Key == kCode_underline ) {
        return true;
    }
    if (IsAlphaNumeric(Key) == false) {
        Key = 0x00;  // discard the key
        return false;
    }
    return true;
}

/* 使用方法例
//void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
//{
//  System::WideChar wrk = Key;
//  bool res = CUtilTEditStringLimit::EditKeyPress_profilename(Sender, Key);
//  if (res == false) {
//      this->Caption = this->Caption + String(wrk);
//  }
//}
*/

사용 예



Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "UtilTEditStringLimit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------

#include <IdGlobal.hpp> // IsAlphaNumeric()

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
{
    System::WideChar wrk = Key;
    bool res = CUtilTEditStringLimit::EditKeyPress_profilename(Sender, Key);
    if (res == false) {
        this->Caption = this->Caption + String(wrk);
    }
}
//---------------------------------------------------------------------------

실행 예



제외된 Key는 폼 캡션에 표시해 보았다.



비고1


__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Edit1->OnKeyPress = CUtilTEditStringLimit::EditKeyPress_profilename;
}

상기는 이하의 에러가 되었다.
[bcc32 エラー] Unit1.cpp(19): E2034 'bool(TObject *,wchar_t &)' 型は 'TKeyPressEvent' 型に変換できない
  詳細な解析情報
    Unit1.cpp(17): 構文解析対象:  _fastcall TForm1::TForm1(TComponent *)

비고 2



복수의 컴포넌트에 적용하는 경우, 이하와 같은 구현이 생각된다.

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "UtilTEditStringLimit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------

#include <IdGlobal.hpp> // IsAlphaNumeric()

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Edit1->OnKeyPress = EditKeyPress_limit_general;
    Edit2->OnKeyPress = EditKeyPress_limit_general;
    Edit3->OnKeyPress = EditKeyPress_limit_general;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::EditKeyPress_limit_general(TObject *Sender, System::WideChar &Key)
{
    System::WideChar wrk = Key;
    bool res = CUtilTEditStringLimit::EditKeyPress_profilename(Sender, Key);
    if (res == false) {
        this->Caption = this->Caption + String(wrk);
    }
}
//---------------------------------------------------------------------------

좋은 웹페이지 즐겨찾기