Skip to content

Instantly share code, notes, and snippets.

@nappa7878
Created May 15, 2011 02:20
Show Gist options
  • Save nappa7878/972834 to your computer and use it in GitHub Desktop.
Save nappa7878/972834 to your computer and use it in GitHub Desktop.
ListBoxの選択したアイテムを上下に移動させる
procedure TForm1.Button1Click(Sender: TObject);
begin
//選択したアイテムを上に移動
ListItemChange(-1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//選択したアイテムを下に移動
ListItemChange(1);
end;
procedure TForm1.ListItemChange(sw: integer);
var
i: integer;
MySelectItemNum: integer;
FileName:TStringList;
FileCount: integer;
DummyStr: string;
begin
FileCount := ListBox1.Items.Count - 1;
MySelectItemNum := ListBox1.ItemIndex;
if (MySelectItemNum+sw >= 0) and (MySelectItemNum+sw <= FileCount) then
begin
FileName := TStringList.Create;
for i := 0 to FileCount do
begin
FileName.Add(ListBox1.Items[i]);
end;
ListBox1.Items.Clear;
DummyStr := FileName[MySelectItemNum];
FileName[MySelectItemNum] := FileName[MySelectItemNum+sw];
FileName[MySelectItemNum+sw] := DummyStr;
for i := 0 to FileCount do
begin
ListBox1.Items.Add(FileName[i]);
end;
ListBox1.ItemIndex := MySelectItemNum+sw;
FileName.Free;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment