What is #13#10 and #$D#$A in Delphi?
#13#10 and #$D#$A are called as control strings in Delphi which are used to set layout of a string. Both #13#10 and #$D#$A same ASCII codes and used to add new line to a string where the difference is in their representation that #13 is decimal but #$D is hexadecimal . For example, if we want to show a message having 2 lines then we can write following codes.
Showmessage('First line' + #13#10 + 'Second line');
Here in above code #13#10 part represents a carriage return + line feed combination. So the message will show as following.
Some other control strings that Delphi provide
#13 - Carriage return (ASCII)
#10 - Line feed (ASCII)
#$D - Carriage return (Hexadecimal)
#$A - Line feed (Hexadecimal)
#0 - NULL
#9 - Tab
#32 - Space
#9 tab display issue is message dialogs
In latest Delphi versions #9 tabs does not work properly with message dialogs because Windows ignores tabs on buttons and dialogs. So set UseLatestCommonDialogs=False to control whether ShowMessage() displays a custom VCL Form or a Win32 TaskDialog as earlier the string is displayed on a standard TLabel, which draws text using the Win32 DrawText() function with the DT_EXPANDTABS flag enabled but in latter case it is a TaskDialog.
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Message 1'+#9+'tab');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MessageDlg('Message 2'+#9+'tab', mtInformation, mbYesNo,0);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
MessageBox(Handle,'Message 3'+#9+'tab', 'Test', MB_OK);
end;
As you can see above this issue effects Showmessage and MessageDlg function but it works fine with MessageBox function. So for alternate we can use MessageBox function or we can create our own message dialog form. So that we would create a form containing a readonly Memo control. The form would be shown using ShowModal. This has the advantage that the user can copy-paste the text. Using buttons that have the ModalResult property, you can also get the Yes/No result back.
var
sInfo: string;
.........
sInfo := #13 + 'Name:' + #9 + sName + #13 +
'Surname:' + #9 + sSurname + #13 +
'Address:' + #9 + sAddress + #13 +
'E-mail:' + #9 + sEmail + #13 +
'Phone:' + #9 + sCell;
myMessageBox := TmyMessageBoxForm.Create;
try
myMessageBox.Memo1.Text := sInfo;
dlgRes := myMessageBox.ShowModal; // returns the ModalResult
finally
FreeAndNil(myMessageBox);
end;
if dlgRes = mrYes then
ShowMessage('Yes Clicked');
#$D#$A issue with SQL
Sometime we get 'Invalid SQL' error because of #$D#$A characters added at the end of the SQL text where we try to get ADOQuery.SQL.Text property for example.
with ADOQuery1 do
begin
Connection:=ufMain.ADOConnection1;
SQL.Clear;
SQL.Text:='UPDATE Customer SET Name =''' + 'Jack' + ''' WHERE CustomerID=''' + 'Cust1' + '''';
ExecSQL;
end;
So to solve these kind of issue please use Parameters in SQL to pass data. for example
with ADOQuery1 do
begin
Connection:=ufMain.ADOConnection1;
SQL.Clear;
SQL.Text:='UPDATE Customer SET Name = :Name WHERE CustomerID= :CustID ';
Parameters.ParamByName('Name').value := 'Jack';
Parameters.ParamByName('CustID').value := 'Cust1';
ExecSQL;
end;
Parameters.ParamByName('Name').value := 'Cust1';
ReplyDeletebetter is
Parameters.ParamByName('CustomerID').value := 'Cust1';
Thanks for pointing out my mistake. I changed it.
ReplyDelete#13 and #$D are both ASCII. The difference is their representation as decimal or hexadecimal numbers. That should not be mixed up. I kind of stopped reading after seeing that basic mistake, you really should correct that, it deters the semi informed...like me.
ReplyDeleteThanks Sherlock. By mistake I had written ASCII instead of decimal.
ReplyDelete