Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

星期五, 9月 14, 2007

用ListBox 選擇 Item (包括全選、反向選擇)


新增
5個按鈕TButton
1個TListBox, MultiSelect=true
1個TMemo

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.SelectAll; //全選
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i: integer;
begin
for i:=0 to ListBox1.Count-1 do
begin
ListBox1.Selected[i]:=(not ListBox1.Selected[i]);
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ListBox1.ClearSelection;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
i: integer;
begin
Memo1.Clear; // 先清除Memo1的內容
Memo1.Lines.Add('你這次選了以下的選項');
for i:=0 to ListBox1.Count-1 do
if (ListBox1.Selected[i]) then
Memo1.Lines.Add(ListBox1.Items.Strings[i]);

if (Memo1.Lines.Count=1) then // 沒有選擇任何一個
begin
Memo1.Clear;
Memo1.Lines.Add('你沒有選擇任何一個');
end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
i: integer;
begin
if (ListBox1.SelCount=0) then exit; // 沒有選擇任何一個
for i:=0 to ListBox1.Count-1 do // 找出點選了哪個
if ListBox1.Selected[i] then
Memo1.Lines.Add(ListBox1.Items.Strings[i]); // 加到右邊

ListBox1.DeleteSelected; // 刪除左邊
end;
end.

2 則留言:

匿名 提到...

Hi there! I simply wish to give an enormous thumbs up for the good data you have here on this post.
I will probably be coming back to your blog for extra soon.


Feel free to visit my blog ... sem edx wikipedia

匿名 提到...

Whats up! I simply want to give an enormous thumbs up for the great info you could have here on this post.
I will probably be coming again to your blog for more soon.



My homepage ... semaphore lighting