From Clomosy Docs

No edit summary
No edit summary
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
TMemo is a multiline text editing control, providing text scrolling. Use TMemo to place a standard multiline edit control on a form. Multiline edit boxes allow the user to enter more than one line of text. They are appropriate for representing large amounts of text. The text in the memo control can be edited as a whole or line by line.
<div class="alert alert-ligth border border-3 border-primary-subtle rounded-5 p-4 shadow-sm" role="alert">
function AddNewMemo(AComponent: TCLComponent; xName,xCaption: string): TclMemo;
</div>


AddNewMemo(xOwner:TComponent; xName,xCaption:String)
<span style="color:blue"><b>AComponent</b></span> : Specifies the parent of the object to be defined.<br>


<span style="color:blue">''TComponent''</span> : The variable name of the defined component is written. Here you should write the component variable name of whatever your component will be in.
<span style="color:blue"><b>xName</b></span> : The name of the defined TclMemo should be written.<br>


<span style="color:blue">''xName''</span> : The name of the defined TclMemo should be written.
<span style="color:blue"><b>xCaption</b></span> : A hint or message to be displayed when the Text property is empty.<br>


<span style="color:blue">''xPromptText''</span> : A hint or message to be displayed when the Text property is empty.
TclMemo is a multiline text editing control, providing text scrolling. Use TclMemo to place a standard multiline edit control on a form. Multiline edit boxes allow the user to enter more than one line of text. They are appropriate for representing large amounts of text. The text in the memo control can be edited as a whole or line by line.<br>


Let's create a TclMemo.<br>
<div class="table-responsive">
{| class="wikitable" style="border: 2px solid #c3d7e0"
! style="background-color: #c3d7e0"| Feature  !!style="background-color: #c3d7e0"| Use of !!style="background-color: #c3d7e0"| Definition
|-
|TclMemo || Memo1 : TclMemo; || A variable belonging to the TclMemo class is created.
|-
|AddNewMemo || Memo1 = Form1.AddNewMemo(Form1,'Memo1','Test Memo Message'); || A new TclMemo is added to the form.
|-
|ReadOnly ||Memo1.ReadOnly = True; || If the value of the component is set to true, it makes the component read-only.
|-
|Add ||Memo1.Lines.Add('New row'); || 'Add' is used to add data to new row in TclMemo.
|-
|Count ||integerValue = Memo1.Lines.Count; || 'Count' is used to get the number of rows in TclMemo.
|-
|Delete ||Memo1.Lines.Delete(i); //i : number of rows to be deleted || Used to delete a line in TclMemo. You must specify the number of rows as a parameter in the delete command.
|-
|Clear || Memo1.Lines.Clear; || Clears all text inside the object.
|-
|WordWrap  || Memo1.TextSettings.WordWrap = True; || When a very long text is typed in TclMemo, the line scrolls sideways. It is used to prevent this, that is, to move to the bottom row when the end of the row is reached.
|-
|SelStart ||Memo1.SelStart = Length(Memo1.Text); || It positions the cursor at the end of the text by setting the length of the text within the TclMemo.
|-
|ScrollTo ||Memo1.ScrollTo(0,Memo1.Lines.Count*Memo1.Lines.Count,True); || In the TclMemo component, the ScrollTo method performs a scrolling operation.
In the example, it scrolls downward vertically by a multiple of its height.
|-
| LoadFromFile ||Memo1.lines.LoadFromFile(MyFileStr,0); //MyFileStr: specifies the file path  || Using the Lines property of the TclMemo component in Clomosy, text can be loaded from a file. The LoadFromFile method loads the text from the specified file path into the TclMemo component, and this text is then displayed within the component.
|-
|SavetoFile ||Memo1.Lines.SavetoFile(MyFileStr,0); || Using the Lines property of the TclMemo component in Clomosy, text can be saved to a file. The SaveToFile method writes the lines of text from the TclMemo component to the specified file path. Passing 0 as the second parameter indicates that the operation will be performed with default options.
|}
</div>


1. Create a new project.<br>


2. You need to define TclMemo on the form. To do this, you should add under the ''var'' parameter on the ide as follows. It is the name of your variable you typed at the beginning. You should define this as you want and add it as TclMemo.<br>
'''var'''
noteMemo : TclMemo;


3. Add the TclMemo to the form. For this, you must add the begin end block and add it inside the form after the form is defined. By saying MyForm.AddNewMemo, we actually add to the form we have defined. Here, you need to add your form definition as whatever you wrote it.<br>
<h2>Example 1</h2><br>
In the example, a text is entered into a TclProEdit. This text is added to TclMemo via the button. We have implemented a simple application for the use of TclMemo. You can improve this application.<br>


noteMemo := ''MyForm''.AddNewMemo(MyForm,'noteMemo','Test Memo Message');
<pre>
 
var
4. If you do not want to define it in the form, you can add it in another component (such as Layout, Panel). Of course, before that, that component must be defined.
  MyForm:TclForm;
<span style="color:blue">testLayout</span> := MyForm.AddNewLayout(MyForm,'testLayout');
  topLyt, btnLyt, bottomLyt : TclLayout;
noteMemo := MyForm.AddNewMemo(<span style="color:blue">testLayout</span>,'noteMemo','Test Memo Message');
  noteEdt : TclProEdit;
 
  addBtn : TclProButton;
5. While defining the component, you can define it manually by typing. Another method is to write its shortcut. If you type "AddNewMemo" while the shortcut is in the code block, a pop-up menu will appear.<br>
  noteAddLbl, notesLbl :TclProLabel;
  mainPanel : TclProPanel;
  noteMemo : TclMemo;
void BtnOnClick;
{
  noteMemo.Lines.Add('* '+noteEdt.Text);
  noteEdt.Text = '';
noteMemo.ScrollTo(0,noteMemo.Lines.Count*noteMemo.Lines.Count,True);
}
   
   
[[File:TclMemoShortcut.png|frameless|400px]]<br><br>
{
 
  MyForm = TclForm.Create(Self);
As soon as you click, the following block will come automatically.<br>
 
  mainPanel=MyForm.AddNewProPanel(MyForm,'mainPanel');
  mainPanel.Align = AlClient;
  mainPanel.Margins.Right = 5;
  mainPanel.Margins.Left = 5;
  mainPanel.Margins.Bottom = 5;
  mainPanel.Margins.Top = 5;
  mainPanel.clProSettings.IsRound = True;
  mainPanel.clProSettings.RoundHeight = 60;
  mainPanel.clProSettings.BorderColor = clAlphaColor.clHexToColor('#3bf5c0');
  mainPanel.clProSettings.BorderWidth = 1;
  mainPanel.SetclProSettings(mainPanel.clProSettings);
 
  topLyt = MyForm.AddNewLayout(mainPanel,'topLyt');
  topLyt.Align=ALMostTop;
  topLyt.Height = 150;
 
  noteAddLbl = MyForm.AddNewProLabel(topLyt,'noteAddLbl','Add Note');
  noteAddLbl.Align = AlMostTop;
  noteAddLbl.Width = 150;
  noteAddLbl.Height = 20;
  noteAddLbl.Margins.Left = 10;
  noteAddLbl.Margins.Top = 20;
  noteAddLbl.clProSettings.FontColor = clAlphaColor.clHexToColor('#030303');
  noteAddLbl.clProSettings.FontSize = 15;
  noteAddLbl.clProSettings.FontVertAlign = palcenter;
  noteAddLbl.clProSettings.FontHorzAlign = palLeading;
  noteAddLbl.clProSettings.TextSettings.Font.Style = [fsBold];
  noteAddLbl.SetclProSettings(noteAddLbl.clProSettings);
 
 
  noteEdt = MyForm.AddNewProEdit(topLyt,'noteEdt','Enter your note...');
  noteEdt.Align = AlTop;
  noteEdt.Height = 45;
  noteEdt.Margins.Right = 10;
  noteEdt.Margins.Left = 10;
  noteEdt.Margins.Top = 10;
  noteEdt.clProSettings.IsRound = True;
  noteEdt.clProSettings.RoundHeight = 10;
  noteEdt.clProSettings.RoundWidth = 10;
  noteEdt.clProSettings.BorderColor = clAlphaColor.clHexToColor('#3bf5c0');
  noteEdt.clProSettings.BorderWidth = 1;
  noteEdt.SetclProSettings(noteEdt.clProSettings);
 
  btnLyt = MyForm.AddNewLayout(topLyt,'btnLyt');
  btnLyt.Align=ALBottom;
  btnLyt.Height = 50;
 
  addBtn = MyForm.AddNewProButton(btnLyt,'addBtn','');
  addBtn.Align = AlRight;
  addBtn.Width = 70;
  addBtn.Margins.Right = 10;
  addBtn.clProSettings.PictureSource = 'https://clomosy.com/demos/add-list.png';
  addBtn.SetclProSettings(addBtn.clProSettings);
  MyForm.AddNewEvent(addBtn,tbeOnClick,'BtnOnClick');
 
  bottomLyt = MyForm.AddNewLayout(mainPanel,'bottomLyt');
  bottomLyt.Align=ALTop;
  bottomLyt.Height = 100;
  bottomLyt.Margins.Top = 20;
 
  notesLbl = MyForm.AddNewProLabel(bottomLyt,'notesLbl','My Notes');
  notesLbl.Align = AlMostTop;
  notesLbl.Width = 150;
  notesLbl.Height = 20;
  notesLbl.Margins.Left = 10;
  notesLbl.Margins.Top = 20;
  notesLbl.clProSettings.FontColor = clAlphaColor.clHexToColor('#030303');
  notesLbl.clProSettings.FontSize = 15;
  notesLbl.clProSettings.FontVertAlign = palcenter;
  notesLbl.clProSettings.FontHorzAlign = palLeading;
  notesLbl.clProSettings.TextSettings.Font.Style = [fsBold];
  notesLbl.SetclProSettings(notesLbl.clProSettings);
 
  noteMemo = MyForm.AddNewMemo(bottomLyt,'noteMemo','');
  noteMemo.Align = alTop;
  noteMemo.Height = 200;
  noteMemo.Width = 150;
  noteMemo.Margins.Left= 10;
  noteMemo.Margins.Right= 10;
  noteMemo.Margins.Top= 10;
  noteMemo.ReadOnly = True;
  noteMemo.TextSettings.WordWrap = True;
 
  MyForm.Run;
}
</pre>


AddNewMemo(xOwner:TComponent; xName,xCaption:String)(xObjectName : String);
<b>Output:</b><br>
[[File:TclMemoExampleScreen.png|frameless|500px]]<br>


6. We gave the variable name while defining the TclMemo in var. Now when you add this in begin end you should use this variable name in all definitions. Your code will throw an error when you write these variable names incorrectly.


7. You can print a message to your TclMemo component as the last parameter when defining your project. This title is the text that will appear on the screen when the application is run. You may not want to write when defining this title. An empty TclMemo will appear on the screen.
<h2>Example 2 (LoadFromFile):</h2><br>
The example creates a form (TclForm) and a memo component (TclMemo), and adds data to a text file at a specified file path, then loads this data into the memo component. First, the Form1 and Memo1 objects are created, and the memo component is added to the form. The SaveFileAndAddData procedure opens or rewrites the file.txt file at the specified file path, adds two lines of text, and then closes the file. In the main program block, if the file.txt file does not exist, the SaveFileAndAddData procedure is called to create the file. Then, the contents of the file.txt file are loaded into the Memo1 component using LoadFromFile.<br>


noteMemo := MyForm.AddNewMemo(MyForm,'noteMemo',<nowiki>''</nowiki>);
<pre>
var
  Form1 : TclForm;
  Memo1: TclMemo;
  MyFileStr: string;


8. Now let's design our TclMemo component. Let's set the width and height first. For this, you must make the following definitions.
void SaveFileAndAddData;
var
  myFile : TextFile;
{
  AssignFile(myFile, Clomosy.AppFilesPath+'file.txt');
  ReWrite(myFile);
  WriteLn(myFile, 'Task 1');
  WriteLn(myFile, 'Task 2');
  ShowMessage('Lines have been added to the file.');
  CloseFile(myFile);
}
 
{
  Form1 = TclForm.Create(self);
  Memo1 = Form1.AddNewMemo(Form1,'Memo1', '--');
  Memo1.Align = alCenter;
  Memo1.Height = Form1.clHeight/2;
  Memo1.Width = 300;
  try
    If not (clFileExists('file.txt',Clomosy.AppFilesPath))
      SaveFileAndAddData;
   
    MyFileStr = Clomosy.AppFilesPath+'file.txt';
   
    // Upload file to Memo1
    Memo1.Lines.LoadFromFile(MyFileStr, 0);
   
  except
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
 
  Form1.Run;
}
</pre>


noteMemo.Height := 50;
<h2>Example 3 (SaveToFile):</h2><br>
noteMemo.Width := 150;
The example creates a form (TclForm) and a memo component (TclMemo), and writes text to a file at a specified path, then saves this text to the memo component. First, the Form1 and Memo1 objects are created, and the memo component is added to the form. The MyFileStr variable specifies the path to the text file. The program opens the file with AssignFile if it does not already exist, and adds three lines of text to the Memo1 component. Then, the SaveToFile method is used to save this text to the saveFile.txt file.<br>


9. With the Align parameter, you can specify where you want our component to be aligned in the form. This parameter has multiple positioning properties. See the [https://www.docs.clomosy.com/index.php/Object_Properties#Align page] to learn about these features. We're going to call it the top part here. So we have to write "AlTop".
<pre>
noteMemo.Align := alTop;
var
 
  Form1 : TclForm;
10. With the Margins parameter, you can give margins at any scale from the right, left, bottom, top.
  Memo1: TclMemo;
 
  MyFileStr: string;
noteMemo.Margins.Left:= 100;
  myFile : TextFile;
noteMemo.Margins.Right:= 100;
noteMemo.Margins.Top:= 100;
noteMemo.Margins.Bottom:= 10;
 
[[File:TclEditMargins.png|frameless|500px]]<br><br>
 
11. You may want to change the appearance of the text in the tag. We will use the TextSettings parameter for this.In order to use this parameter, you have to make the following definition. Otherwise your commands will not work.<br>
noteMemo.StyledSettings := ssFamily;
To adjust the text size;<br>
noteMemo.TextSettings.Font.Size := 20;
 
12. If you want to prevent writing on TclMemo component, you should write the following piece of code.
 
noteMemo.'''ReadOnly''' := True;
 
13. 'Count' is used to get the number of rows in TclMemo. Use of;
 
integerValue := noteMemo.Lines.'''Count''';
 
14. 'Add' is used to add data to new row in TclMemo.
 
noteMemo.Lines.'''Add'''('Hello World');
 
15. If you are going to enter a single line text in TclMemo, the following code can be used.
 
MyMemo.'''Text''' := 'TMemo is a multiline text editing control, providing text scrolling. Use TMemo to place a standard multiline edit control on a form. Multiline edit boxes allow the user to enter more than one line of text. They are appropriate for representing large amounts of text. The text in the memo control can be edited as a whole or line by line.';
 
16. If you want to delete a line in TclMemo, you can use the 'Delete' command. You must specify the number of lines as a parameter in the delete command.
 
noteMemo.Lines.'''Delete(i)'''; //i : number of rows to be deleted
 
17. When a very long text is written in TclMemo, the line shifts sideways. To prevent this, that is, to set it to go to the bottom line when it comes to the end of the line;
 
noteMemo.TextSettings.'''WordWrap''' := True;
18. To tell you the truth, you have created a simple application that does nothing yet. Let's save and start using our project. You can save in one of two ways:<br>
Click the save icon (the button in the upper right corner) or press Ctrl + S to save the project and see what you've done on the platforms now.


{
  Form1 = TclForm.Create(self);
  Memo1 = Form1.AddNewMemo(Form1,'Memo1', '---');
  Memo1.Align = alCenter;
  Memo1.Height = Form1.clHeight/2;
  Memo1.Width = 300;
 
  MyFileStr = Clomosy.AppFilesPath+'saveFile.txt';
  try
    If not (clFileExists('saveFile.txt',Clomosy.AppFilesPath))
      AssignFile(myFile, Clomosy.AppFilesPath+'saveFile.txt');
   
    Memo1.Lines.Add('Row 1');
    Memo1.Lines.Add('Row 2');
    Memo1.Lines.Add('Row 3');
    // Upload saveFile to Memo1
    Memo1.Lines.SaveToFile(MyFileStr, 0);
   
  except
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
 
  Form1.Run;
}
</pre>


= See Also =
<h2> See Also </h2>
* [[Components]]
* [[Object Properties]]
* [[Object Properties]]
* [[AddNewEvent]]
* [[AddNewEvent]]
{{#seo:|title=TclMemo Using in Clomosy - Clomosy Docs}}
{{#seo:|description=Explore TclMemo in Clomosy, a multiline text editor for large text, with features like formatting, word wrap, and file load/save capabilities.}}

Latest revision as of 10:25, 24 April 2025

AComponent : Specifies the parent of the object to be defined.

xName : The name of the defined TclMemo should be written.

xCaption : A hint or message to be displayed when the Text property is empty.

TclMemo is a multiline text editing control, providing text scrolling. Use TclMemo to place a standard multiline edit control on a form. Multiline edit boxes allow the user to enter more than one line of text. They are appropriate for representing large amounts of text. The text in the memo control can be edited as a whole or line by line.

Feature Use of Definition
TclMemo Memo1 : TclMemo; A variable belonging to the TclMemo class is created.
AddNewMemo Memo1 = Form1.AddNewMemo(Form1,'Memo1','Test Memo Message'); A new TclMemo is added to the form.
ReadOnly Memo1.ReadOnly = True; If the value of the component is set to true, it makes the component read-only.
Add Memo1.Lines.Add('New row'); 'Add' is used to add data to new row in TclMemo.
Count integerValue = Memo1.Lines.Count; 'Count' is used to get the number of rows in TclMemo.
Delete Memo1.Lines.Delete(i); //i : number of rows to be deleted Used to delete a line in TclMemo. You must specify the number of rows as a parameter in the delete command.
Clear Memo1.Lines.Clear; Clears all text inside the object.
WordWrap Memo1.TextSettings.WordWrap = True; When a very long text is typed in TclMemo, the line scrolls sideways. It is used to prevent this, that is, to move to the bottom row when the end of the row is reached.
SelStart Memo1.SelStart = Length(Memo1.Text); It positions the cursor at the end of the text by setting the length of the text within the TclMemo.
ScrollTo Memo1.ScrollTo(0,Memo1.Lines.Count*Memo1.Lines.Count,True); In the TclMemo component, the ScrollTo method performs a scrolling operation.

In the example, it scrolls downward vertically by a multiple of its height.

LoadFromFile Memo1.lines.LoadFromFile(MyFileStr,0); //MyFileStr: specifies the file path Using the Lines property of the TclMemo component in Clomosy, text can be loaded from a file. The LoadFromFile method loads the text from the specified file path into the TclMemo component, and this text is then displayed within the component.
SavetoFile Memo1.Lines.SavetoFile(MyFileStr,0); Using the Lines property of the TclMemo component in Clomosy, text can be saved to a file. The SaveToFile method writes the lines of text from the TclMemo component to the specified file path. Passing 0 as the second parameter indicates that the operation will be performed with default options.


Example 1


In the example, a text is entered into a TclProEdit. This text is added to TclMemo via the button. We have implemented a simple application for the use of TclMemo. You can improve this application.

var
  MyForm:TclForm;
  topLyt, btnLyt, bottomLyt : TclLayout;
  noteEdt : TclProEdit;
  addBtn : TclProButton;
  noteAddLbl, notesLbl :TclProLabel;
  mainPanel : TclProPanel;
  noteMemo : TclMemo;
 
void BtnOnClick;
{
  noteMemo.Lines.Add('* '+noteEdt.Text);
  noteEdt.Text = '';
noteMemo.ScrollTo(0,noteMemo.Lines.Count*noteMemo.Lines.Count,True);
}
 
{
  MyForm = TclForm.Create(Self);
   
  mainPanel=MyForm.AddNewProPanel(MyForm,'mainPanel');
  mainPanel.Align = AlClient;
  mainPanel.Margins.Right = 5;
  mainPanel.Margins.Left = 5;
  mainPanel.Margins.Bottom = 5;
  mainPanel.Margins.Top = 5;
  mainPanel.clProSettings.IsRound = True;
  mainPanel.clProSettings.RoundHeight = 60;
  mainPanel.clProSettings.BorderColor = clAlphaColor.clHexToColor('#3bf5c0');
  mainPanel.clProSettings.BorderWidth = 1;
  mainPanel.SetclProSettings(mainPanel.clProSettings);
   
  topLyt = MyForm.AddNewLayout(mainPanel,'topLyt');
  topLyt.Align=ALMostTop;
  topLyt.Height = 150;
   
  noteAddLbl = MyForm.AddNewProLabel(topLyt,'noteAddLbl','Add Note');
  noteAddLbl.Align = AlMostTop;
  noteAddLbl.Width = 150;
  noteAddLbl.Height = 20;
  noteAddLbl.Margins.Left = 10;
  noteAddLbl.Margins.Top = 20;
  noteAddLbl.clProSettings.FontColor = clAlphaColor.clHexToColor('#030303');
  noteAddLbl.clProSettings.FontSize = 15;
  noteAddLbl.clProSettings.FontVertAlign = palcenter;
  noteAddLbl.clProSettings.FontHorzAlign = palLeading;
  noteAddLbl.clProSettings.TextSettings.Font.Style = [fsBold];
  noteAddLbl.SetclProSettings(noteAddLbl.clProSettings);
  
   
  noteEdt = MyForm.AddNewProEdit(topLyt,'noteEdt','Enter your note...');
  noteEdt.Align = AlTop;
  noteEdt.Height = 45;
  noteEdt.Margins.Right = 10;
  noteEdt.Margins.Left = 10;
  noteEdt.Margins.Top = 10;
  noteEdt.clProSettings.IsRound = True;
  noteEdt.clProSettings.RoundHeight = 10;
  noteEdt.clProSettings.RoundWidth = 10; 
  noteEdt.clProSettings.BorderColor = clAlphaColor.clHexToColor('#3bf5c0');
  noteEdt.clProSettings.BorderWidth = 1;
  noteEdt.SetclProSettings(noteEdt.clProSettings); 
  
  btnLyt = MyForm.AddNewLayout(topLyt,'btnLyt');
  btnLyt.Align=ALBottom;
  btnLyt.Height = 50;
  
  addBtn = MyForm.AddNewProButton(btnLyt,'addBtn','');
  addBtn.Align = AlRight;
  addBtn.Width = 70;
  addBtn.Margins.Right = 10;
  addBtn.clProSettings.PictureSource = 'https://clomosy.com/demos/add-list.png';
  addBtn.SetclProSettings(addBtn.clProSettings);
  MyForm.AddNewEvent(addBtn,tbeOnClick,'BtnOnClick');
  
  bottomLyt = MyForm.AddNewLayout(mainPanel,'bottomLyt');
  bottomLyt.Align=ALTop;
  bottomLyt.Height = 100;
  bottomLyt.Margins.Top = 20;
  
  notesLbl = MyForm.AddNewProLabel(bottomLyt,'notesLbl','My Notes');
  notesLbl.Align = AlMostTop;
  notesLbl.Width = 150;
  notesLbl.Height = 20;
  notesLbl.Margins.Left = 10;
  notesLbl.Margins.Top = 20;
  notesLbl.clProSettings.FontColor = clAlphaColor.clHexToColor('#030303');
  notesLbl.clProSettings.FontSize = 15;
  notesLbl.clProSettings.FontVertAlign = palcenter;
  notesLbl.clProSettings.FontHorzAlign = palLeading;
  notesLbl.clProSettings.TextSettings.Font.Style = [fsBold];
  notesLbl.SetclProSettings(notesLbl.clProSettings);
  
  noteMemo = MyForm.AddNewMemo(bottomLyt,'noteMemo','');
  noteMemo.Align = alTop;
  noteMemo.Height = 200;
  noteMemo.Width = 150;
  noteMemo.Margins.Left= 10;
  noteMemo.Margins.Right= 10; 
  noteMemo.Margins.Top= 10;
  noteMemo.ReadOnly = True;
  noteMemo.TextSettings.WordWrap = True;
  
  MyForm.Run;
}

Output:
TclMemoExampleScreen.png


Example 2 (LoadFromFile):


The example creates a form (TclForm) and a memo component (TclMemo), and adds data to a text file at a specified file path, then loads this data into the memo component. First, the Form1 and Memo1 objects are created, and the memo component is added to the form. The SaveFileAndAddData procedure opens or rewrites the file.txt file at the specified file path, adds two lines of text, and then closes the file. In the main program block, if the file.txt file does not exist, the SaveFileAndAddData procedure is called to create the file. Then, the contents of the file.txt file are loaded into the Memo1 component using LoadFromFile.

var
  Form1 : TclForm;
  Memo1: TclMemo;
  MyFileStr: string;

void SaveFileAndAddData;
var
  myFile : TextFile;
{
  AssignFile(myFile, Clomosy.AppFilesPath+'file.txt');
  ReWrite(myFile);
  WriteLn(myFile, 'Task 1');
  WriteLn(myFile, 'Task 2');
  ShowMessage('Lines have been added to the file.'); 
  CloseFile(myFile);
}
  
{
  Form1 = TclForm.Create(self);
  Memo1 = Form1.AddNewMemo(Form1,'Memo1', '--');
  Memo1.Align = alCenter;
  Memo1.Height = Form1.clHeight/2;
  Memo1.Width = 300;
  try
    If not (clFileExists('file.txt',Clomosy.AppFilesPath)) 
      SaveFileAndAddData;
    
    MyFileStr = Clomosy.AppFilesPath+'file.txt';
    
    // Upload file to Memo1
    Memo1.Lines.LoadFromFile(MyFileStr, 0);
    
  except
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
  
  Form1.Run;
}

Example 3 (SaveToFile):


The example creates a form (TclForm) and a memo component (TclMemo), and writes text to a file at a specified path, then saves this text to the memo component. First, the Form1 and Memo1 objects are created, and the memo component is added to the form. The MyFileStr variable specifies the path to the text file. The program opens the file with AssignFile if it does not already exist, and adds three lines of text to the Memo1 component. Then, the SaveToFile method is used to save this text to the saveFile.txt file.

var
  Form1 : TclForm;
  Memo1: TclMemo;
  MyFileStr: string;
  myFile : TextFile;

{
  Form1 = TclForm.Create(self);
  Memo1 = Form1.AddNewMemo(Form1,'Memo1', '---');
  Memo1.Align = alCenter;
  Memo1.Height = Form1.clHeight/2;
  Memo1.Width = 300;
  
  MyFileStr = Clomosy.AppFilesPath+'saveFile.txt';
  try
    If not (clFileExists('saveFile.txt',Clomosy.AppFilesPath)) 
      AssignFile(myFile, Clomosy.AppFilesPath+'saveFile.txt');
    
    Memo1.Lines.Add('Row 1');
    Memo1.Lines.Add('Row 2');
    Memo1.Lines.Add('Row 3');
    // Upload saveFile to Memo1
    Memo1.Lines.SaveToFile(MyFileStr, 0);
    
  except
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
  
  Form1.Run;
}

See Also