Board index » delphi » Help translating VB code to Delphi.

Help translating VB code to Delphi.

I'm trying to re-write on of my programs written in VB,
to Delphi but I'm having trouble converting some of my
code to it's Delphi equivalent.

Basically I'm trying to fill a ListBox with data and then
allow the end user to manipulate that data. (eg. If the
program is a phone book, I load the names into the ListBox.
Then when the user selects a name, the corresponding data is
retrieved.) The problem is that the ListBox is sorted. So
the position of an item in the ListBox may not be it's actual
position.

In VB, as I load the ListBox I can place the actual position
of the item into the List1.ItemData index. Then regardless
of the items position In the ListBox, I always know its actual
position.

In VB the code looks something like this;

'//fill the list box with data
For S = 1 To RecordCount
    Get #1, S, MyData
    List1.AddItem MyData
    '//the following line is added to support mapping
    List1.ItemData(List1.NewIndex) = S
Next S

If you understand my question and can help me, I'd appreciate
it very much.

Thanks,
Niall.
--
------------------------------------------------------
e-mail nwils...@cairns.net.au
------------------------------------------------------
please remove the * before using.

 

Re:Help translating VB code to Delphi.


Quote
Niall wrote:

> I'm trying to re-write on of my programs written in VB,
> to Delphi but I'm having trouble converting some of my
> code to it's Delphi equivalent.

> Basically I'm trying to fill a ListBox with data and then
> allow the end user to manipulate that data. (eg. If the
> program is a phone book, I load the names into the ListBox.
> Then when the user selects a name, the corresponding data is
> retrieved.) The problem is that the ListBox is sorted. So
> the position of an item in the ListBox may not be it's actual
> position.

> In VB, as I load the ListBox I can place the actual position
> of the item into the List1.ItemData index. Then regardless
> of the items position In the ListBox, I always know its actual
> position.

> In VB the code looks something like this;

> '//fill the list box with data
> For S = 1 To RecordCount
>     Get #1, S, MyData
>     List1.AddItem MyData
>     '//the following line is added to support mapping
>     List1.ItemData(List1.NewIndex) = S
> Next S

> If you understand my question and can help me, I'd appreciate
> it very much.

> Thanks,
> Niall.
> --
> ------------------------------------------------------
> e-mail nwils...@cairns.net.au
> ------------------------------------------------------
> please remove the * before using.

If sorted listbox bothers you, you can turn sorted property off. Does it
help?

Re:Help translating VB code to Delphi.


In article <33B178A6.7...@erols.com>, Vernon Person <s...@erols.com>
writes

Quote
>Niall wrote:

>> I'm trying to re-write on of my programs written in VB,
>> to Delphi but I'm having trouble converting some of my
>> code to it's Delphi equivalent.

>> Basically I'm trying to fill a ListBox with data and then
>> allow the end user to manipulate that data. (eg. If the
>> program is a phone book, I load the names into the ListBox.
>> Then when the user selects a name, the corresponding data is
>> retrieved.) The problem is that the ListBox is sorted. So
>> the position of an item in the ListBox may not be it's actual
>> position.

>> In VB, as I load the ListBox I can place the actual position
>> of the item into the List1.ItemData index. Then regardless
>> of the items position In the ListBox, I always know its actual
>> position.

>> In VB the code looks something like this;

>> '//fill the list box with data
>> For S = 1 To RecordCount
>>     Get #1, S, MyData
>>     List1.AddItem MyData
>>     '//the following line is added to support mapping
>>     List1.ItemData(List1.NewIndex) = S
>> Next S

>> If you understand my question and can help me, I'd appreciate
>> it very much.

>> Thanks,
>> Niall.
>> --
>> ------------------------------------------------------
>> e-mail nwils...@cairns.net.au
>> ------------------------------------------------------
>> please remove the * before using.

>If sorted listbox bothers you, you can turn sorted property off. Does it
>help?

You can simply turn sort off as suggested, or use the AddObject method
to assign data.

i.e.
   for s := 0 to RecordCount - 1 do
   begin
      { get the item of data }
      List1.Items.AddObject(MyData, S);
   end;

You can obviously read the data in place of the commented line and use
the TStrings AddObject method from the Listboxes Items property.

Hope this helps

Mark

Re:Help translating VB code to Delphi.


Niall

sorry my other reply was slightly incorrect, the line

List1.Items.AddObject(MyData, S)

should have read

List1.Items.AddObject(MyData, TObject(S));

The cast seems to work but you might need to create TObject drived
classes for more complex data stored via the Objects property.
Mark

Re:Help translating VB code to Delphi.


Quote
Niall wrote:

[snip]
Quote
> In VB, as I load the ListBox I can place the actual position
> of the item into the List1.ItemData index. Then regardless
> of the items position In the ListBox, I always know its actual
> position.

> In VB the code looks something like this;

> '//fill the list box with data
> For S = 1 To RecordCount
>     Get #1, S, MyData
>     List1.AddItem MyData
>     '//the following line is added to support mapping
>     List1.ItemData(List1.NewIndex) = S
> Next S

[snip]
Niall,
A TStringList, as well as holding strings, can hold associated objects.
This would be a handy construct in which to store your Index to the data
set. See help under e.g. TStringList.AddObject.

Alternately, you could use a TStringGrid, with, say, column 1 as "Name",
Column 2 as "Index", with Column 2 width set to 0 (i.e. invisible).
In this case you probably have to type cast the Index from numeric to
string and back.

A third and much more "native" approach with Delphi would be to set up
your address book as a Paradox database and deal with it directly, using
TDBGrid components for example, using Secondary Indices for sorting.

HTH
Peter Hill.

Other Threads