掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
TObjectでご指導ねがいます。 (ID:37317)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
ListBoxのitemsはTStringsなのでソートは難しいです。 データは別に管理してListBoxは表示に専念させた方が簡単す。 こんな感じ、 今回は、ソースをコピペするだけで動きます。 オブジェクトの貼付け不要す。 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TBlood = (UnKnown,A,B,AB,O); function BloodToString(aBlood:TBlood):string; type TValues = class(TObject) public Name : string; Age : Byte; Blood : TBlood; procedure SetValue(aName:string; aAge:Byte; aBlood:TBlood); function GetString:string; end; TForm1 = class(TForm) Button1: TButton; private { Private 宣言 } fList : TList; fListBox : TListBox; fButton : TButton; procedure AddValue; public { Public 宣言 } constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure fButtonClick(Sender: TObject); procedure SortByAge; procedure ListUp; procedure ListClear; end; var Form1: TForm1; implementation {$R *.dfm} function BloodToString(aBlood:TBlood):string; begin case aBlood of UnKnown: Result:= '-'; A : Result:= 'A'; B : Result:= 'B'; AB : Result:= 'AB'; O : Result:= 'O'; end; end; function SortAge(Item1, Item2: Pointer): Integer; begin result := TValues(Item1).Age - TValues(Item2).Age; end; { TValues } function TValues.GetString: string; begin Result := Name + ' ' + IntToStr(Age) + '歳 ' + BloodToString(Blood) + '型'; end; procedure TValues.SetValue(aName: string; aAge: Byte; aBlood: TBlood); begin Name := aName; Age := aAge; Blood := aBlood; end; { TForm1 } procedure TForm1.AddValue; var aValue : TValues; begin aValue := TValues.Create; aValue.SetValue('山田',30,A); fList.Add(aValue); aValue := TValues.Create; aValue.SetValue('田中',50,B); fList.Add(aValue); aValue := TValues.Create; aValue.SetValue('斉藤',11,AB); fList.Add(aValue); aValue := TValues.Create; aValue.SetValue('鈴木',25,UnKnown); fList.Add(aValue); aValue := TValues.Create; aValue.SetValue('山口',80,AB); fList.Add(aValue); aValue := TValues.Create; aValue.SetValue('千葉',31,A); fList.Add(aValue); ListUp; end; constructor TForm1.Create(AOwner: TComponent); begin inherited; fList := TList.Create; fListBox := TListBox.Create(self); fListBox.Align := alClient; fListBox.Parent:= self; fListBox.Show; fButton := TButton.Create(self); fButton.Caption := '年齢順'; fButton.Parent := fListBox; fButton.Left := fListBox.Width - fButton.Width; fButton.Anchors := [akTop,akRight]; fButton.OnClick := fButtonClick; AddValue; end; destructor TForm1.Destroy; begin ListClear; fButton.Free; fListBox.Free; fList.Free; inherited; end; procedure TForm1.fButtonClick(Sender: TObject); begin SortByAge; end; procedure TForm1.ListClear; var i:Integer; aValue : TValues; begin for i := fList.Count-1 downto 0 do begin aValue := fList.Items[i]; aValue.Free; end; fList.Clear; end; procedure TForm1.ListUp; var i:Integer; aValue : TValues; begin fListBox.Clear; for i := 0 to fList.Count - 1 do begin aValue := fList.Items[i]; fListBox.AddItem(aValue.GetString,nil); end; end; procedure TForm1.SortByAge; begin fList.Sort(SortAge); ListUp; end; end.
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.