C++ Builder XE4, 10.2 Tokyo > 外部ソース > TEditへの入力制限 {数値 | アルファベット | -_}

```txt:動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)
```

https://qiita.com/7of9/items/fd0441c7f061be360faa
をベースに外部ソースとした。

### 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はフォームキャプションに表示してみた。

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/1a7ff670-6999-79a4-c7df-1729b8973d37.png)

### 備考1

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