delphi - 向用户显示累积的消息

标签 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;

可执行的 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.

当然,迭代中的代码在现实生活中非常复杂。 而且消息的信息量也更大,甚至可以格式化和多行。

所以...

是否有可用于此目的的最佳实践或模式?
任何 Delphi 组件可以处理这个问题吗?

最佳答案

一个简单的解决方案是使用 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;

关于delphi - 向用户显示累积的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4826860/

相关文章:

delphi - 发送 smtp 消息时什么 header 控制 "keep on server"功能?

user-interface - 使用屏幕音量 slider 时不应出现音量 UI - iOS

android - 带有菜单按钮的手机上 ActionBar 的正确用户体验?

python - 如何缩进 Python 列表理解?

javascript - 如何在 JavaScript 中为内容添加样式?

在代码中注释 printf() 语句

linux - 适用于 Linux 的 Delphi Tokyo : stream file contents to stdout (console output)

delphi - IdTCPServerExecute 运行但未接收数据

php - Delphi 按位过程转换为 PHP

c++ - 图论算法——工作流图形界面直角箭头排列优化