Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   check TEdit field (https://www.delphipraxis.net/177029-check-tedit-field.html)

question 10. Okt 2013 22:38

check TEdit field
 
Hi,
I have a Form which contains six TEdit component, i would like to check whether the TEdit field is empty or not, i can do it with the following code
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin

 if (Edit1.Text<>'')and (Edit2.Text<>'') and (Edit3.text<>'')
  and (Edit4.Text<>'')and (Edit5.Text<>'') and (Edit6.text<>'')then
    ShowMessage('Edit field not empty ')
  else
    ShowMessage('Edit fieldempty ')

end;
is it possible to do it in another way, for example
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin

If (TEdit(Sender).Text <>'') then
     ShowMessage('Edit field not empty ')
  else
    ShowMessage('Edit fieldempty ')

end;

sx2008 10. Okt 2013 23:57

AW: check TEdit field
 
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
begin
  // Sender contains a reference to "Button1" so the following code
  // is not correct and will fail
If (TEdit(Sender).Text <>'') then
     ShowMessage('Edit field not empty ')
  else
    ShowMessage('Edit fieldempty ')
end;
Here is a little helper function:
Delphi-Quellcode:
function EditHasData(edit:TEdit):Boolean;
begin
  // TrimRight removes blanks
  result := TrimRight(edit.Text) <> '';
end;
Using the helper function you can write:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
begin
  if EditHasData(Edit1)and EditHasData(Edit2) and EditHasData(Edit3)
  and EditHasData(Edit4)and EditHasData(Edit5) and EditHasData(Edit6) then
    ShowMessage('all Edits are filled with data')
  else
    ShowMessage('there is at least one empty Edit');
end;

DeddyH 11. Okt 2013 07:25

AW: check TEdit field
 
Another suggestion:
Delphi-Quellcode:
function DataComplete(const EditArray: array of TEdit): Boolean;
var
  i: integer;
begin
  Result := true;
  for i := Low(EditArray) to High(EditArray) do
    begin
      Result := trim(EditArray[i].Text) <> '';
      if not Result then
        break;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if DataComplete([Edit1, Edit2, Edit3, Edit4, Edit5, Edit6]) then
    ShowMessage('All Edits are filled')
  else
    ShowMessage('At least one Edit is empty');
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:40 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz