RepeatUntil
RepeatUntil

Reputation: 2330

How to display TStringList items in a MessageBox

I can't figure this out...

Display the TStringList (A1,A2,A3) items in an InputBox.

also, I have tried to use

function InputCombo2(const ACaption, APrompt: string; const AList: TStrings): string;

but this function does not work

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;

Upvotes: 0

Views: 4438

Answers (1)

Johan
Johan

Reputation: 76753

A dialogbox like the one shown using MessageBox, InputBox etc are simply precooked forms.

You want to add extra item to them you'll have to design your own form.

Here's how to do this:

Adding an extra form to your project

Add an extra form to your project: File -> New... -> Form

Drop a ComboBox or ListBox onto the form (I prefer the listbox).
And drop two Buttons.

Set the following properties:

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

This form will be your dialog.

Customize the dialog so it can hold options to display
Add a public property to the form like so:

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

Assign the following code to the OnShow event of the form:

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;

Store the selected item when to user selects it.

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

Showing the form
By default the form will be automatically created, which is fine.

The following code will do the trick:

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;

Something like this should do the trick.

Some info
See: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Forms_TCustomForm_ShowModal.html
Delphi TListBox OnClick / OnChange?

Upvotes: 2

Related Questions