ListView Drag Drop lazarus / 31-LCL ---------------------------------------------------------------------- Weitentaaal · 2021-05-05 05:59 Hello guys So through my Prev. Post : https://forum.lazarus.freepascal.org/index. php/topic,54449.msg404533.html#msg404533 I was able to Separate Data and Gui but didn't managed to implement this with Drag and Drop. I Know how to make 2 ListViews able to drag and drop but i dont get it how u match ID's of my items with ItemIndex of ListView and how do i Display those Items the correct way ? With Separation in those 2 ListViews i ment Something like : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure TForm1.ListView2Data(Sender: TObject; Item: TListItem);var  i: SizeInt;begin  i := Item.Index;     IF (FData[i].aProperty = ThisandThisState) then begin        //Add To This List    end else begin        //Add to other list    end;end; ---------------------------------------------------------------------- ASerge · 2021-05-05 12:36 A little slurred, I don't understand what is needed. First, I will answer the last question from a closed topic. Why virtual? So that the data representation component does not waste its memory. In addition, it will exclude all sorts of "phantoms". As far as I understand, you have two ListViews of the data. If you want to use a single data source (FData), then you need a selection criterion. If not, I don't see the problem: when dragged the data from FData1 (the indexes of the selected elements are the indexes in the data array), move it to FData2, and then adjust the number of elements in the virtual Listviews. ---------------------------------------------------------------------- Weitentaaal · 2021-05-05 13:14 What i ment was that i  have 2 Lists. 1 Displays All Items wich are here and if u drag it to the Second it wont be Displayed anymore at First List. It's like a Checkstate (First List unchecked second checked). So i thought i use a Data Array wich i get from my Database, in wich all of my items are represented. I Have a Property (boolean) wich will provide me info about the "checkstate" of the Item if True then it should get Displayed in Second Form, if False then in the First. In Short: 2 ListViews, 1 Data Array (How do i provide ListViews with info and make those basic operations (Add, Delete, Edit(Data), Put item from 1 list in the other)) But i don't realy get it how i should do the Data Linking. Pls Help Thanks ---------------------------------------------------------------------- alpine · 2021-05-05 15:21 Since TListItem.Data is of type Pointer you should typecast: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ListView2Data(Sender: TObject; Item: TListItem);var  i: SizeInt;begin  //i := Item.Index;  i := PtrInt(Item.Data);   And when assigning to it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Item.Data := Pointer(PtrInt(i));  as korba812 pointed out in: https://forum.lazarus.freepascal.org/index .php/topic,54449.msg404508.html#msg404508 ---------------------------------------------------------------------- Weitentaaal · 2021-05-06 05:39 I Know how to cast thanks How do i manage my Indexes ? My ID's should not change itself. And why doesn't my ListView dispülay things when i set it to vsReport Edit : Now using ListBox, works better. But i don't understand why i don't have to Free my Object before Deleting an item. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     if (Source is TListBox)  and (TListBox(Source).ItemIndex > -1) then begin // Wenn der Objekttyp stimmt werden die Daten übernommen      Data := GetData(TListBox(Source).Items.Objects[TListBox(Source).ItemIndex]);    Data^.Ausgewaehlt:= True;       ListBox2.Items.AddObject(Data^.Name, TObject(Data)); // Fügt den Text aus dem Edit hinzu       TListBox(Source).Items.Delete(TListBox(Source).ItemIndex);   end;  Do i have to Free The Object before Deleting Item ? bc when i did that i got an error now it works (i guess ). Or can i just delete item bc Object is used from another Item ---------------------------------------------------------------------- alpine · 2021-05-06 13:17 It may be easier to read that way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  LB: TListBox; begin  if (Source is TListBox) then    LB := TListBox(Source) else    Exit;    if LB.ItemIndex > -1 then begin // Wenn der Objekttyp stimmt werden die Daten übernommen      Data := GetData(LB.Items.Objects[LB.ItemIndex]);      Data^.Ausgewaehlt:= True;       ListBox2.Items.AddObject(Data^.Name, TObject(Data)); // Fügt den Text aus dem Edit hinzu       LB.Items.Delete(LB.ItemIndex);   end;  Quote from: Weitentaaal on May 06, 2021, 07:39:15 amDo i have to Free The Object before Deleting Item ? bc when i did that i got an error now it works (i guess ). Or can i just delete item bc Object is used from another Item I don't see freeing of objects in the snippet, as it should be. The creation/freeing of data is up to you and it is not related to AddObject/Delete in TListBoxes. Items.Object[] in the listboxes are just additional pointers to the data. Just be careful not to use this pointers after your data is freed. ---------------------------------------------------------------------- Weitentaaal · 2021-05-06 13:53 Any idea how to iterate through 2 ListBoxes that way ? Edit: Thought about Searching an Item through its ID or check if Item is in first or second Listbox. Is it possible to Create an Array or an object list wich stores all references to the object. Then use this List to Change Propertys of my Object (Will it change in the Listboxes to ? for example when i Store object of item 1 in a list, then change Propertys of Object in List (Example : Name), will it change the Name also in my ListBox ?). Or is there Something wich provides more control over those Items. ---------------------------------------------------------------------- alpine · 2021-05-06 15:28 Quote from: Weitentaaal on May 06, 2021, 03:53:58 pmAny idea how to iterate through 2 ListBoxes that way ? Edit: Thought about Searching an Item through its ID or check if Item is in first or second Listbox. Is it possible to Create an Array or an object list wich stores all references to the object. Then use this List to Change Propertys of my Object (Will it change in the Listboxes to ? for example when i Store object of item 1 in a list, then change Propertys of Object in List (Example : Name), will it change the Name also in my ListBox ?). Or is there Something wich provides more control over those Items. I am not sure I can understand  It is true I'm not a native speaker, may be is the language barrier... ---------------------------------------------------------------------- Weitentaaal · 2021-05-06 15:31 Sorry  I just wanted to know how to search for items in 2 ListBoxes. How can i go through 2 ListBoxes ? ---------------------------------------------------------------------- howardpc · 2021-05-06 15:53 A listbox is a visual control designed to hold a limited number of strings (and, optionally, objects associated with each string). Searching for a particular string item is normally done by setting the listbox's Sorted property, and letting the user scroll to find the string she wants to select. If you prefer to search programmatically, the listbox provides an indexed Items array property which you can enumerate between indexes 0 and Pred(listbox.Items.Count). To search for an object associated with a string item, you can likewise enumerate the Items.Objects array property. You can obviously enumerate the items of as many listboxes as you want. Are you asking for example code to illustrate this? ---------------------------------------------------------------------- alpine · 2021-05-06 16:01 Quote from: Weitentaaal on May 06, 2021, 05:31:06 pmSorry  I just wanted to know how to search for items in 2 ListBoxes. How can i go through 2 ListBoxes ? ListBox1.Items.Objects is an indexed property, you can iterate it with a for loop: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for I := 0 to ListBox1.Items.Count - 1 do    ...; // Do something with ListBox1.Items.Objects[I] I still don't understand what you are trying to achieve. You give very little information on each post. ---------------------------------------------------------------------- speter · 2021-05-06 23:56 I think the OP wants a function to search through 2 listboxes. I suggest following y.ivanov's advice to iterate through the first listbox; if you find the item you are searching for set your return values (aka result) and exit; if you get to the end of the list (without finding the item); iterate through the second listbox... The following is NOT tested Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  Tinfo = record    list : TListBox;    itemnum : integer;  end; function makeit(lb : TListBox; i : integer) : Tinfo;begin  result.list := lb;  result.itemnum := i;end; function findit(thestring : string; listbox1,listbox2 : tlistbox) : Tinfo;var  i : integer;begin  for i := 0 to listbox1.items.count-1 do    if listbox1[i] = thestring then    exit(makeit(listbox1,i));   for i := 0 to listbox2.items.count-1 do  if listbox2[i] = thestring then      exit(makeit(listbox1,i));   result.list := nil;  result.itemnum := -1;end;  cheers S. ---------------------------------------------------------------------- Weitentaaal · 2021-05-07 05:43 Thanks For all ur Reply's Got this far now. Sorry for my Questions but i'm just trying to find good methodes to get better at Lazarus. Got this right now: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  //Insertprocedure readAccessoires();var   i : Integer;   Data : PSelectedZubehoer;begin   //ZubehoerListe Laden   dbExecute(RsZub, 'SELECT * FROM ZubehoerSW ORDER BY ID');    BereSW.ZubListe1.Clear;   if (not RsZub.IsEmpty) then begin      RsZub.First;      while not(RsZub.EOF) do begin         Data := New(PSelectedZubehoer);       Data^.ID:= RsZub.FieldByName('ID').AsInteger;         Data^.Name:= RsZub.FieldByName(FeldD).AsString;;         Data^.Ausgewaehlt := False;          BereSW.ZubListe1.Items.AddObject(Data^.Name, TObject(Data));         RsZub.Next;      end;   end;    //Größte ID holen und von hier jezt die ID's für Selbst def. geben   dbExecute(RsZub, 'SELECT * FROM ZubehoerSW WHERE ID = (SELECT MAX(ID) FROM ZubehoerSW);');   Data := New(PSelectedZubehoer);   Data^.ID:= (RsZub.FieldByName('ID').AsInteger + 1);   //IF (RsCalc.FieldByName('Zubehoer' + (i + 1).ToString).AsString <> '') then Data^.Name:= RsCalc.FieldByName('Zubehoer' + (i + 1).ToString).AsString   {else} Data^.Name:= '...';   Data^.Ausgewaehlt := False;    BereSW.ZubListe1.Items.AddObject(Data^.Name, TObject(Data));end; //Insert with pre-set Values//String is Binary (Example: 101000101) 1 for checked 0 for uncheckedprocedure AccOfData(AccStr: String);var   N, i: Integer;   Data : PSelectedZubehoer;begin   laden:= True;    //write all items in first box   For i := 0 to BereSW.ZubListe2.Items.Count do begin      Data:= Global.ZubGetData(BereSW.ZubListe2.Items.Objects[i]);      Data^.Ausgewaehlt:= False;       Global.ZubChangeList(i, BereSW.ZubListe2, BereSW.ZubListe1);   end;    //check if some items should get into second box   for N := 0 to BereSW.ZubListe1.Items.Count - 1 do begin      if(N > AccStr.Length) or (AccStr[N + 1] = '9')then begin         laden:= False;         BereSW.ZubListeClick(Nil);         Exit;      end;       Data:= Global.ZubGetData(BereSW.ZubListe1.Items.Objects[N]);      if(AccStr[N + 1] = '1')then begin         Data^.Ausgewaehlt:= True;          Global.ZubChangeList(N, BereSW.ZubListe1, BereSW.ZubListe2);      {end else begin         Data^.Ausgewaehlt:= False;          Global.ZubChangeList(N, BereSW.ZubListe2, BereSW.ZubListe1);      }end;   end;    BereSW.ZubListeClick(Nil);   laden:= False;end;  @speter thanks for your Code, i will definitely try it out, but i'm not a great fan of creating a Record just for the Search  .