Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: using *source to access items in tlistbox


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Sep 23, 2015 3:24 PM Last Post By: John MacDonald
John MacDonald

Posts: 179
Registered: 3/22/98
using *source to access items in tlistbox  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 4:05 AM
Hello everyone:

I'm trying to use the source pointer to find out which tlistbox was the source of a drag and drop.

When I tried a dynamic_cast the error tells me it can not make a tlistbox pointer from tobject.
Its been some time since I had to do something like this. As usual I was sure it was easy.

Background- using XE6
I'm using two listboxes as the source of items to be dragged to a third listbox.
On the drop I would like to find out which of the two source listboxes was used so that my code can
go through the appropriate list and get the items.

--
JMAC out

Arkady Semylio

Posts: 87
Registered: 9/18/15
Re: using *source to access items in tlistbox  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 7:07 AM   in response to: John MacDonald in response to: John MacDonald
John MacDonald wrote:
Hello everyone:

I'm trying to use the source pointer to find out which tlistbox was the source of a drag and drop.

When I tried a dynamic_cast the error tells me it can not make a tlistbox pointer from tobject.
Its been some time since I had to do something like this. As usual I was sure it was easy.

Background- using XE6
I'm using two listboxes as the source of items to be dragged to a third listbox.
On the drop I would like to find out which of the two source listboxes was used so that my code can
go through the appropriate list and get the items.

--
JMAC out


Hi.

This is a typical working approach.

void __fastcall TForm1::ListBox2DragDrop(TObject *Sender, TObject *Source, int X,
          int Y)
{
    if ( TListBox* const SourceListBox = dynamic_cast<TListBox*>( Source ) ) {
        ShowMessage( SourceListBox->Name );
    }
}


Bye
John MacDonald

Posts: 179
Registered: 3/22/98
Re: using *source to access items in tlistbox  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 1:10 PM   in response to: Arkady Semylio in response to: Arkady Semylio

Thanks. It does work. I thought I was going a little crazy.

To get my pointer I used " TListBox* LB = dynamic_cast<TListBox*> ( Source );

Now I can use my pointer to find the item(s) I need to transfer.

Thanks so much

Arkady Semylio wrote:
John MacDonald wrote:
Hello everyone:

I'm trying to use the source pointer to find out which tlistbox was the source of a drag and drop.

When I tried a dynamic_cast the error tells me it can not make a tlistbox pointer from tobject.
Its been some time since I had to do something like this. As usual I was sure it was easy.

Background- using XE6
I'm using two listboxes as the source of items to be dragged to a third listbox.
On the drop I would like to find out which of the two source listboxes was used so that my code can
go through the appropriate list and get the items.

--
JMAC out


Hi.

This is a typical working approach.

void __fastcall TForm1::ListBox2DragDrop(TObject *Sender, TObject *Source, int X,
          int Y)
{
    if ( TListBox* const SourceListBox = dynamic_cast<TListBox*>( Source ) ) {
        ShowMessage( SourceListBox->Name );
    }
}


Bye

--
JMAC out
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: using *source to access items in tlistbox  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 3:05 PM   in response to: John MacDonald in response to: John MacDonald
John wrote:

To get my pointer I used " TListBox* LB = dynamic_cast<TListBox*> ( Source
);

There is no functional difference between these two statements:

TListBox* LB = dynamic_cast<TListBox*> ( Source )
 
TListBox* const SourceListBox = dynamic_cast<TListBox*>( Source )


They both have the same Source pointer, and are both performing the same
dynamic_cast. The 'const' in the second statement does not effect the outcome.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: using *source to access items in tlistbox  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 3:04 PM   in response to: John MacDonald in response to: John MacDonald
John wrote:

When I tried a dynamic_cast the error tells me it can not make a
tlistbox pointer from tobject.

What error? A compiler error? A runtime error? dynamic_cast does not report
a runtime error, is you code checking the result of the cast and displaying
its own error?

I'm using two listboxes as the source of items to be dragged to
a third listbox.
On the drop I would like to find out which of the two source
listboxes was used so that my code can go through the appropriate
list and get the items.

Since you only have two controls to check, you don't really need a cast at
all, you could just compare the pointers directly, which would be faster
than performing a runtime RTTI lookup anyway:

TListBox* LB;// = dynamic_cast<TListBox*>(Source);
if (Source == ListBox1)
    LB = ListBox1;
else if (Source == ListBox2)
    LB = ListBox2;
else
    LB = NULL;


--
Remy Lebeau (TeamB)
John MacDonald

Posts: 179
Registered: 3/22/98
Re: using *source to access items in tlistbox  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 3:24 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks Remy:

I was thinking of a possible future and I needed to explore a bit. I was thinking of just the two pointers as well.
I am very happy with the outcome.

Remy Lebeau (TeamB) wrote:
John wrote:

When I tried a dynamic_cast the error tells me it can not make a
tlistbox pointer from tobject.

What error? A compiler error? A runtime error? dynamic_cast does not report
a runtime error, is you code checking the result of the cast and displaying
its own error?

I'm using two listboxes as the source of items to be dragged to
a third listbox.
On the drop I would like to find out which of the two source
listboxes was used so that my code can go through the appropriate
list and get the items.

Since you only have two controls to check, you don't really need a cast at
all, you could just compare the pointers directly, which would be faster
than performing a runtime RTTI lookup anyway:

TListBox* LB;// = dynamic_cast<TListBox*>(Source);
if (Source == ListBox1)
    LB = ListBox1;
else if (Source == ListBox2)
    LB = ListBox2;
else
    LB = NULL;


--
Remy Lebeau (TeamB)

--
JMAC out
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02