Recent

Author Topic: know if a text (or char) field is empty  (Read 18800 times)

maxpesa

  • Newbie
  • Posts: 4
know if a text (or char) field is empty
« on: May 26, 2013, 10:27:31 pm »
hello, i'm new here, i searched in the forum if this question was asked before but i found nothing (maybe it is too simple?). So, i want to know if a text field (or a char) is empty (for example a Edit or a Memo).
another question, in c/c++, a string's end comes from the '\0' character, is te same in lazarus?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: know if a text (or char) field is empty
« Reply #1 on: May 26, 2013, 10:32:50 pm »
1) check if a string variable is empty
Code: [Select]
if MyStringVar = '' then writeln('Empty String') else WriteLn('Not an empty string');
2) String type has a hidden length field which keeps the actually length and can be used regardless of character but since the win api expects zero terminated strings it also has a #0 at the end of each string.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

maxpesa

  • Newbie
  • Posts: 4
Re: know if a text (or char) field is empty
« Reply #2 on: May 26, 2013, 10:37:36 pm »
i already tried it, with
Code: [Select]
if (Edit3.Text='') then ShowMessage('!!!')ut i don't get a message

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: know if a text (or char) field is empty
« Reply #3 on: May 26, 2013, 10:45:47 pm »
You'd better show more code then.

Also note that the sysutils unit defines
Code: [Select]
EmptyStr: string = '';
NullStr: PString = @EmptyStr
Using these can aid code clarity.

maxpesa

  • Newbie
  • Posts: 4
Re: know if a text (or char) field is empty
« Reply #4 on: May 26, 2013, 10:50:17 pm »
sysutils is in the uses
Code: [Select]
procedure TForm1.TrovaFenotipoClick(Sender: TObject);
var fgen1,fgen2,ffiglio:fenotipo;
begin
str:=Edit4.Text;
 if (Edit3.Text='') //genitore-genitore
  then
   begin
    showmessage('!!!');
    duegenitori(Edit1.Text,Edit2.Text);
   end;     
                   

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: know if a text (or char) field is empty
« Reply #5 on: May 26, 2013, 10:56:42 pm »
i already tried it, with
Code: [Select]
if (Edit3.Text='') then ShowMessage('!!!')ut i don't get a message

I can only assume that the program flow never reached that line at all or the Edit3.text has one more space characters.
Try something simple eg make that the only line on a button's click event and add a break point to make sure that the line does executes. You can also write something like
Code: [Select]
var
  CompRes : Boolean;
begin
  CompRes := (Edit3.text = '');
  If CompRes then ShowMessage('!!!');
and place a break point on the if line, observe the CompRes value by moving the mouse pointer on the CompRes.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

maxpesa

  • Newbie
  • Posts: 4
Re: [SOLVED]know if a text (or char) field is empty
« Reply #6 on: May 26, 2013, 11:23:47 pm »
YAY, now it works, i didn't change anything, i used the debugger and it worked, i don't now why, but it works
thanks taazz

 

TinyPortal © 2005-2018