Jør*_*eit 4 delphi user-interface coding-style
我想向用户显示代码执行期间发生的所有相关消息的摘要(例如,解析,算法,转换,验证等).过程完成后,消息应一起显示.
类似的事件可能不会发生,一次或多次.如果事件发生,应通知用户.可能有几种类型的事件.
我不确定方案是否清晰,但也许一些代码会有所帮助:
伪代码:
begin
//Execute computing process//
repeat
Set a flag if an incident occurs
Set another flag if another incident occurs
until done
//Show message to user//
if AnyFlagIsSet then
ShowPrettyMessageToUser
end;
Run Code Online (Sandbox Code Playgroud)
可执行的DELPHI代码:
program Test;
{$APPTYPE CONSOLE}
uses
SysUtils, StrUtils;
var
i: Integer;
tmpFlags: Array[1..4] of Boolean;
tmpMessage: String;
tmpChar: Char;
begin
Randomize;
repeat
//Initialization//
for i := 1 to 4 do
tmpFlags[i] := False;
//Will insident occur?//
for i := 0 to 5 do
begin
if (Random(10) = 0) then tmpFlags[1] := True;
if (Random(10) = 0) then tmpFlags[2] := True;
if (Random(10) = 0) then tmpFlags[3] := True;
if (Random(10) = 0) then tmpFlags[4] := True;
end;
//Show message//
tmpMessage := '';
if tmpFlags[1] then tmpMessage := tmpMessage + IfThen(tmpMessage <> '', #13#10+#13#10) + 'Incident 1';
if tmpFlags[2] then tmpMessage := tmpMessage + IfThen(tmpMessage <> '', #13#10+#13#10) + 'Incident 2';
if tmpFlags[3] then tmpMessage := tmpMessage + IfThen(tmpMessage <> '', #13#10+#13#10) + 'Incident 3';
if tmpFlags[4] then tmpMessage := tmpMessage + IfThen(tmpMessage <> '', #13#10+#13#10) + 'Incident 4';
Writeln('----------');
Writeln(tmpMessage);
Writeln('----------');
Writeln;
Write('Again? (Y/N) ');
Readln(tmpChar);
until tmpChar <> 'y';
end.
Run Code Online (Sandbox Code Playgroud)
当然,迭代中的代码在现实生活中是非常复杂的.并且消息也提供更多信息,甚至可以格式化和多行.
所以...
是否有可用于此的最佳实践或模式?
任何处理这个的Delphi组件?
jpf*_*ius 11
一个简单的解决方案是使用a TStringList
来收集所有消息.然后,您可以在列表框中显示字符串或连接字符串(在这种情况下,所有消息都应该是有效的句子).
伪代码:
procedure DoSomething(Log : TStrings);
begin
//...
Log.Add ('Some hint.');
//...
Log.Add ('Some error happened.');
//...
end;
DoSomething (Log);
if (Log.Count > 0) then
LogListBox.Items.AddStrings (Log);
Run Code Online (Sandbox Code Playgroud)
对于格式化或多行消息,您可以将HTML字符串存储在字符串列表中,并使用可以显示HTML格式文本的组件来显示消息.
编辑:如果你不想重复的消息,只需这样做
Log.Duplicates := dupIgnore;
Run Code Online (Sandbox Code Playgroud)