리스트박스는 보여 지는 리스트이기에 통상 많이 사용합니다.
일반적으로 리스트박스에 스트링을 저장하기 / 읽어오기를 할 수 있습니다.
즉, ListBox1.Items.Add('할머니');
Edit1.Text:= ListBox1.Items[ListBox1.ItemIndex];
과 같은 방법으로 사용합니다 만, Object 도 저장 할 수 있습니다.
TMyObject = class
No: integer;
Name: string;
constructor Create(const iNo: integer; iName: string);
end;
위와 같은 객체를 저장하기는
var
iMyObject: TMyObject;
begin
iMyObject:= TMyObject.Create(StrToInt(Edit1.text), Edit2.Text);
ListBox1.AddItem(iMyObject.Name, iMyObject);
end;
리스트 박스에는 iMyObject.Name 이 보여지며, 검색하거나 소팅은 기본 적으로 이것으로 할 수 있습니다.
읽어 오기는
var
iMyObject: TMyObject;
begin
if ListBox1.ItemIndex <> -1 then begin
iMyObject:= TMyObject(ListBox1.Items.Objects[ListBox1.ItemIndex]);
Edit1.Text:= IntToStr(iMyObject.No);
Edit2.Text:= iMyObject.Name;
end;
end;
오브젝트로 TStrings에 저장 하였을 경우에는 해당되는 오브젝트를 지워줘야 합니다.
procedure TForm3.FreeObjects(const Strings: TStrings);
var idx: integer;
begin
for idx := 0 to Strings.Count -1 do begin
Strings.Objects[idx].Free;
Strings.Objects[idx]:= nil;
end;
end;
procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeObjects(ListBox1.Items);
end;
리스트박스를 폼에 올려 놓고 크기를 줄이고, Visible 을 False 로 한다음
var iStrings: TStrings; 선언을 해주고
iStrings:= ListBox4.Items; 로 지정해 주고 사용하면 편리 할때도 있습니다.
iStrings[idx] 나 iStrings.Objects[idx] 형식으로 사용하면 되기도 하겠지요...
댓글 없음:
댓글 쓰기