一個關於ListBox的簡易問題 |
尚未結案
|
jxau_zhou23
一般會員 發表:3 回覆:8 積分:2 註冊:2003-04-05 發送簡訊給我 |
|
ddy
站務副站長 發表:262 回覆:2105 積分:1169 註冊:2002-07-13 發送簡訊給我 |
這個是delphi 裡面的TDualListDlg,新增一個Item ,在Form 單元裡會看得到 unit Unit2; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Forms, Dialogs, Controls, StdCtrls,
Buttons; type
TDualListDlg = class(TForm)
OKBtn: TButton;
CancelBtn: TButton;
HelpBtn: TButton;
SrcList: TListBox;
DstList: TListBox;
SrcLabel: TLabel;
DstLabel: TLabel;
IncludeBtn: TSpeedButton;
IncAllBtn: TSpeedButton;
ExcludeBtn: TSpeedButton;
ExAllBtn: TSpeedButton;
procedure IncludeBtnClick(Sender: TObject);
procedure ExcludeBtnClick(Sender: TObject);
procedure IncAllBtnClick(Sender: TObject);
procedure ExcAllBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure MoveSelected(List: TCustomListBox; Items: TStrings);
procedure SetItem(List: TListBox; Index: Integer);
function GetFirstSelection(List: TCustomListBox): Integer;
procedure SetButtons;
end; var
DualListDlg: TDualListDlg; implementation {$R *.dfm} procedure TDualListDlg.IncludeBtnClick(Sender: TObject);
var
Index: Integer;
begin
Index := GetFirstSelection(SrcList);
MoveSelected(SrcList, DstList.Items);
SetItem(SrcList, Index);
end; procedure TDualListDlg.ExcludeBtnClick(Sender: TObject);
var
Index: Integer;
begin
Index := GetFirstSelection(DstList);
MoveSelected(DstList, SrcList.Items);
SetItem(DstList, Index);
end; procedure TDualListDlg.IncAllBtnClick(Sender: TObject);
var
I: Integer;
begin
for I := 0 to SrcList.Items.Count - 1 do
DstList.Items.AddObject(SrcList.Items[I],
SrcList.Items.Objects[I]);
SrcList.Items.Clear;
SetItem(SrcList, 0);
end; procedure TDualListDlg.ExcAllBtnClick(Sender: TObject);
var
I: Integer;
begin
for I := 0 to DstList.Items.Count - 1 do
SrcList.Items.AddObject(DstList.Items[I], DstList.Items.Objects[I]);
DstList.Items.Clear;
SetItem(DstList, 0);
end; procedure TDualListDlg.MoveSelected(List: TCustomListBox; Items: TStrings);
var
I: Integer;
begin
for I := List.Items.Count - 1 downto 0 do
if List.Selected[I] then
begin
Items.AddObject(List.Items[I], List.Items.Objects[I]);
List.Items.Delete(I);
end;
end; procedure TDualListDlg.SetButtons;
var
SrcEmpty, DstEmpty: Boolean;
begin
SrcEmpty := SrcList.Items.Count = 0;
DstEmpty := DstList.Items.Count = 0;
IncludeBtn.Enabled := not SrcEmpty;
IncAllBtn.Enabled := not SrcEmpty;
ExcludeBtn.Enabled := not DstEmpty;
ExAllBtn.Enabled := not DstEmpty;
end; function TDualListDlg.GetFirstSelection(List: TCustomListBox): Integer;
begin
for Result := 0 to List.Items.Count - 1 do
if List.Selected[Result] then Exit;
Result := LB_ERR;
end; procedure TDualListDlg.SetItem(List: TListBox; Index: Integer);
var
MaxIndex: Integer;
begin
with List do
begin
SetFocus;
MaxIndex := List.Items.Count - 1;
if Index = LB_ERR then Index := 0
else if Index > MaxIndex then Index := MaxIndex;
Selected[Index] := True;
end;
SetButtons;
end; end.
|
channel
尊榮會員 發表:67 回覆:707 積分:854 註冊:2002-05-02 發送簡訊給我 |
|
cmj
高階會員 發表:15 回覆:242 積分:226 註冊:2002-06-12 發送簡訊給我 |
|
jxau_zhou23
一般會員 發表:3 回覆:8 積分:2 註冊:2003-04-05 發送簡訊給我 |
|
cmj
高階會員 發表:15 回覆:242 積分:226 註冊:2002-06-12 發送簡訊給我 |
引言: 縣感謝cma大俠,我試出來。我還有一個問題就是:如果ListBox1中 Item 對應的值為0,而ListBox2中Item對應的值為其他的值(假設為1),那麽怎麽將ListBox1中的選項轉移到ListBox2中,並使他的值也相應的變爲1。這各位大大,應怎麽做啊!!! jxau_zhou23 不是很了解伙你的意思大概說明如下 1.ListBox.Items.Strings[i]其中i起始值為0,請參閱help檔 2.如果ListBox1之0 -> ListBox2之1 ListBox2.Items.Strings[1]:=ListBox1.Items.Strings[0]; 注意ListBox2必須有1也就是ListBox2.Items.Count至少為2,否則會有問題 3.改ListBox位置如 ListBox1.ItemIndex:=2; 4.ListBox2.Items.Count 表示ListBox2.Items之字串列數 |
jxau_zhou23
一般會員 發表:3 回覆:8 積分:2 註冊:2003-04-05 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |