動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)
失敗する流れ
- 複数のTEdit型コンポーネントがある
- Tag値をXXX設定のレジスタ値として使う設計をした
- フォームのOnCreateでレジスタ値を設定しておく
- !!ミス!!: 将来、コンポーネント追加時にTag設定を忘れる
ミスを防ぐには、設定し忘れたコンポーネントを明示すればいい。
code
関連: c++ builder > debug / testing > OnKeyPressイベントをセットし忘れたコンポーネントを明示する
をベースに変更。
Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE で管理されるコンポーネント
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
void __fastcall FormShow(TObject *Sender);
private: // ユーザー宣言
void __fastcall TForm1::debug_showTagNotSetTEdit();
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Edit1->Tag = 3;
//Edit2->Tag = 1;
Edit3->Tag = 4;
}
//---------------------------------------------------------------------------
static const int kTag_default = 0;
void __fastcall TForm1::debug_showTagNotSetTEdit()
{
TComponent *ptarget;
for(int idx = 0; idx < ComponentCount; idx++) {
ptarget = this->Components[idx];
if (dynamic_cast<TEdit *>(ptarget) == NULL) { // TEditのみチェック
continue;
}
TEdit *edPtr = (TEdit *)ptarget;
if (edPtr->Tag == kTag_default) {
edPtr->Color = clLime;
edPtr->Hint = L"Tag is not defined on constructor";
edPtr->ShowHint = true;
}
}
}
void __fastcall TForm1::FormShow(TObject *Sender)
{
debug_showTagNotSetTEdit();
}
//---------------------------------------------------------------------------
実行例
TCheckBox
TCheckBoxの場合は色づけができない。
代わりにUnderlineを使う、など。
cbPtr->Font->Style = TFontStyles() << fsBold << fsUnderline;
関連
技術英語 > to make errors difficult to overlook
間違いを見つけやすいUIは良いUIだと思う。