优雅登录窗口中的WinForms C# [英] Elegant Log Window in WinForms C#

查看:164
本文介绍了优雅登录窗口中的WinForms C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找到实施Windows窗体应用程序日志窗口的有效途径的想法。在过去,我已经实现了几个使用TextBox和RichTextBox的,但我仍然不完全满意的功能。

本日志是为了与最近的各种事件,在数据采集应用主要用于历史提供用户那里特定交易如何完成了一个可能会好奇。在这种情况下,该日志不必是永久性的,也没有保存到文件中。

This log is intended to provide the user with a recent history of various events, primarily used in data-gathering applications where one might be curious how a particular transaction completed. In this case, the log need not be permanent nor saved to a file.

首先,提出了一些要求:

First, some proposed requirements:


  • 高效快捷;如果数百行被写入日志临门,它需要消耗最少的资源和时间。

  • 能够提供高达2000线左右的变量回滚。更长的东西是不必要的。

  • 高亮和颜色是preferred。字体效果不是必需的。

  • 在达到极限回溯自动修剪线。

  • 自动滚动作为新数据添加。

  • 奖金,但不是必需的:人工交互期间暂停自动滚动,例如,如果用户浏览历史

我有什么一直使用至今写和修剪日志:

What I have been using so far to write and trim the log:

我用下面的code(这是我从其他线程调用):

I use the following code (which I call from other threads):

// rtbLog is a RichTextBox
// _MaxLines is an int
public void AppendLog(string s, Color c, bool bNewLine)
{
    if (rtbLog.InvokeRequired)
    {
        object[] args = { s, c, bNewLine };
        rtbLog.Invoke(new AppendLogDel(AppendLog), args);
        return;
    }
    try
    {
        rtbLog.SelectionColor = c;
        rtbLog.AppendText(s);
        if (bNewLine) rtbLog.AppendText(Environment.NewLine);
        TrimLog();
        rtbLog.SelectionStart = rtbLog.TextLength;
        rtbLog.ScrollToCaret();
        rtbLog.Update();
    }
    catch (Exception exc)
    {
        // exception handling
    }
}

private void TrimLog()
{
    try
    {
        // Extra lines as buffer to save time
        if (rtbLog.Lines.Length < _MaxLines + 10)
        {
            return;
        }
        else
        {
            string[] sTemp = rtxtLog.Lines;
            string[] sNew= new string[_MaxLines];
            int iLineOffset = sTemp.Length - _MaxLines;
            for (int n = 0; n < _MaxLines; n++)
            {
                sNew[n] = sTemp[iLineOffset];
                iLineOffset++;
            }
            rtbLog.Lines = sNew;
        }
    }
    catch (Exception exc)
    {
        // exception handling
    }
}

使用这种方法的问题是每当TrimLog被调用,我失去颜色格式。一个普通的TextBox这个工作得很好(有一点,当然修改)。

The problem with this approach is that whenever TrimLog is called, I lose color formatting. With a regular TextBox this works just fine (with a bit of modification of course).

为解决这一搜索从来没有真正令人满意。有人建议削减超出部分按字符计数,而不是行数在一个RichTextBox。我也看到了使用列表框,但还没有成功的尝试。

Searches for a solution to this have never been really satisfactory. Some suggest to trim the excess by character count instead of line count in a RichTextBox. I've also seen ListBoxes used, but haven't successfully tried it.

推荐答案

我建议您不要使用控制为你的日志在所有。相反,写日志的的类,有你的愿望(不包括显示器属性)的属性。

I recommend that you don't use a control as your log at all. Instead write a log collection class that has the properties you desire (not including the display properties).

然后写,需要到该集合转储到各种用户界面元素的code的一点​​。就个人而言,我会把 SendToEditControl SendToListBox 方法融入我的日志对象。我可能会增加过滤功能,这些方法。

Then write the little bit of code that is needed to dump that collection to a variety of user interface elements. Personally, I would put SendToEditControl and SendToListBox methods into my logging object. I would probably add filtering capabilities to these methods.

您只能经常更新UI日志,因为它是有道理的,为您提供最佳的性能,更重要的是,让您减少开销UI当日志正在迅速变化。

You can update the UI log only as often as it makes sense, giving you the best possible performance, and more importantly, letting you reduce the UI overhead when the log is changing rapidly.

重要的事情是不是要你的日志绑在一块的用户界面,这是一个错误。总有一天,你可以运行无头。

The important thing is not to tie your logging to a piece of UI, that's a mistake. Someday you may want to run headless.

在长远来看,一个记录器一个良好的用户界面可能是一个自定义的控制。但就短期来看,你只是想从任何的具体断开你的日志的一块UI的。

In the long run, a good UI for a logger is probably a custom control. But in the short run, you just want to disconnect your logging from any specific piece of UI.

这篇关于优雅登录窗口中的WinForms C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆