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

1、程序

csharp 复制代码
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、运行效果

相关推荐
凌霜残雪5 个月前
基于ListBox制作一个好看的侧边菜单导航栏
wpf·菜单·listbox
taller_20001 年前
限制LitstBox控件显示指定行数的最新数据(3/3)
控件·listbox·动态加载控件·显示指定行·userform
taller_20001 年前
限制LitstBox控件显示指定行数的最新数据(2/3)
listbox·列表框·动态加载·动态下拉列表·动态列表框