TToolButton - read from database

I'm using C++ Buider 6.0 and I have this idea of enabling/disabling toolbuttons and menuitems using the settings in a Database. I use this so when the user logs in the application enables or disables these buttons depending of the permissions he has. Instead of writing the all code like:

ToolButton1->Enabled = False;

I would like to read the name of buttons from the database and its attributes, something like this;

for (int i=0; iEnabled = database.RecordButtonValue;

But the problem is that I cannot convert AnsiString to TToolButton.

Any idea how to make this or another system?

Sorry about the newbie questions.



  • : But the problem is that I cannot convert AnsiString to TToolButton.
    Of course you cannot. Therefore you have to write a function to convert this. If you just want to set the Caption of the TButton, use

    Button->Caption = database.RecordButtonValue;

    Also, I saw in your code that you create pointers to TButton in an incorrect way. The safest is to let these pointers be handled by C++ Builder (i.e. just drag-and-drop). You can create one dynamically by:
    void __fastcall TForm1::Button1Click(TObject *Sender)
    TButton * button = new TButton(this);
    button->Left = random(200);
    button->Top = random(200);
    button->Visible = true;
    button->Parent = this;
    button->Caption = IntToStr(random(100));
    This gives the hassle that you need to keep a std::vector of OnClick functions, one for each button.

    Good luck,

  • Thanks bilderbikkel for your answer, but I was not looking to change the caption of the button, the idea was to access that button from a caption stored in a database.

    I have this object called ToolButton1, the name of the object is stored in the database, the idea is read the object from the databse and then enable or disable.

  • Hi Worldbyte, sorry I misunderstood, I hope I get it now:

    I think you want to know this:
    (I typed this code without my glasses, perhaps I got a typo in it)
    //Add to header files of 'Unit1.hpp'

    //Have a private std::vector with all TToolButton in TForm1's declaration in 'Unit1.hpp'
    private: //User declarations
    std::vector mButtons;

    //In TForm1's constructor in 'Unit1.cpp'

    //In TForm1's destructor:
    //Nothing! These copied pointers' originals are deleted by Builder

    //In TForm1's code somewhere:
    mDatabase->setButtons(mButtons); //mDatabase is a member Database of TForm1

    //In your database:

    void Database::setButtons(std::vector& buttons) const
    switch(mPhase) //mPhase is a member int storing the phase your program is in
    case 0: setButtons(buttons,"Init",true); break;
    case 1: setButtons(buttons,"Init",false); break;
    case 2: setButtons(buttons,"Start",true); break;
    case 3: setButtons(buttons,"Start",false); break;
    case 4: setButtons(buttons,"End",true); break;
    default: assert(!"Should not get here");

    //Private function:
    void Database::setButtons(std::vector& buttons, const String& caption, const bool& doEnable) const
    const int nButtons = buttons.size();
    for (int i=0; iCaption == caption) buttons[i]->Enabled = doEnable;

    You could also use a std::map instead of a std::vector (see e.g. and

    I hope I helped you on the way with a well-understood question,

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!