1、实现功能
限制ListBox显示的最大条数;
ListBox自动滚动,显示最新行;
2、C#代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
namespace ListBoxApp
{
public partial class Form1 : Form
{
Timer timer;
bool isRun = false;
int count = 0;
int MAX_COUNT = 20;
string filename = "";
/// <summary>
/// 构造函数
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
timer = new Timer();
timer.Interval = 500;
timer.Tick += Timer_Tick;
timer.Start();
}
/// <summary>
/// 定时处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer_Tick(object sender, EventArgs e)
{
if (isRun)
{
string str = $"No. {count} item";
ListBoxSaveAndAdd(filename,listBox1, str, MAX_COUNT);
count++;
}
}
/// <summary>
/// 测试开启和停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btStart_Click(object sender, EventArgs e)
{
if (btStart.Text.Equals("开始"))
{
btStart.Text = "停止";
listBox1.Items.Clear();
filename = Path.Combine(Environment.CurrentDirectory, "f"+DateTime.Now.ToString("yyyyMMdd_HHmmssFFF")+".txt");
count = 0;
isRun = true;
}
else
{
btStart.Text = "开始";
isRun = false;
}
}
#region 处理算法
/// <summary>
/// 数据保存和ListBox添加
/// </summary>
/// <param name="filename"></param>
/// <param name="listBox"></param>
/// <param name="text"></param>
/// <param name="maxCount"></param>
public static void ListBoxSaveAndAdd(string filename, ListBox listBox, string text, int maxCount)
{
FileStreamWriter(filename, text);
ListBoxScrollAddCross(listBox, text, maxCount);
}
/// <summary>
/// 文件保存
/// </summary>
/// <param name="filename"></param>
/// <param name="content"></param>
public static void FileStreamWriter(string filename, string content)
{
using (StreamWriter sw = new StreamWriter(filename, true))
{
sw.WriteLine(content);
}
}
/// <summary>
/// listBox跨域处理
/// </summary>
/// <param name="listBox"></param>
/// <param name="text"></param>
/// <param name="maxCount"></param>
public static void ListBoxScrollAddCross(ListBox listBox, string text, int maxCount)
{
if (listBox.InvokeRequired)
{
MethodInvoker mi = delegate ()
{
ListBoxScrollAdd(listBox, text, maxCount);
};
listBox.Invoke(mi);
}
else
{
ListBoxScrollAdd(listBox, text, maxCount);
}
}
/// <summary>
/// listBox添加
/// </summary>
/// <param name="listBox"></param>
/// <param name="text"></param>
/// <param name="maxCount"></param>
public static void ListBoxScrollAdd(ListBox listBox, string text, int maxCount)
{
listBox.Items.Add(text);
if (listBox.Items.Count > maxCount)
{
listBox.Items.RemoveAt(0);
}
listBox.TopIndex = listBox.Items.Count - (int)(listBox.Height / listBox.ItemHeight);
}
#endregion
}
}
3、运行结果