当前位置: 首页 > article >正文

ListBox显示最新数据、左移和右移操作

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、运行效果
在这里插入图片描述


http://www.kler.cn/a/306568.html

相关文章:

  • Java结合ElasticSearch根据查询关键字,高亮显示全文数据。
  • elementui el-table中给表头 el-table-column 加一个鼠标移入提示说明
  • 前端常用布局模板39套,纯CSS实现布局
  • 【机器学习】机器学习中用到的高等数学知识-2.概率论与统计 (Probability and Statistics)
  • Redis高可用-主从复制
  • Python多进程间通讯(包含共享内存方式)
  • K8s中HPA自动扩缩容及hml
  • idea2024.2永久使用
  • MFC工控项目实例之十五定时刷新PC6325A模拟量输入
  • HTML添加文字
  • 【深度学习】Pytorch基础
  • 分享一些成功的 SQL 优化案例
  • 2024工业机器视觉产业现状
  • 多模态大语言模型综述(中)-算法实用指南
  • 如何在Django中创建新的模型实例
  • MFC工控项目实例之十六输入信号验证
  • app抓包 chrome://inspect/#devices
  • 2024.9.12(k8s环境搭建2)
  • WebSocket vs. Server-Sent Events:选择最适合你的实时数据流技术
  • VUE3中ref与reactive
  • Sentinel 安装
  • BSV区块链上的覆盖网络服务现已开放公测
  • 常回家看看之house_of_cat
  • 基于单片机的超声波液位检测系统(论文+源码)
  • STM32 HAL freertos零基础(二)-通过STM32CubeMX配置Freertos后在程序中进行任务创建,便于任务管理与识别。
  • 微服务保护之熔断降级