Mirror

How to use a loop to catch edit control values (Views: 704)


Problem/Question/Abstract:

I want to check if the user has filled all required DBEdit controls on a notebook, before enabling a button on the form.

Answer:

If you dropped the controls onto the notebook at design time, their Owner will be the form not the notebook. This means that it will not belong to the Components array of the notebook, but of the form. The Notebook's Controls array will be all the controls it parents and that is probably the array you want to loop through.

procedure TAddFrm.SetNextBtn;
var
  I: Integer;
  fld: TControl;
  fldEmpty: Boolean;
begin
  fldEmpty := False;
  with Notebook do
  begin
    for I := 0 to ControlCount - 1 do
    begin
      fld := Controls[i];
      if (fld is TDBEdit) then
      begin
        fldEmpty := TDBEdit(fld).GetTextLen = 0;
        if fldEmpty then
          Break;
      end
    end;
    AddfrmNextBtn.Enabled := not fldEmpty;
  end;
end;

if fldName is TCustomEdit then
  fldEmpty := TCustomEdit(fldName).GetTextLen = 0;

<< Back to main page