1、程序
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();
MoveInitialize();
}
/// <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
#region 左移和右移
/// <summary>
/// 移动初始化
/// </summary>
private void MoveInitialize()
{
listBox2.Items.Add("产品设计");
listBox2.Items.Add("硬件设计");
listBox2.Items.Add("逻辑设计");
listBox2.Items.Add("软件设计");
}
/// <summary>
/// 右移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnAddProject_Click(object sender, EventArgs e)
{
if (listBox2.Items.Count > 0)
{
CurrentMove(listBox2, listBox3);
}
}
/// <summary>
/// 左移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnRemoveProject_Click(object sender, EventArgs e)
{
if (listBox3.Items.Count > 0)
{
CurrentMove(listBox3, listBox2);
}
}
/// <summary>
/// 移动处理
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
public void CurrentMove(ListBox source,ListBox target)
{
foreach(var selectItem in source.SelectedItems)
{
target.Items.Add((string)selectItem);
}
List<int> list = new List<int>();
foreach(int item in source.SelectedIndices)
{
list.Add(item);
}
for(int i=list.Count-1; i>=0; i--)
{
source.Items.RemoveAt(list[i]);
}
}
#endregion
}
}
2、运行效果