Forum > Beginners

Observer but more simple

(1/2) > >>

Weitentaaal:
Hello Guys :)

Just a Newbie Question i guess

I did implement a Drag and Drop with 2 listBoxes (every Item Has a Record as Object)... After that i had Some Conditions wich needed to be checked. I had to check if certains items are Checked or not checked (the Rocord has a Property "IsChecked"). The Problem now is that i have 2 listboxes  so i would have to check in First and second Listbox, wich would take many resources bc to check if a item is checked i have to Search it First so i would always have to iterate through both List's to find it and there is not just 1 item wich needs to be checked on so i would iterate so many times through my Lists. Any Good Solution ? i know my english is not so good so i wrote a Summary of my problem:

-2 ListBoxes
-Drag and Drop
-Every Item has a Record Stored wich referes to Data.
-Data is Record with a property "isChecked"

What i have to do:

-Every Time i drag an item in other List i have to check for Some items
-Are they Checked ? (the Property)
-lot of Computing needed if i iterate always through listboxes to search

Is there something like an observer wich checks if certain item is dragged to there and if so then store something ?

lucamar:
You could trigger an event from the record when isChecked changes. Then, in the handler, store the information in e.g. an array and, when you need  to, just check if the item in question is in the array.

Of course, it depends quite a lot on what exactly you're doing and why but that or a similar solution should work well, IMHO.

Thaddy:
That is a way to solve it, but FPC has already a built-in observer pattern for classes:
See https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.html

And that is about the simplest you can get.

winni:

Hi!

There is a simple example in


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fpcsrc/packages/fcl-base/examples/dobserver.pp
Winni

cdbc:
Hi
Every class that descends from TPersistent already have the I/Tobserved implemented.
All you have to do, is implement the IObserver/TObserver
class, Ie: one method or one class with one method.
EasyPeasy... "Use the source Luke"  :D
Ex: Form1 = TForm1(IObserver)
      .....
      Procedure FPOObservedChanged(ASender : TObject; Operation : TFPObservedOperation; Data : Pointer);
      End;
or...
You can roll your own, like this: https://github.com/cdbc-dk/bc_rtl/blob/master/bc_observer_2.pas
Regards Benny

Navigation

[0] Message Index

[#] Next page

Go to full version