delphi – 向用户显示累积的消息
发布时间:2020-12-15 09:41:28 所属栏目:大数据 来源:网络整理
导读:我想向用户显示代码执行期间发生的所有相关消息的摘要(例如,解析,算法,转换,验证等).过程完成后,消息应一起显示. 类似的事件可能不会发生,一次或多次.如果事件发生,应通知用户.可能有几种类型的事件. 我不确定方案是否清晰,但也许一些代码会有所帮助: 伪代
我想向用户显示代码执行期间发生的所有相关消息的摘要(例如,解析,算法,转换,验证等).过程完成后,消息应一起显示.
类似的事件可能不会发生,一次或多次.如果事件发生,应通知用户.可能有几种类型的事件. 我不确定方案是否清晰,但也许一些代码会有所帮助: 伪代码: 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; 可执行的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. 当然,迭代中的代码在现实生活中是非常复杂的. 所以… 是否有可用于此的最佳实践或模式? 解决方法
一个简单的解决方案是使用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); 对于格式化或多行消息,您可以将HTML字符串存储在字符串列表中,并使用可以显示HTML格式文本的组件来显示消息. 编辑:如果你不想重复的消息,只需这样做 Log.Duplicates := dupIgnore; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |