From Clomosy Docs

TclUnit is a class used for the management and functionality of units.
It is used to track which form is being called or which unit is being interacted with. This is essential for managing flow and user interactions within the application.

It is typically used to specify which form is calling during the creation or management of units.

How to Use TclUnit:
Let's assume;

  • Target unit: uLogin
  • Main unit: Main Code

First, an object of the TclUnit class needs to be defined in the main unit.

Unit1 : TclUnit;

Then, the defined object needs to be created.

Unit1 = TclUnit.Create;

To navigate between units, set the UnitName property of the created object to the name of the target unit:

 Unit1.UnitName = 'uLogin';

To specify the form from which the target unit is called, use the CallerForm property for reference. (The Form1 object is the form object created on the main unit.)

Unit1.CallerForm = Form1;

After completing these steps, use the Run method to initiate the target unit.

Unit1.Run;

Example 1

Main Code:

var
  Form1:TCLForm;
  BtnMainCode : TclButton;
  Unit1 :TclUnit;
void goToUnit;
{
  Unit1.UnitName = 'uLogin';
  Unit1.CallerForm = Form1;
  Unit1.Run;
}
{
  Form1 = TCLForm.Create(Self);
  Form1.clSetCaption('Ana Kod');
  Unit1 = TclUnit.Create;
  BtnMainCode = Form1.AddNewButton(Form1,'BtnMainCode', 'Go to Unit!');
  BtnMainCode.Width = Form1.clWidth;
  Form1.AddNewEvent(BtnMainCode,tbeOnClick,'goToUnit');
  Form1.Run;
}

uLogin:

var
 Form2:TCLForm;
 Button2 : TclButton;
void AnaKodaGit;
{
 BtnMainCode.Text = 'Came from the uLogin unit to Main Code.';
 Form2.clHide;
 CallerForm.clShow;
}
{
 Form2 = TCLForm.Create(Self);
 Form2.clSetCaption('uLogin');
 Button2 = Form2.AddNewButton(Form2,'Button2','Update the button text in Main Code.');
 Button2.Width = Form2.clWidth;
 Form2.AddNewEvent(Button2,tbeOnClick,'AnaKodaGit');
 Form2.Run;
}

Example 2

Main Code:

var
  MyForm : TclStyleForm;
  BtnRunUnitSample, BtnCustomUnitSample, BtnClickFromUnit : TclButton;
  MyUnit2 :TclUnit;
   
 void BtnSimpleUnitSampleClick;
 {
  Clomosy.RunUnit('MyUnit1');
 }
 
 void BtnCustomUnitSampleClick;
 {
  MyForm.clShow;
  MyUnit2.UnitName = 'MyUnit2';
  MyUnit2.CallerForm = MyForm;
  MyUnit2.Run;
 }
 
 void BtnClickFromUnitClick;
 {
  ShowMessage('Call from Unit2 Form and value : '+BtnClickFromUnit.Hint);
 }
 
 {
  MyForm = TclStyleForm.Create(Self);
  MyUnit2 = TclUnit.Create;
 
  BtnRunUnitSample = MyForm.AddNewButton(MyForm,'BtnRunUnitSample','Click Me!');
  BtnRunUnitSample.Align = alTop;
  MyForm.AddNewEvent(BtnRunUnitSample,tbeOnClick,'BtnSimpleUnitSampleClick');
 
  BtnCustomUnitSample = MyForm.AddNewButton(MyForm,'BtnCustomUnitSample','Run Custom Unit!');
  BtnCustomUnitSample.Align = alTop;
  MyForm.AddNewEvent(BtnCustomUnitSample,tbeOnClick,'BtnCustomUnitSampleClick');
  
  BtnClickFromUnit = MyForm.AddNewButton(MyForm,'BtnClickFromUnit','Click From Unit!');
  BtnClickFromUnit.Align = alTop;
  MyForm.AddNewEvent(BtnClickFromUnit,tbeOnClick,'BtnClickFromUnitClick');
  
  MyForm.Run;
 }

myunit1

var
  MyForm : TclStyleForm;
  BtnRunUnitSample :TclButton;
 
  void BtnRunUnitSampleClick;
  {
    ShowMessage('This is a Simple Hello From Unit');
  }
 
  {
    MyForm = TclStyleForm.Create(Self);
    BtnRunUnitSample = MyForm.AddNewButton(MyForm,'BtnRunUnitSample','CLick!');
    BtnRunUnitSample.Align = alCenter;
    MyForm.AddNewEvent(BtnRunUnitSample,tbeOnClick,'BtnRunUnitSampleClick');
    MyForm.Run;
  }

myunit2

 var
  MyForm : TclStyleForm;
  BtnRunUnitSample :TclButton;
  EntEdit :TclEdit;
 
 void BtnRunUnitSampleClick;
 {
  CallerForm.clShow;
  BtnClickFromUnit.Text = 'Test Click From Unit2';
  BtnClickFromUnit.Hint = EntEdit.Text;
  clDoClick(BtnClickFromUnit);
 }
 
 {
  MyForm = TclStyleForm.Create(Self);
  EntEdit= TclEdit.Create(MyForm);
  MyForm.clGetChild(EntEdit);
  EntEdit.Text= 'type a value for send';
  EntEdit.Align = alTop;
 
  BtnRunUnitSample = MyForm.AddNewButton(MyForm,'BtnRunUnitSample','Click Me!');
  BtnRunUnitSample.Align = alTop;
  MyForm.AddNewEvent(BtnRunUnitSample,tbeOnClick,'BtnRunUnitSampleClick');
 
  MyForm.Run;
 }

Output:

See Also