Multiple lines in a Showmessage in Delphi
I need to fire an error message if the user tries to enter invalid codes in a temporary table in the grid. The codes come from a .txt
. The codes triggered on the monitor are ok but the message is repeated n times as there are n incorrect codes. I would like a single message with the list of incorrect codes to be fired. How to do this? Thanks.
// procedure para leitura do texto
var
myFile : TextFile;
begin
AssignFile(myFile, arquivoTxt);
Reset(myFile);
while not Eof(myFile) do
begin
ReadLn(myFile, text);
InsereTexto;
end;
CloseFile(myFile);
end;
// procedure para inserir dados do txt no banco
begin
if not qrSELCSPCliente.Locate('CSC_CAP_NroCartao', StrToInt(text), []) then
begin
{INSERE DADOS NO BANCO do txt}
else
begin
MsgAviso('Número já cadastrado!' + text);
end;
end;
3 answers
Simple!
Add the character #13
(enter) to the end of each line.
But first, create a temporary variable to accumulate the messages, and display them at once at the end of the process
//procedure1 para leitura do texto
var
myFile : TextFile;
mensagens:String;
begin
mensagens:='Números já cadastrado!';//Inicializa a variável
...
if not qrSELCSPCliente.Locate('CSC_CAP_NroCartao', StrToInt(text), []) then
begin
{INSERE DADOS NO BANCO do txt}
end
else
mensagens:=mensagens+#13+ text);
//veja que aqui ja terminou todo while
if mensagens <>'' then
MsgAviso(mensagens);
end;
Complementing @Ricardo's answer, you have several ways to include a new line in a string. See below several commented examples:
procedure TForm1.Button1Click(Sender: TObject);
var
Msg: string;
begin
Msg := '';
Msg := Msg + 'Linha 01'#13; // (1) Comum de se encontrar em códigos Delphi
Msg := Msg + 'Linha 02'#10; // (2) Padrão Posix (Linux, OSX, Android, iOS, etc.)
Msg := Msg + 'Linha 03'#13#10; // (3) Padrão Windows
Msg := Msg + 'Linha 04' + sLineBreak; // (4) Multiplataforma
Msg := Msg + 'Linha 05' + chr(13); // (5) Mesmo que (1)
Msg := Msg + 'Linha 06' + chr(10); // (6) Mesmo que (2)
Msg := Msg + 'Linha 07' + chr(13) + chr(10); // (7) Mesmo que (3)
Msg := Msg + 'Linha 08' + ^M; // (8) Mesmo que (1)
Msg := Msg + 'Linha 09' + ^J; // (9) Mesmo que (2)
Msg := Msg + 'Linha 10' + ^M^J; // (10) Mesmo que (3)
Msg := Msg + AdjustLineBreaks('Linha 11'#10); // (11) Mesmo que (1), mas ajusta para a plataforma usada
Msg := Msg + AdjustLineBreaks('Linha 12'#13); // (12) Mesmo que (2), mas ajusta para a plataforma usada
Msg := Msg + AdjustLineBreaks('Linha 13'#13#10); // (13) Mesmo que (3), mas ajusta para a plataforma usada
Msg := Msg + 'Linha 14';
ShowMessage(Msg);
end;
I usually use sLineBreak
because it is cross-platform and can avoid future problems if you reuse a code on multiple platforms. But if it's not your case, use what you think is best.
To complete the other answers with a little simpler process!
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Linha 01'+#13+
'Linha 02'+#13+
'Linha 03'+#13+
'Linha 04'+#13+
'Linha 05'+#13+
'Linha 06'+#13+
'Linha 07'+#13+
'Linha 08'+#13+
'Linha 09'+#13+
'Linha 10'+#13+
'Linha 11'+#13+
'Linha 12'+#13+
'Linha 13'+#13+
'Linha 14)';
end;
If you use +#13#13+
it skips two lines.