The question arises from the need to reuse a code for different types of objects.
- Scenario:
Drag and Drop in listboxes either from a different listbox or same.
above question arises, when i try to implement drag and drop in same control.
the user can rearrange items in a listbox by dragging them up or down.
I have written the logic, it works.
The problem is, in my app, i have used different kind of listboxes as per requirement.
So their type gets changed. in order to use the same code for drag and drop,
when the source and sender is same, i typecasted different list-boxes to the common ancestor TCustomListBox
and then sent that to the function which performs the moving of items.
The function swaps the items from the items collection to their new positions.
-
Now particular scenario:
i have used a checklistbox which has a checkbox associated with each item.
It so happens, that with each item, there is an object of checkstate associated.
Now in the function, for moving items, i use a tstringlist for temporary storing the items
and their objects and then delete them from the control and then reinsert them in the control.
but in case of this checklistbox, when i delete an item, the item gets deleted,
but its corresponding checkstate object remains and it gets associated to the next item,
which is not desired.
so right now i am using an int variable as a mode indicator for normal,
and special dragdrop.
when it is special, as in case of checklistbox, i typecast it to tchecklist and
then access its delete method which removes item alongwith its checkstate.So instead of using an int as a mode, i was wondering,
whether i can use a class reference of the base class of all listboxes and
then how can i access the properties of a particular listbox type, if the need arises.
the above solution, is not optimum, thats why, i need a more generic and proper way.
See if you can help. thanks. and thanks for the advise, i do care for this community, i am not used to editing features. sorry.
i am using delphi 6.
The declaration for the function is as follows :-
DragDropItemsInListBox(p_HostIndex : Integer; p_HostListBox : TCustomListBox;
p_Mode:Integer);
call:-
the hostindex is the drop site index, hostlistbox is the control on which the item will be dropped,
in this case, the source control and destination control is same,
and mode indicates the way of deleting.
DragDropItemsInListBox(3,TCustomListBox(Source), 1);
if the mode is 0 then i delete items normally as in:-
p_HostListBox.items.delete(i);
if it is 1, which indicates special processing,
in this case as i know what to do, therefore i use,
if p_Mode = 1 then
(p_HostListBox as TRzCheckList).items.delete(i);
this removes the item alongwith the checkstate object.
but i dont wanna rely on this int based differentiation.
can i use metaclasses and if i normally call items.delete,
then will it apply to that specific type?
You can not use metaclass variable – the metaclass pointer is the same for all the objects of the type. If Delphi allowed that – would have no way to know which of checklists to delete from.
However using int is poor solution indeed.
Probably yet better approach to read help about RzChkLst.TRzCheckList.Items – that is property of TStrings type.
Modify the declaration
Call it like DragDropItemsInListBox(0, RzCheckList1, RzCheckList1.Items);
And in procedure do