Delphi 2 - Moving From VB to Delphi

By: Borland Staff

Abstract: This document is designed to facilitate an understanding of Delphi 2.0 among those with existing knowledge in Visual Basic 4.0.

Moving From VB to Delphi: Command Reference
General Controls
Command Buttons
File Controls
Component Palette

General Controls
There are several component types in Delphi which can be used in the design environment. You will notice when you place a component on a form that the class name for the component (which shows up in the Object Inspector) begins with a "T" for "type." This is a convention that is used in Object Pascal frequently. Therefore, the class name of the button control is TButton, an edit control TEdit and so on. The time it is most important to know this is when you are trying to get help on a component by searching for it by name.

The following table is a list of Visual Basic controls and the corresponding components found in Delphi:

This VB control corresponds to this Delphi component found on this page of the Delphi component palette
Image Timage Additional
Label Tlabel Standard
TextBox Tedit Standard
Frame TGroupBox Standard
CommandButton Tbutton Standard
CheckBox TCheckBox Standard
OptionButton TRadioButton Standard
ComboBox TComboBox Standard
ListBox TListBox Standard
HScrollBar TScrollBar Standard
VScrollBar TScrollBar Standard
Timer Ttimer System
DriveListBox TDriveComboBox System
DirListBox TDirectoryListBox System
FileListBox TFileListBox System
Shape Tshape Additional
OleControl TOleContainer System
Grid TStringGrid Additional
CommonDialog TOpenDialog Dialog
TSaveDialog Dialog
TFontDialog Dialog
TColorDialog Dialog
TPrintDialog Dialog
TPrinterSetupDialog Dialog
TFindDialog Dialog
TReplaceDialog Dialog
Gauge Tgauge Samples
Graph Tchart OCX
MMControl TMediaPlayer Additional
MaskEdBox TMaskEdit Additional
Outline Toutline Additional
SpinButton TSpinButton Samples
SSCommand TBitBtn Additional

In addition, there are several controls included with Delphi that you would need to purchase separately with Visual Basic: the SpeedButton, TabSet, Notebook, Header, Scrollbox, TabbedNotebook and Calendar, and Grid components.

What follows is a discussion of the more prominent control types and how they compare to their VB equivalents.

Forms in Delphi are very similar in function and operation to forms in Visual Basic. They both act as the center of an application and as containers for controls. The following is a list of form properties in VB and their equivalents in Delphi:

Visual Basic Delphi
ActiveControl ActiveControl
ActiveForm ActiveMDIChild
BackColor Color
BorderStyle BorderStyle
Caption Caption
Enabled Enabled
FontBold Font.Style
FontItalic Font.Style
FontName Font.Name
FontSize Font.Size
FontStrikThru Font.Style
FontUnderline Font.Style
ForeColor Font.Color
HDC Canvas
Height Height
HelpContextID HelpContext
Hwnd Handle
Icon Icon
KeyPreview KeyPreview
Left Left
MDIChild FormStyle
MousePointer Cursor
Name Name
Picture Picture
ScaleHeight ClientHeight
ScaleWidth ClientWidth
Tag Tag
Top Top
Visible Visible
Width Width
WindowState WindowState

Where there isn't a direct property relationship, there is alternative functionality such as in the case of DDE and scaling. In addition, there are several methods that the forms have in common, as the following table shows:

Visual Basic Delphi
Circle Cancas.Elipse, Canvas.Arc
Hide Hide
Line Canvas.LineTo
Move SetBounds
PrintForm Print
Print Canvas.TextOut
Refresh Refresh
SetFocus SetFocus
Show Show
TextHeight Canvas.TextHeight
TextWidth Canvas.TextWidth
Zorder BringToFront, SendToBack
(Load) Create
(Unload) Destroy

As you can see, there is a property of a form which is its canvas. It is on the canvas that you do your drawing. As you will see below, there is a canvas associated with every object in Delphi on which you are able to draw.

Finally, there is more overlap in the events available from a Delphi form and those available in VB.

Visual Basic Delphi
Activate OnActivate
Click OnClick
DblClick OnDblClick
Deactivate OnDeactivate
DragDrop OnDragDrop
DragOver OnDragOver
GotFocus OnGotFocus
KeyDown OnKeyDown
KeyPress OnKeyPress
KeyUp OnKeyUp
Load OnCreate
LostFocus OnLostFocus
MouseDown OnMouseDown
MouseMove OnMouseMove
MouseUp OnMouseUp
Paint OnPaint
QueryUnload OnQueryClose
Resize OnResize
Unload OnDestroy

The equivalent of Me is Self in Delphi. You can access the object that triggered an event at any point by using the Sender parameter. To act on the form, you would use the syntax:

TForm(Self).Caption := 'Hello World!';

Because ObjectPascal is such a strongly typed language, you need to cast a generic object variable to a specific type. In the example above, this is done by using the type as if it's a function call—for example, TForm(Self). This allows access to all of the methods and properties of that object.

Command Buttons
The TButton component in Delphi is the equivalent of the CommandButton control type in VB. It is nearly identical in operation to the CommandButton. The CommandButton properties (which are not shared with a form) are below:

Visual Basic Delphi
Cancel Cancel
Default Default
DragIcon DragCursor
DragMode DragMode
Parent Parent
TabIndex TabOrder
TabStop TabStop

The only new method for the CommandButton is the Drag method which has an equivalent in the BeginDrag method of the TButton component. All of the events of the CommandButton are covered in the table for the form object.

The equivalent of the VB TextBox in Delphi is the TEdit component. As with the button objects, the TextBox and TEdit have most properties and methods in common. The really significant difference is that in Delphi there are two controls which correspond to the TextBox in VB: the TEdit and the TMemo components. Here are the equivalent properties of the TextBox, as reflected in Delphi's TEdit and TMemo components:

Visual Basic Delphi
Alignment Alignment (TMemo)
HideSelection HideSelection
MaxLength MaxLength
PasswordChar PasswordChar
Scrollbars Scrollbars (TMemo)
SelLength SelLength
SelStart SelStart
SelText SelText
Text Text
Lines (Tmemo)

The Lines property of the TMemo component is similar to the List property of a Listbox and allows line by line access to the contents of the control. This property is a TStringList object which exists in several places in Delphi and is discussed in conjunction with the ListBox component. There are no new methods in the TextBox and the new event, OnChange, is one the TextBox has in common with the TEdit component.

The Font.Style property is an Object Pascal "set" which is a bit flag variable but with a cleaner syntax than is available in Visual Basic. For example, the code behind the chkBold checkbox looks like this:

procedure TForm1.chkBoldClick(Sender: TObject);


  if TCheckBox(Sender).State = cbChecked then

    txtDisplay.Font.Style := txtDisplay.Font.Style + [fsBold]


    txtDisplay.Font.Style := txtDisplay.Font.Style - [fsBold];


Simply by adding the appropriate constant in brackets, you add that attribute to the style and you remove it by subtracting it.

Once again, the TListBox component in Delphi is very similar to the ListBox control in Visual Basic. Here are the properties of a ListBox:

Visual Basic Delphi
Columns Columns
ItemData Items.Objects*
List Items
ListCount Items.Count
ListIndex ItemIndex
MultiSelect MultiSelect*
NewIndex (Items.Add)
Selected Selected*
Sorted Sorted
TopIndex TopIndex
*Not a direct equivalent

The primary methods a little different because they are the methods of the Items collection but the methods for the ListBox are as follows:

Visual Basic Delphi
AddItem Items.Add
Clear Clear
RemoveItem Items.Delete

The contents of a ListBox are in the Items property, which is much like the List property in VB except it is more than an array; it is also a class of type TStringList. This object has many interesting features but fundamentally, manipulation of the list takes place via methods of the Items property.

A VB example of Listbox functionality can be found in the LISTBOX.FRM example in the SAMPLESCONTROLSCONTROLS.MAK sample Visual Basic project. This is a good demonstration of the differences in using the TListbox and the VB ListBox. In the VB sample, the Add button executes the following code:

Sub cmdAdd_Click ()

    lstClient.AddItem txtName.Text

    txtName.Text = ""


    lblDisplay.Caption = Str(lstClient.ListCount)

End Sub

The equivalent code in Delphi looks something like this:

procedure TfrmListBox.cmdAddClick(Sender: TObject);


     lstClient.Items.Add (txtName.Text);

     txtName.Text := '';


     lblDisplay.Caption := IntToStr (lstClient.Items.Count);


The first thing you should notice is that the Add method is on the Items property, not the listbox itself. A brief aside is that you must surround the parameters of both subroutines and functions in parentheses, unlike in Visual Basic where subroutines (and methods) don't use parentheses. Second, you should see that the use of variants is not recommended in Delphi any more than it is in VB. This means that you need to explicitly convert data types for assignment. Therefore, in order to display the Items.Count property in the label lblDisplay, use the IntToStr function which is similar in functionality to STR in VB.

The code behind the VB Remove button looks like this:

lstClient.RemoveItem lstClient.ListIndex

while the equivalent Delphi code looks like this:


where again the Delete method is supported by the Items property. In both VB and Delphi, the Clear method is a method of the listbox object itself so the code looks identical except for the semicolon on the end of the line of Delphi code.

A major advantage of the TStringList property type is that it is compatible with lots of other properties so the following represent working statements in Delphi:

ListBox1.Items := Memo1.Lines;

ListBox2.Items := Screen.Fonts;


The TImage component of Delphi is equivalent to the Image control in Visual Basic except that it has the equivalent drawing functionality of a PictureBox, thereby allowing it to serve the same purpose as both controls in VB. It is a lightweight control just like the Image control and shares a number of properties in common with the Image and the PictureBox. The core display property is the Picture property which operates in much the same way in both environments.

An example of pictureboxes in VB is the SAMPLESFIRSTAPPBUTTERF.MAK demo shipped with Visual Basic. The timer code in Visual Basic looks like this:

Sub Timer1_Timer ()

     Static PickBmp As Integer

     Main.Move (Main.Left + 20) Mod ScaleWidth,_

               (Main.Top - 5 + ScaleHeight) Mod ScaleHeight

     If PickBmp Then

        Main.Picture = OpenWings.Picture


        Main.Picture = CloseWings.Picture

     End If

     PickBmp = Not PickBmp

End Sub

whereas the equivalent code in Delphi looks pretty much the same:

procedure TForm1.Timer1Timer(Sender: TObject);


     PickBmp:Boolean = False;


     Main.SetBounds ((Main.Left + 20) Mod ClientWidth,

                     (Main.Top - 5 + ClientHeight) Mod


     if PickBmp = True then

         Main.Picture := OpenWings.Picture


         Main.Picture := CloseWings.Picture;

     PickBmp := Not PickBmp;


There are a couple of differences worth noting. First, the SetBounds method, unlike the Move method in VB has no optional parameters so you need to supply the Width and Height values. Second, there is no so-called static variable type in Delphi. Instead you may use a typed consant in its place. The point is that the assignment to the Picture property works just like it does in VB.

Another key difference is that instead of a LoadPicture function that returns a picture, the Picture property of a TImage has its own LoadFromFile method as demonstrated in the next section.

File Controls
Just as in Visual Basic, there are a set of file-oriented components in Delphi for the construction of browsers and customized file dialogs. These components are the TDirectoryListBox, TFileListBox, TDriveComboBox and TFilterComboBox. There is no equivalent to the FilterComboBox in VB but you have seen similar functionality in the Filter property of the common dialog control.

The TDriveComboBox is the counterpart to the DriveListBox in VB. The relevant property in both is the Drive property. The primary difference is that the DirectoryListBox in Delphi also has a Drive property for direct assignment from the DriveComboBox. Therefore, in place of the code

Sub Drive1_Change ()

    Dir1.Path = Drive1.Drive

End Sub

in Visual Basic, the Delphi equivalent is:

procedure TForm1.Drive1Change(Sender: TObject);


     Dir1.Drive := Drive1.Drive;


The TDirectoryListBox and DirListBox are the Delphi and Visual Basic components to represent a directory. Both display a similar hierarchical structure. The core property of the TDirectoryListBox is Directory which is the equivalent of the Path property in VB's DirListBox. So the following code in VB:

Sub Dir1_Change ()

    file1.Path = Dir1.Path

End Sub

translates to the following in Delphi:

procedure TForm1.Dir1Change(Sender: TObject);


     File1.Directory := Dir1.Directory;


Certainly, you can see that the operation of these controls is very similar in Visual Basic and Delphi. Most of the differences you encounter are subtle changes to the object design.

Finally, there comes the TFileListBox control which is the counterpart to the FileListBox in Visual Basic. Again, the operation of these controls is similar but there are enough differences that they bear closer scrutiny. Here are the relevant properties of each:

Visual Basic Delphi
Archive FileType
FileName FileName
Hidden FileType
Normal FileType
Path Directory
Pattern Mask
ReadOnly FileType
System FileType

As you can see, the biggest difference between the two controls is the selection of file types to display. In VB, this is a set of Boolean properties whereas in Delphi it is a set property type like the Style property of a TFont object. In the Object Inspector, you simply double-click on FileType to expand the component choices. In addition to the choices in VB, there is also the ability in the TFileList to include the directories and volume id. To change these values programmatically, you simply add or subtract the constants from the property. In other words:

File1.FileType := File1.FileType + [fsDirectory] - [fsHidden];

File1.FileType := File1.FileType + [fsNormal] + [fsSystem];

would be valid operations with the FileType property. If you want to check membership in a set, you simply use the in operator. For example,

if fsHidden in File1.FileType then ...

is the code you would use to test whether the TFileListBox was displaying hidden files.

The operation of the two file lists is similar so that the following code from PICVIEW.MAK:

Sub File1_DblClick ()

' When at the root level (for example, C:) the Path property

' has a backslash () at the end.  When at any other level,

' there is no final .  This code handles either case to build

' the complete path and filename of the selected file.

      If Right(file1.Path, 1) <> "" Then

        label1.Caption = file1.Path & "" & file1.FileName


        label1.Caption = file1.Path & file1.FileName

      End If

' Load the selected picture file. = LoadPicture(label1.Caption)

End Sub

would look like this in Delphi:

procedure TForm1.File1DblClick(Sender: TObject);


     if Length(File1.Directory) = 3 then

         Form1.Caption := File1.Directory + File1.FileName


         Form1.Caption := File1.Directory + '' + File1.FileName;

     Image1.Picture.LoadFromFile (Form1.Caption);


The only difference here is the result of no Right function in Delphi. You could code it with Copy command in Delphi (which is like MID, see string handling below) but it was just as simple to check the length instead.

Menus are handled a little differently in Delphi than in VB and allow for much greater flexibility. However, the common functionality between them is fairly straightforward.

To create a menu for a form in Delphi, place a TMainMenu component on the form. By default, this is the first control in the Standard page of the Component palette. This component encapsulates all the functionality of the menu. To get to the Menu Designer, simply double-click the main menu component. The Delphi Menu Designer looks very much like a menu. When it first appears, there is one blank menu item. Simply type in a caption. You will notice that the Object Inspector will record what you type in the Caption property and, when you press Enter, will use the menu caption to generate a default name for this menu item.

Once you press Enter, you can start typing the caption of the first item in this menu and so on. When a particular menu item is selected, it appears in the Object Inspector. Menu items in Delphi have similar properties to menu items in Visual Basic.

Visual Basic Delphi
Caption Caption
Checked Checked
Enabled Enabled
ShortCut ShortCut
Tag Tag

Menu items are used the same way for the most part. An "&" in the caption creates an accelerator. Using a hyphen "-" as the caption creates a separator bar in the menu.

Unlike in the menu designer in VB, you are directly manipulating menu items. Therefore, to move a menu item, you can simply drag it from one place in the menu to another, including to another menu. For example, you can drag an item from the File menu to the Edit menu. To create a sub-menu, right-click a menu item and choose Create Sub-menu or use Ctrl-Right Arrow to create it directly.

To associate code with a menu item, simply double-click the item in the menu designer to get to the OnClick event of the menu item. Bear in mind that the only way to see these menu items is to open the Menu Designer by double-clicking on the TMainMenu component.

This section doesn't really do justice to the menu functionality of Delphi. Be sure to read the documentation on merging menus and on creating them dynamically. While there are no control arrays as such in Delphi, the ability to dynamically create objects provides even more flexibility and power. A method of emulating control arrays is discussed below.

ActiveX Control Support
Delphi provides robust support for ActiveX Controls (OCX). However, support for data binding, specific to Visual Basic is not supported at this time. This is proprietary to Visual Basic and deviates from the generic OCX specification.

To add a OCX control to the component library,

  1. Open the Install Components dialog box.
  2. Choose OCX to open the Install OCX File dialog box.
  3. Navigate until you locate the .OCX file you want to add, then choose OK.
  4. Once you have added all the OCX controls and other modules you want, choose OK to close the Install Components dialog box and rebuild the component library.

Delphi will then build a wrapper VCL component around the OCX control, thereby integrating it into the Delphi environment. The OCX is not directly linked into your application in any way. Instead it remains as a separate and shareable resource. As such it is important to remember that whatever license file was required to use it in Visual Basic will continue to be required to use it in the Delphi development environment. Note that some OCX controls will only operate properly if they are registered. Make sure your setup procedure registers any OCXs you'll be using as this is not automatic.

The Component Palette
Now is a good time to mention the differences in the component palette and the toolbox in Visual Basic. In Visual Basic, you define a set of custom controls to be associated with a particular project. By contrast in Delphi, custom components become part of the development environment itself when installed. Of course, in the case of OCXs, this is only a wrapper but the OCX remains "installed", if not loaded into memory beyond the current project.

This is a sensible and beneficial approach, because the overhead associated with Delphi components is much less than that of OCXs in VB. In Visual Basic, each OCX is a separate dynamic-link library (though it may contain multiple controls) and as such consumes memory and resources. Therefore, in VB it is very important to limit the number of OCXs you keep loaded during development, because you will quite literally run out of resources.

By contrast, there are three reasons this situation is far better and more efficient in Delphi. First, components are typically smaller because they are all inherited from other components you already have installed. For example, the TFileList component is based on the TListbox component which is already loaded. Second, the code behind each component is compiled down to native code when installed in the IDE. For example, the DLL containing all of the standard controls is just over 1 Mb in size. Third, all of the installed components are added to a single dynamic-link library for greater memory and resource efficiency.

That said, it is possible to control which custom components are loaded at a particular point by changing the dynamic-link library currently installed in the IDE. At any point, you can make a copy of the current COMPLIB.DCL under a new name. You can then load that library at some future point. Bear in mind that, like in Visual Basic, it is essential you have all of the required controls installed into the IDE when using a project that requires them. This is not the case with distribution of your completed applications, as Delphi Visual Component Library (VCL) components are linked directly into your EXE file.

Server Response from: ETNASC02