Question

I am working VCL on C++ Builder XE4.

I have following components.

FormMain having

  • TButton *B_select;

FormSelect (or DialogSelect) having

  • TComboBox *CB_list;
  • TButton *B_ok;
  • TButton *B_cancel;

Desired behavior

  1. @FormMain, click B_select to open FormSelect
  2. @FormSelect, select list in CB_list
  3. @FormSelect, click B_ok to pass listindex to FormMain

At step 3 above, I would like to do:

  • close FormSelect
  • pass the listindex to the caller (FormMain)

By using dialog box, I can return one value from Modal Result Constans. http://docwiki.embarcadero.com/RADStudio/XE5/en/Modal_Result_Constants

However when I want to return 1 value from 20 items or more, how can I pass the value using Form/Dialog?

Was it helpful?

Solution

Set B_ok.ModalResult to mrOk. (Delphi code given, as you included Delphi in the tags.)

Use if FormSelect.ShowModal = mrOk to find out if the Ok button was clicked. Read the FormSelect.CB_list.ItemIndex to find out which value was chosen.

Or, better yet, give TFormSelect a public property that contains the ItemSelected. Set it to the CB_list.ItemIndex in the B_ok.OnClick event:

type
  TFormSelect = class(TForm)
   // other declarations
    procedure B_okClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FItemSelected: Integer;
  public
    property ItemSelected: Integer read FItemSelected;
  end;

implementation

procedure TFormSelect.B_okClick(Sender: TObject);
begin
  FItemSelected := CB_list.ItemIndex;
end;

procedure TFormSelect.FormCreate(Sender: TObject);
begin
  FItemSelected := -1;
end;

In the calling code:

SelectForm := TFormSelect.Create(nil);
try
  if SelectForm.ShowModal = mrOk then
    SelectedItem := SelectForm.ItemSelected;
finally
  SelectForm.Free;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top