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.
NOTE: The difference from the RunUnit method is that it provides access to the objects within the unit from which it was called. This is a specialized unit-calling method in Clomosy.
For example, when navigating from the “Main Code” to the “uLogin” unit, a button and a label are present on the “Main Code” page. When transitioning to the “uLogin” unit with TclUnit, all objects on the “Main Code” page can be accessed directly from the “uLogin” unit.
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;
If you are new to using the TclUnit, try the application shown in Example 1.
Example 1
In this example, a new unit needs to be created when using TclUnit. Before using the sample code, a unit named "uLogin" must be created in your application; otherwise, an error will occur.
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: