全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1827
推到 Plurk!
推到 Facebook!

一個關於ListBox的簡易問題

尚未結案
jxau_zhou23
一般會員


發表:3
回覆:8
積分:2
註冊:2003-04-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-04-28 14:28:10 IP:218.14.xxx.xxx 未訂閱
請問各位大大,我想通過點擊Button ,使ListBox1的一個選項内容轉移到ListBox2中,請問怎麽做? jxau_zhou23
------
jxau_zhou23
ddy
站務副站長


發表:262
回覆:2105
積分:1169
註冊:2002-07-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-04-28 14:34:41 IP:61.218.xxx.xxx 未訂閱
這個是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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-04-28 14:42:34 IP:211.21.xxx.xxx 未訂閱
引言: 請問各位大大,我想通過點擊Button ,使ListBox1的一個選項内容轉移到ListBox2中,請問怎麽做? jxau_zhou23
其實像這一種常用的功能,Delphi已經幫我們寫好一個模組,等著我們去使用,程式都Delphi幫我們寫好了,方法如下:
  • 按Delphi Menu/File/New
  • 撰擇Forms頁夾/撰擇Dual list box
  • 您可撰擇下方的Copy選項,就會有程式碼可以參考
~小弟淺見,參考看看~
------
~小弟淺見,參考看看~
cmj
高階會員


發表:15
回覆:242
積分:226
註冊:2002-06-12

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-04-28 14:47:04 IP:211.76.xxx.xxx 未訂閱
Procedure TFrom1.Button1Click(Sender: TObject);
var i:integer;
begin
  i:=ListBox1.ItemIndex; //ListBox1現在選項位置
  if i>-1 then 
   begin
     ListBox2.Items.Add( ListBox1.Items.Strings[i] ); //加入ListBox2
   end;
end;
jxau_zhou23
一般會員


發表:3
回覆:8
積分:2
註冊:2003-04-05

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-04-28 17:24:15 IP:218.14.xxx.xxx 未訂閱
縣感謝cma大俠,我試出來。我還有一個問題就是:如果ListBox1中 Item 對應的值為0,而ListBox2中Item對應的值為其他的值(假設為1),那麽怎麽將ListBox1中的選項轉移到ListBox2中,並使他的值也相應的變爲1。這各位大大,應怎麽做啊!!! jxau_zhou23
------
jxau_zhou23
cmj
高階會員


發表:15
回覆:242
積分:226
註冊:2002-06-12

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-04-28 18:03:36 IP:211.76.xxx.xxx 未訂閱
引言: 縣感謝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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-04-29 09:45:14 IP:218.14.xxx.xxx 未訂閱
謝謝各位大大,我已經得到我要的答案了。 jxau_zhou23
------
jxau_zhou23
系統時間:2024-05-15 6:54:16
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!