ListBox表现最新数据、左移和右移利用

[复制链接]
发表于 2026-1-24 15:29:57 | 显示全部楼层 |阅读模式
1、步伐
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
  12. namespace ListBoxApp
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Timer timer;
  17.         bool isRun = false;
  18.         int count = 0;
  19.         int MAX_COUNT = 20;
  20.         string filename = "";
  21.         /// <summary>
  22.         /// 构造函数
  23.         /// </summary>
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.             MoveInitialize();
  28.         }
  29.         /// <summary>
  30.         /// 窗体加载
  31.         /// </summary>
  32.         /// <param name="sender"></param>
  33.         /// <param name="e"></param>
  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.             timer = new Timer();
  37.             timer.Interval = 500;
  38.             timer.Tick += Timer_Tick;
  39.             timer.Start();
  40.         }
  41.         /// <summary>
  42.         /// 定时处理
  43.         /// </summary>
  44.         /// <param name="sender"></param>
  45.         /// <param name="e"></param>
  46.         private void Timer_Tick(object sender, EventArgs e)
  47.         {
  48.             if (isRun)
  49.             {
  50.                 string str = $"No. {count} item";
  51.                 ListBoxSaveAndAdd(filename,listBox1, str, MAX_COUNT);
  52.                 count++;
  53.             }
  54.         }
  55.         /// <summary>
  56.         /// 测试开启和停止
  57.         /// </summary>
  58.         /// <param name="sender"></param>
  59.         /// <param name="e"></param>
  60.         private void btStart_Click(object sender, EventArgs e)
  61.         {
  62.             if (btStart.Text.Equals("开始"))
  63.             {
  64.                 btStart.Text = "停止";
  65.                 listBox1.Items.Clear();
  66.                 filename = Path.Combine(Environment.CurrentDirectory, "f"+DateTime.Now.ToString("yyyyMMdd_HHmmssFFF")+".txt");
  67.                 count = 0;
  68.                 isRun = true;
  69.             }
  70.             else
  71.             {
  72.                 btStart.Text = "开始";
  73.                 isRun = false;
  74.             }
  75.         }
  76.         #region 显示(最新数据)和保存
  77.         /// <summary>
  78.         /// 数据保存和ListBox添加
  79.         /// </summary>
  80.         /// <param name="filename"></param>
  81.         /// <param name="listBox"></param>
  82.         /// <param name="text"></param>
  83.         /// <param name="maxCount"></param>
  84.         public static void ListBoxSaveAndAdd(string filename, ListBox listBox, string text, int maxCount)
  85.         {
  86.             FileStreamWriter(filename, text);
  87.             ListBoxScrollAddCross(listBox, text, maxCount);
  88.         }
  89.         /// <summary>
  90.         /// 文件保存
  91.         /// </summary>
  92.         /// <param name="filename"></param>
  93.         /// <param name="content"></param>
  94.         public static void FileStreamWriter(string filename, string content)
  95.         {
  96.             using (StreamWriter sw = new StreamWriter(filename, true))
  97.             {
  98.                 sw.WriteLine(content);
  99.             }
  100.         }
  101.         /// <summary>
  102.         /// listBox跨域处理
  103.         /// </summary>
  104.         /// <param name="listBox"></param>
  105.         /// <param name="text"></param>
  106.         /// <param name="maxCount"></param>
  107.         public static void ListBoxScrollAddCross(ListBox listBox, string text, int maxCount)
  108.         {
  109.             if (listBox.InvokeRequired)
  110.             {
  111.                 MethodInvoker mi = delegate ()
  112.                 {
  113.                     ListBoxScrollAdd(listBox, text, maxCount);
  114.                 };
  115.                 listBox.Invoke(mi);
  116.             }
  117.             else
  118.             {
  119.                 ListBoxScrollAdd(listBox, text, maxCount);
  120.             }
  121.         }
  122.         /// <summary>
  123.         /// listBox添加
  124.         /// </summary>
  125.         /// <param name="listBox"></param>
  126.         /// <param name="text"></param>
  127.         /// <param name="maxCount"></param>
  128.         public static void ListBoxScrollAdd(ListBox listBox, string text, int maxCount)
  129.         {
  130.             listBox.Items.Add(text);
  131.             if (listBox.Items.Count > maxCount)
  132.             {
  133.                 listBox.Items.RemoveAt(0);
  134.             }
  135.             listBox.TopIndex = listBox.Items.Count - (int)(listBox.Height / listBox.ItemHeight);
  136.         }
  137.         #endregion
  138.         #region 左移和右移
  139.         /// <summary>
  140.         /// 移动初始化
  141.         /// </summary>
  142.         private void MoveInitialize()
  143.         {
  144.             listBox2.Items.Add("产品设计");
  145.             listBox2.Items.Add("硬件设计");
  146.             listBox2.Items.Add("逻辑设计");
  147.             listBox2.Items.Add("软件设计");
  148.         }
  149.         /// <summary>
  150.         /// 右移
  151.         /// </summary>
  152.         /// <param name="sender"></param>
  153.         /// <param name="e"></param>
  154.         private void BtnAddProject_Click(object sender, EventArgs e)
  155.         {
  156.             if (listBox2.Items.Count > 0)
  157.             {
  158.                 CurrentMove(listBox2, listBox3);
  159.             }
  160.         }
  161.         /// <summary>
  162.         /// 左移
  163.         /// </summary>
  164.         /// <param name="sender"></param>
  165.         /// <param name="e"></param>
  166.         private void BtnRemoveProject_Click(object sender, EventArgs e)
  167.         {
  168.             if (listBox3.Items.Count > 0)
  169.             {
  170.                 CurrentMove(listBox3, listBox2);
  171.             }     
  172.         }
  173.         /// <summary>
  174.         /// 移动处理
  175.         /// </summary>
  176.         /// <param name="source"></param>
  177.         /// <param name="target"></param>
  178.         public void CurrentMove(ListBox source,ListBox target)
  179.         {
  180.             foreach(var selectItem in source.SelectedItems)
  181.             {
  182.                 target.Items.Add((string)selectItem);
  183.             }
  184.             List<int> list = new List<int>();
  185.             foreach(int item in source.SelectedIndices)
  186.             {
  187.                 list.Add(item);
  188.             }
  189.             for(int i=list.Count-1; i>=0; i--)
  190.             {
  191.                 source.Items.RemoveAt(list[i]);
  192.             }
  193.         }
  194.         #endregion
  195.     }
  196. }
复制代码
2、运行结果


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表