在 Forms 应用程序中,我正在显示来自生成大量输出的长时间运行的命令行应用程序的日志输出。我在后台启动程序,捕获它的输出,当前使用 AppendText 在 TextBox 中显示它。例如,我更喜欢只显示最后 1000 行。从 TextBox 中删除行的成本很高,而且 TextBox 并不是滚动日志显示的最佳方法。
关于在 Windows 窗体中执行滚动日志窗口的最佳控件的任何想法?
我曾经让列表框做这种事情。如果行数达到 1000,您只需删除第一行。如果日志行太长,您可以使列表框更宽一些(取决于日志信息以及是否有可能从第一个可见的没有水平滚动的单词)并使水平滚动条可见。
正是我需要的。我用以下代码解决了它,它使最后添加的项目保持可见:
delegate void UpdateCCNetWindowDelegate(String msg);
private void Message2CCNetOutput(String message)
{
// Check whether the caller must call an invoke method when making method calls to listBoxCCNetOutput because the caller is
// on a different thread than the one the listBoxCCNetOutput control was created on.
if (listBoxCCNetOutput.InvokeRequired)
{
UpdateCCNetWindowDelegate update = new UpdateCCNetWindowDelegate(Message2CCNetOutput);
listBoxCCNetOutput.Invoke(update, message);
}
else
{
listBoxCCNetOutput.Items.Add(message);
if (listBoxCCNetOutput.Items.Count > Program.MaxCCNetOutputLines)
{
listBoxCCNetOutput.Items.RemoveAt(0); // remove first line
}
// Make sure the last item is made visible
listBoxCCNetOutput.SelectedIndex = listBoxCCNetOutput.Items.Count - 1;
listBoxCCNetOutput.ClearSelected();
}
}
有同样的需求,非常感谢这种帮助。这是一个稍作修改的版本。
创建一个列表框:
<ListBox x:Name="lbLog" Background="LightGray"></ListBox>
在主线程中(在代码的初始部分),将其用于存储对 UI 线程的引用:
Thread m_UIThread;
....
m_UIThread = Thread.CurrentThread;
然后这是您的日志方法,可从任何线程调用:
public void AddToLog(String message)
{
if (Thread.CurrentThread != m_UIThread)
{
// Need for invoke if called from a different thread
this.Dispatcher.BeginInvoke(
DispatcherPriority.Normal, (ThreadStart)delegate()
{
AddToLog(message);
});
}
else
{
// add this line at the top of the log
lbLog.Items.Insert(0, message);
// keep only a few lines in the log
while (lbLog.Items.Count > LOG_MAX_LINES)
{
lbLog.Items.RemoveAt(lbLog.Items.Count-1);
}
}
}
不久前我需要这样做,而 Listbox 就是解决方案。甚至没有人会注意到差异。
非常简单的解决方案
Textbox1.Appendtext(<yourtext>)
用于控制台等滚动日志