2014-04-26 20 views
0

我無法弄清楚這一點......如何在消息框中顯示TStringList項目

在InputBox中顯示TStringList(A1,A2,A3)項目。

也,我曾嘗試使用

功能InputCombo2(常量ACaption,APrompt:字符串; const的ALIST: 字符串列表):字符串;

但這功能不起作用

var 
List: TStringList; 


if Not FileExists(CradTypeText) 
then 
    Begin 
     List := TStringList.Create; 
     List.Add('A1'); 
     List.Add('A2'); 
     List.Add('A3'); 
     repeat 
       CardTypeStr := InputBox('Card Recharger', 'Please select the card', List); 
     until (CardTypeStr = 'A1') or (CardTypeStr = 'A2') or (CardTypeStr = 'A3'); 
     //ShowMessage(iStr);//Test 
     AssignFile(myFile, CradTypeText); 
     ReWrite(myFile); 
     WriteLn(myFile, CardTypeStr); 
     CloseFile(myFile); 
     List.Free; 
    End 
    Else 
     Begin 
      IDEdt.Enabled := False; 
      AssignFile(myFile, IDtext); 
      Reset(myFile); 
      ReadLn(myFile, CardTypeStr); 
      IDEdt.Text := CardTypeStr;//Test 
     End; 
+1

'ShowMessage(StringList.Text);'? – TLama

+0

@TLama我想要用戶選擇其中一個項目,然後他點擊確定按鈕,如果他關閉了窗口,或者他點擊取消按鈕,窗口將重複,直到他選擇了一個項目 – RepeatUntil

+0

@TLama選擇其中一個項目顯示爲組合框 – RepeatUntil

回答

2

喜歡使用MessageBoxInputBox等被簡單地預煮形式示出的一個所述的對話框。

你想添加額外的項目給他們,你將不得不設計自己的形式。

這裏是如何做到這一點:

增加一個額外的表格到您的項目

添加一個額外的表格到您的項目:文件 - >新建 - >表

降一個組合框或列表框(我喜歡列表框)。
並放下兩個按鈕。

設置以下屬性:

Button1.ModalResult:= mrOK; 
Button2.ModalResult:= mrCancel; 

這種形式將是你的對話框。

自定義對話框,以便它可以容納選項來顯示
的公共屬性添加到窗體像這樣:

TForm2 = class(TForm) 
private 
    FOptions: TStringList; 
    FChoosenOption: string; 
    .... 
public 
property Options: TStringList read FOptions write FOptions; 
property ChoosenOption: string read FChoosenOption; 
.... 
end; 

下面的代碼分配給窗體的OnShow中事件:

procedure TForm2.Form2Show(Sender: TObject); 
var 
    i: integer; 
begin 
    if Assigned(FOptions) then begin 
    ListBox1.Items.Clear; 
    for i:= 0 to FOptions.Count -1 do begin 
     ListBox1.Items.Add(FOptions[i]); 
    end; {for} 
    end; {if} 
end; 

當用戶選擇它時,存儲所選項目。

procedure TForm2.ListBox1Click(Sender: TObject); 
begin 
    self.FChoosenOption:= ListBox1.Items[ListBox1.ItemIndex]; 
end; 

顯示形式
默認的形式將被自動創建,這是好的。

下面的代碼將這樣的伎倆:

procedure TForm1.BtnShowMeOptionsClick(Sender: TObject); 
begin 
    Form2.Options:= MyListOfOptions; 
    case Form2.ShowModal() of 
    mrOK: begin 
     self.OptionPicked:= Form2.ChoosenOption; 
    end; 
    mrCancel: begin 
     self.OptionPicked:= ''; 
    end; 
    end; {case} 
end; 

像這樣的東西應該做的伎倆。

一些信息
參見:http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Forms_TCustomForm_ShowModal.html
Delphi TListBox OnClick/OnChange?