Надоело писать:
Button1->Enabled = SomeCondition;
Button2->Enabled = SomeConsition;
...
ButtonXXX->Enabled = SomeConsition;
В общем, куча ненужного дублирования кода...
Набросал простенький классец, который вышеописанную проблему решает так:
TDisabler Disabler;
// можно задизаблить так:
Disabler << Button1 << Button2 << Label1 << Memo1 << ListView1;
// можно заэнаблить так
Disabler >> Button1 >> Button2 >> Label1 >> Memo1 >> ListView1;
// Можно просто менять состояние в зависимости от условия
// если true - то будет включать компоненты, false - выключать
Disabler.SetEnabled(SomeCondition);
Disabler << Button1 << Button2 << Label1 << Memo1 << ListView1;
Сам класс:
//---------------------------------------------------------------------------
#ifndef EnablerHelperH
#define EnablerHelperH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <TypInfo.hpp>
//---------------------------------------------------------------------------
class TDisabler
{
private:
void SetComponentState(TComponent* Component)
{
if(Component)
{
PPropInfo PropInfo = GetPropInfo((PTypeInfo)Component->ClassInfo(), "Enabled");
if (PropInfo)
SetOrdProp(static_cast<TObject *>(Component), PropInfo, (int)FEnabled);
}
}
bool FEnabled;
public:
void SetEnabled(bool Enabled) {FEnabled = Enabled;}
TDisabler() : FEnabled(false) {}
~TDisabler() {}
TDisabler& operator<<(TComponent* Component)
{
if(Component)
{
SetComponentState(Component);
}
return *this;
}
TDisabler& operator>>(TComponent* Component)
{
if(Component)
{
bool Temp = FEnabled;
FEnabled = true;
SetComponentState(Component);
FEnabled = Temp;
}
return *this;
}
};
//---------------------------------------------------------------------------
#endif