Simple Example

Simple Example

Note

This topic has not been updated for CrossTalk 2.0 yet. It was written for CrossTalk 1.0, however it still contains useful and relevant information.

Simple Example

CrossTalk Projects are not special Delphi projects. In fact a CrossTalk Project refers to the CTP file used to generate the .pas files used by your Delphi application(s).

Generally each Delphi project that uses CrossTalk will also have an accompanying CTP file. Associated CTP files are matched by having the same name as the Delphi project file. For example: Project1.dpr and Project1.ctp.

It is possible for a single Delphi project could have multiple CTP files, and a single CTP file could be shared among several Delphi projects. Such scenarios however will require manual execution of the generation stage.

Create a Delphi Project

Create a VCL Forms Delphi project and save it.

*CrossTalk also works with C++. The steps are very similar to this tutorial but using C++ project instead of Delphi.

Creating a CTP file

To create a CTP file, access the CrossTalk Project Manager using the menu in Delphi.

Manual Execution

The CrossTalk Project Manager can also be accessed from the CrossTalk program group, or in the CrossTalk installation directory. The executable name is CTClassgen.exe. There is also a command line form usable for automated builds. If you run it manually you will see this screen. If accessed from Delphi, this screen will not be displayed.

Select “Create a new CrossTalk Project”. A Save As dialog will appear. Browse to the directory where you saved the Delphi project. Select a name for your CTP file. It can be named anything. For example Test.ctp.

Next the editor form will appear.

Adding References

If you accessed the CrossTalk Project Options from Delphi, you will see this dialog immediately. If no CTP file exists, CrossTalk will create one automatically

*This dialog has changed in appearance slightly from version 1.0.61.

This form allows the selection of .NET assemblies, and then classes that we want to use in our Delphi project. The Add GAC button allows addition of assemblies that are in the GAC, and the Add User button allows non GAC assemblies to be added. Click the Add GAC button. The Add GAC Assembly form will appear.

Assemblies can be browsed or filtered. For this example lets add mscorlib, where much of the System namespace exists. To do this type “mscor” in the search text field.

Now select mscorlib. The form will display the version and culture. This is useful as assemblies with duplicate names can exist in the GAC, but with different versions and/or cultures.

Now click OK. The main edit form will reappear with mscorlib added to the assembly list.

Now we will add a class from mscrolib. Select mscorlib in the Assembly References list.

Click the Add button to the right of the Types list, and an Add Type form will appear.

Available Types lists the types that are available in mscorlib for use. A search text field also exists. Type ‘stringbu’ into the search text field.

 

Select System.Text.StringBuilder.

 

Click OK.

 

In this example we have added one assembly reference, and selected one class. More can be added but we will continue with only one for simplicity. The CTP file can be edited again at a later date to add more types or assembly references.

Click Save, and then close the CTP Manager. Go to the Delphi project directory and you will see your Delphi project files, and your newly created CTP file.

 

Manual Execution

If you ran the CrossTalk Project Manager manually, you will be returned to this screen. If you accessed it through Delphi IDE, you can skip this section.

Double click on Test.ctp to run the CTP manager again. This time we run the CTP manager to generate the necessary .pas files.

 

Click “Generate …”

 

Close this window. The .pas files are ready and have been generated in our project directory.

 

Switch back to Delphi and test compile the application.

From Delphi Menu

If you accessed the CrossTalk Project Options from within Delphi IDE, you do not need to manually generate the. pas wrappers. CrossTalk automatically creates an associated CTP file. When an associated CTP file exists, CrossTalk will automatically keep up to date the .pas wrappers each time you compile your Delphi/C++ project in the IDE.

If you use the command line compiler outside the IDE, the .pas wrappers will not be updated automatically and you will need to call the command line version of the class generator.

Example: Using .NET StringBuilder

Open the .pas file for the form in the Delphi project and add CTmscorlib to the uses clause.

Now we can use StringBuilder. Add a button to the form and create a Click event. Add the following code.

 

Now run the project and press the button.

 

You have just successfully created a native VCL Forms application that calls a .NET class! This sample was designed to be simplistic to reduce the possibilities for errors. However now you should understand how to add more classes including your own custom .NET classes and use them from Delphi. CrossTalk can use System.XML, .NET DataSet classes, and much more. If you would like more information you can check the topic on using .NET custom classes.