Results 1 to 10 of 10

Thread: Hoe gebruik ik een ICompare for een ListView (Delphi 2005)

  1. #1
    Old Navigator Matthijs's Avatar
    Join Date
    Mar 2001
    Location
    Ede, NL. Delphi: Delphi 7/2005 :). Matthijs schrijf je Matthijs
    Posts
    2,199

    Hoe gebruik ik een ICompare for een ListView (Delphi 2005)

    Goede morgen,

    Ik zoek me weer eens blauw maar kan het niet vinden:
    In C# doe je iets als:
    Code:
          // ColumnClick event handler.
          private void ColumnClick(object o, ColumnClickEventArgs e){
               // Set the ListViewItemSorter property to a new ListViewItemComparer object.
             this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
               // Call the sort method to manually sort the column based on the ListViewItemComparer implementation.
             listView1.Sort();
          }
    
       }
    
    // ...
       // Implements the manual sorting of items by columns.
       class ListViewItemComparer : IComparer {
          private int col;
          public ListViewItemComparer() {
              col=0;
          }
          public ListViewItemComparer(int column) {
              col=column;
          }
          public int Compare(object x, object y) {
              return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
          }
       }
    Hoe doe ik dit in Delphi2005?
    Ik heb iets geprobeerd als:

    Code:
      myListviewsorter = class(IComparer)  // <--- Fout
      private
        Col: Integer;
      public
        constructor Create(Column: Integer);
        function Compare(X, Y: TObject): Integer;
      end;
    
    procedure TWF_MainForm.ListViewMessageList_ColumnClick(sender: System.Object;
      e: System.Windows.Forms.ColumnClickEventArgs);
    begin
      ListViewMessageList.ListViewItemSorter = myListviewsorter.Create(e.Column);
      ListViewMessageList.Sort;
    end;
    
    { myListviewsorter }
    
    constructor myListviewsorter.Create(Column: Integer);
    begin
      inherited;
      self.Col := Column;
    end;
    
    function myListviewsorter.Compare(X, Y: TObject): Integer;
    begin
      result := CompareStr(TListItem(X).SubItems[Col], TListItem(Y).SubItems[Col]);
    end;
    Maar ik krijg de foutmelding: Class type required

    Matthijs
    What's in a sig?

    Would my posting be less valuable if it didnot have a sig? (Vrij naar William S.)

    Let op de kleine lettertjes. For all postings: e&oe!
    This program performed an illegal function, the police are on their way

  2. #2
    Jan
    Join Date
    Oct 2007
    Location
    Mijdrecht
    Posts
    906
    Of het werkt in Delphi 2005 weet ik niet (ondersteuning generics ?), maar hier een voorbeeld voor D2009:
    http://delphi.about.com/od/delphitip...rt-generic.htm

  3. #3
    Old Navigator Matthijs's Avatar
    Join Date
    Mar 2001
    Location
    Ede, NL. Delphi: Delphi 7/2005 :). Matthijs schrijf je Matthijs
    Posts
    2,199
    Die had ik ook gevonden. Maar zo kan het helaas niet in 2005.
    What's in a sig?

    Would my posting be less valuable if it didnot have a sig? (Vrij naar William S.)

    Let op de kleine lettertjes. For all postings: e&oe!
    This program performed an illegal function, the police are on their way

  4. #4
    Code:
      myListviewsorter = class(TObject, IComparer)
      private
        .....
    Zoiets?
    De beste manier om te leren is door fouten te maken.
    80 procent van alle leugens die jij en ik vertellen blijft onopgemerkt

  5. #5
    Old Navigator Matthijs's Avatar
    Join Date
    Mar 2001
    Location
    Ede, NL. Delphi: Delphi 7/2005 :). Matthijs schrijf je Matthijs
    Posts
    2,199
    Jep, zoiets heb ik nu gedaan.
    Code:
      TmyListviewsorter = class(TObject, IComparer)
      private
        FCol: Integer;
      published
        property Col: integer read FCol write FCol;
      public
        constructor Create;
        function Compare(X, Y: TObject): Integer;
      end;
    
    // ..
    
    procedure TWF_MainForm.ListViewMessageList_ColumnClick(sender: System.Object;
      e: System.Windows.Forms.ColumnClickEventArgs);
    begin
      myListviewsorter := TmyListviewsorter.Create;
      myListviewsorter.Col := e.Column;
      ListViewMessageList.ListViewItemSorter := myListviewsorter;
      ListViewMessageList.Sort;
      myListviewsorter.Free;
    end;
    
    // ..
    
    { myListviewsorter }
    
    constructor TmyListviewsorter.Create;
    begin
      inherited;
      FCol := 0;
    end;
    
    function TmyListviewsorter.Compare(X, Y: TObject): Integer;
    begin
      result := CompareStr(TListItem(X).SubItems[FCol], TListItem(Y).SubItems[FCol]);
    end;
    What's in a sig?

    Would my posting be less valuable if it didnot have a sig? (Vrij naar William S.)

    Let op de kleine lettertjes. For all postings: e&oe!
    This program performed an illegal function, the police are on their way

  6. #6
    Old Navigator Matthijs's Avatar
    Join Date
    Mar 2001
    Location
    Ede, NL. Delphi: Delphi 7/2005 :). Matthijs schrijf je Matthijs
    Posts
    2,199
    Het compileert nu in ieder geval.
    Runtime gaat het helaas nog niet goed en krijg ik een "object refenrence not set ..." in de regel
    Code:
      ListViewMessageList.ListViewItemSorter := myListviewsorter;
    ergens gaat er dus nog iets niet goed. Alleen snap ik nu even niet waarom.

    Ik ben er ondertussen achter dat de error niet op de genoemde regel plaatsvindt, maar in de compare-functie. De objecten die worden meegegeven zijn dus geen TListItem.

    De vraag is nu dus, wat is het dan wel?
    Last edited by Matthijs; 20-Oct-11 at 20:13. Reason: Meer informatie gevonden.
    What's in a sig?

    Would my posting be less valuable if it didnot have a sig? (Vrij naar William S.)

    Let op de kleine lettertjes. For all postings: e&oe!
    This program performed an illegal function, the police are on their way

  7. #7
    Old Navigator Matthijs's Avatar
    Join Date
    Mar 2001
    Location
    Ede, NL. Delphi: Delphi 7/2005 :). Matthijs schrijf je Matthijs
    Posts
    2,199
    Het is dus een ListViewItem.

    En ik maar zoeken naar een TListViewItem.

    Maar goed, probleem opgelost!
    Last edited by Matthijs; 20-Oct-11 at 21:11. Reason: typo
    What's in a sig?

    Would my posting be less valuable if it didnot have a sig? (Vrij naar William S.)

    Let op de kleine lettertjes. For all postings: e&oe!
    This program performed an illegal function, the police are on their way

  8. #8
    mov rax,marcov; push rax marcov's Avatar
    Join Date
    Apr 2004
    Location
    Ehv, Nl
    Posts
    10,357
    Ik zou overigens erven van TInterfacedObject, niet TObject.

  9. #9
    SillyMember
    Join Date
    May 2003
    Location
    Gent
    Posts
    7,725
    Quote Originally Posted by marcov View Post
    Ik zou overigens erven van TInterfacedObject, niet TObject.
    In Delphi.NET was dat hetzelfde iirc.
    All methodologies are based on fear. -- Kent Beck.

  10. #10
    mov rax,marcov; push rax marcov's Avatar
    Join Date
    Apr 2004
    Location
    Ehv, Nl
    Posts
    10,357
    Aw, sorry, had alleen "D2005" gezien, en niet .NET (ik vond de kwalificatie van identifiers in de src al vreemd)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •