C++ Builder

La Isla Bonita



Form, Send To Back
Form, Send To Back


Description

This article descibes a method to send a particular Form to the back, so that all other forms stay on the top of it.

Form1 contains 4 buttons:
Button1 used to create 20 Forms
Button2 used to sent to back a selected Form (Form 10)
Button3 used to hide all Forms except the selected one
Button4 used to show all hidden Forms

SendToBack.cpp

#include <vcl.h>
#pragma hdrstop

#include "SendToBack.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TForm1 *pForm[1000];

int FormCounter = -1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    Color     = clBlue;
    FormStyle = fsStayOnTop;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Create 20 Application Forms

    while (FormCounter < 20)
    {
        FormCounter++;
        pForm[FormCounter] = new TForm1(Application);

        pForm[FormCounter]->Left      = 10*FormCounter;
        pForm[FormCounter]->Top       = 10*FormCounter;
        pForm[FormCounter]->Width     = 300;
        pForm[FormCounter]->Height    = 100;
        pForm[FormCounter]->Color     = clRed;
        pForm[FormCounter]->Caption   = "Form " + String( FormCounter );
        pForm[FormCounter]->FormStyle = fsNormal;
        pForm[FormCounter]->Show();
   }
   
   // Disable the button and make it invisible
   // after creating all the forms
   //
   Button1->Enabled = false;
   Button1->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Send To Back Form whose caption is "Form 10"

   for( int i = 0; i <= FormCounter; i++)
   {
       if ( pForm[i]->Caption == "Form " + String(10) )
       {
          // Send the desired Form to back
          //
          pForm[i]->FormStyle = fsNormal;
       }
       else
       {
          // Send all other Forms to the top
          // (Make them stay on the top)
          //
          pForm[i]->FormStyle = fsStayOnTop;
       }
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
// Hide all Forms except the selected one. (Form 10)

   for( int i = 0; i <= FormCounter; i++)
   {
       if ( !(pForm[i]->Caption == "Form " + String(10)))
       {
           pForm[i]->Hide();
       }
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
// Show all hidden Forms

   for( int i = 0; i <= FormCounter; i++)
   {
       if ( !(pForm[i]->Caption == "Form " + String(10)))
       {
           pForm[i]->Show();
       }
   }
}

SendToBack.h

#ifndef SendToBackH
#define SendToBackH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        TButton *Button2;
        TButton *Button3;
        TButton *Button4;

        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall Button3Click(TObject *Sender);
        void __fastcall Button4Click(TObject *Sender);
private:	// User declarations

public:		// User declarations

        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.